–
[root@master-10 /usr/local/nginx/conf/conf.d]#cat 80.conf # # The default server # server { listen 80 default_server; server_name _; root /data/web; location / { index index.html index.htm index.php; } location /lua_request { default_type text/html; lua_code_cache off; content_by_lua_file /lua/lua_request.lua; } location /lua_response { default_type text/html; lua_code_cache off; content_by_lua_file /lua/lua_response.lua; } location /lua_log { default_type text/html; lua_code_cache off; content_by_lua_file /lua/lua_log.lua; } location /lua_sum { internal; content_by_lua_block { local args = ngx.req.get_uri_args() ngx.say(tonumber(args.a)+tonumber(args.b)) } } location /lua_sum_test { content_by_lua_block { local res = ngx.location.capture('/lua_sum', {args={a=3,b=8}}) ngx.say('status:', res.status, ' response:', res.body) } } location /lua_redirect { default_type text/html; lua_code_cache off; content_by_lua_file /lua/lua_redirect.lua; } } [root@master-10 /lua]#ls lua_log.lua lua_redirect.lua lua_request.lua lua_response.lua [root@master-10 /lua]#cat lua_log.lua local log = "i'm log\n the second line " local num = 10 ngx.log(ngx.ERR, 'log', log) ngx.log(ngx.WARN, 'num', num) [root@master-10 /lua]#cat lua_redirect.lua ngx.redirect('http://baidu.com', 302) [root@master-10 /lua]#cat lua_request.lua local arg = ngx.req.get_uri_args() for k,v in pairs(arg) do ngx.say("[GET] key:", k, " v:", v) end ngx.req.read_body() local arg = ngx.req.get_post_args() for k,v in pairs(arg) do ngx.say("[POST] key:", k, " v:", v) end ngx.say('<br/>') local headers = ngx.req.get_headers() ngx.say('headers begin', '<br/>') ngx.say('Host: ', headers['Host'], '<br/>') ngx.say('user-agent: ', headers['user-agent'], '<br/>') ngx.say('user-agent: ', headers.user_agent, '<br/>') ngx.say('<br/>') for k,v in pairs(headers) do if type(v) == 'table' then ngx.say(k, ' : ', table.concat(v, ','), '<br/>') else ngx.say(k, ' : ', v, '<br/>') end end ngx.say('<br/>') ngx.say(ngx.req.http_version(), '<br/>') ngx.say(ngx.req.get_method(), '<br/>') ngx.say(ngx.req.raw_header(), '<br>') ngx.say(ngx.req.get_body_data(), '<br/>') ngx.say('<br/>') local ua = ngx.var.http_user_agent local uri = ngx.var.request_uri local url = ngx.var.host .. uri local ip = ngx.var.remote_addr ngx.say(ua, '<br/>') ngx.say(uri, '<br/>') ngx.say(url, '<br/>') ngx.say(ip, '<br/>') [root@master-10 /lua]#cat lua_response.lua ngx.header.a='1' ngx.header.b={'a', 'b'} ngx.say('hello', '<br/>') ngx.say('status', ngx.status, '<br/>') ngx.print('sss', '<br/>') return ngx.exit(304)
–
–
–
评论前必须登录!
注册