
正文
Nginx+lua环境搭建
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
其实有点类似WampServer一站式安装包
wget http://openresty.org/download/ngx_openresty-1.7.10.1.tar.gztar -zxvf ngx_openresty-1.7.10.1.tar.gzcd ngx_openresty-1.7.10.1./configuremake && make install
configure之前需要安装的相关组件
yum -y install pcre-develyum -y install gcc zlib zlib-devel openssl openssl-devel
安装目录默认会在
cd /usr/local/openresty/
测试lua环境是否正常
luaprint("hello")
测试nginx+lua
cd /usr/local/openresty/nginx/conf
在nginx.conf里添加测试内容
location /hello { default_type 'text/plain'; content_by_lua 'ngx.say("ok")'; }
location /say {
default_type text/html;
content_by_lua '
ngx.say("<p>hello, world</p>")
ngx.say(tostring(ngx.var.remote_addr),"<br/>")
ngx.say(tostring(ngx.var.arg_name),"<br/>")
';
}
启动nginx
sbin/nginx
测试:
[root@k2 nginx]# curl 'http://localhost/hello'ok
浏览器访问:


测试3:
location ~ /lua_request/(\d+)/(\d+) { #设置nginx变量 ; set $b $host; default_type "text/html"; #nginx内容处理 content_by_lua_file /test/test_request.lua; #内容体处理完成后调用 echo_after_body "ngx.var.b $b"; }
test_request.lua
--nginx变量local var = ngx.varngx.say("ngx.var.a : ", var.a, "<br/>")ngx.say("ngx.var.b : ", var.b, "<br/>")ngx.say(], "<br/>")ngx.;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/>")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/>") endendngx.say("headers end", "<br/>")ngx.say("<br/>")--get请求uri参数ngx.say("uri args begin", "<br/>")local uri_args = ngx.req.get_uri_args()for k, v in pairs(uri_args) do if type(v) == "table" then ngx.say(k, " : ", table.concat(v, ", "), "<br/>") else ngx.say(k, ": ", v, "<br/>") endendngx.say("uri args end", "<br/>")ngx.say("<br/>")--post请求参数ngx.req.read_body()ngx.say("post args begin", "<br/>")local post_args = ngx.req.get_post_args()for k, v in pairs(post_args) do if type(v) == "table" then ngx.say(k, " : ", table.concat(v, ", "), "<br/>") else ngx.say(k, ": ", v, "<br/>") endendngx.say("post args end", "<br/>")ngx.say("<br/>")--请求的http协议版本ngx.say("ngx.req.http_version : ", ngx.req.http_version(), "<br/>")--请求方法ngx.say("ngx.req.get_method : ", ngx.req.get_method(), "<br/>")--原始的请求头内容ngx.say("ngx.req.raw_header : ", ngx.req.raw_header(), "<br/>")--请求的body内容体ngx.say("ngx.req.get_body_data() : ", ngx.req.get_body_data(), "<br/>")ngx.say("<br/>")
脚本测试
wget --post-data 'a=1&b=2' 'http://127.0.0.1/lua_request/1/2?a=3&b=4' -O -
访问:
http://192.168.1.120/lua_request/1/2?a=3&b=4

参考:
http://openresty.org/
https://m.oschina.net/blog/344540
http://jinnianshilongnian.iteye.com/blog/2186448








