在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Nginx添加Lua扩展模块编译安装LuaJITwget http://luajit.org/download/LuaJIT-2.0.4.tar.gz tar xf LuaJIT-2.0.4.tar.gz cd LuaJIT-2.0.4 make PREFIX=/usr/local/luajit make install PREFIX=/usr/local/luajit
下载扩展模块cd /usr/local/src/ wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz tar -xf v0.3.0.tar.gz wget https://github.com/openresty/lua-nginx-module/archive/v0.10.8.tar.gz tar xf v0.10.8.tar.gz
编辑安装nginxyum groupinstall -y "Development Tools" yum install -y libxml2-devel curl-devel pcre-devel openssl-devel siege traceroute vim openssl cd /usr/local/src wget http://nginx.org/download/nginx-1.10.3.tar.gz export LUAJIT_LIB=/usr/local/luajit/lib export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0 tar xf nginx-1.10.3.tar.gz && cd nginx-1.10.3 ./configure \ --prefix=/usr/local/nginx-1.10.2 \ # nginx安装目录 --with-http_ssl_module \ # 支持 SSL --with-http_stub_status_module \ # nginx状态模块 --add-module=/usr/local/src/ngx_devel_kit-0.3.0 \ # lua模块 --add-module=/usr/local/src/lua-nginx-module-0.10.8 # lua扩展模块 make && make install mkdir /usr/local/nginx-1.10.2/conf/vhost ln -s /usr/local/nginx-1.10.3/sbin/nginx /bin/nginx
重新编译nginx# 查看之前的编译参数 nginx -V # 设置环境变量 export LUAJIT_LIB=/usr/local/luajit/lib export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0 # 进入源码包目录 cd /opt/software/nginx-1.10.2/ ./configure \ --prefix=/usr/local/nginx-1.10.2 \ # nginx安装目录 --with-http_ssl_module \ # 支持 SSL --with-http_stub_status_module \ # nginx状态模块 --add-module=/usr/local/src/ngx_devel_kit-0.3.0 \ # lua模块 --add-module=/usr/local/src/lua-nginx-module-0.10.8 # lua扩展模块 make make install
遇到的报错:# nginx -t nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory # 解决: 查找文件,创建软连接
第一个lua脚本在server块中添加 location /lua { default_type 'text/html'; content_by_lua_file conf/lua/test.lua; # 相对于nginx安装目录 }
nginx + lua获取url请求参数有个需求就是获取 url 中 clientId 参数的值,根据clientid中的参数upstream到不同服务器,url有GET和POST请求。
代码如下: upstream sdk_proxy { server 127.0.0.1:188; keepalive 64; } upstream default_sdk { server 127.0.0.1:288; keepalive 64; } server { listen 6443; server_name 127.0.0.1; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location / { default_type text/plain; access_by_lua ' local request_method = ngx.var.request_method local clientids = {"112","113","114"} if (request_method == "GET") then local arg = ngx.req.get_uri_args()["clientId"] or 0 for i,clientid in ipairs(clientids) do if (arg == clientid) then ngx.exec("@sdk") end end elseif (request_method == "POST") then ngx.req.read_body() local arg = ngx.req.get_post_args()["clientId"] or 0 for i,clientid in ipairs(clientids) do if (arg == clientid) then ngx.exec("@sdk") end end end '; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass http://default_sdk; } location @sdk { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass http://sdk_proxy; } }
参考: https://segmentfault.com/q/1010000011130967 |
请发表评论