其实有点类似WampServer一站式安装包
wget http://openresty.org/download/ngx_openresty-1.7.10.1.tar.gz tar -zxvf ngx_openresty-1.7.10.1.tar.gz cd ngx_openresty-1.7.10.1 ./configure make && make install
configure之前需要安装的相关组件
yum -y install pcre-devel yum -y install gcc zlib zlib-devel openssl openssl-devel
安装目录默认会在
cd /usr/local/openresty/
测试lua环境是否正常
lua print("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.var ngx.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/>") end end ngx.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/>") end end ngx.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/>") end end ngx.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