Nginx添加Lua扩展模块

编译安装LuaJIT

wget  http://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar xf LuaJIT-2.0..tar.gz
cd LuaJIT-2.0.
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

编辑安装nginx

yum 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..tar.gz && cd nginx-1.10.
./configure \
--prefix=/usr/local/nginx-1.10. \ # nginx安装目录
--with-http_ssl_module \ # 支持 SSL
--with-http_stub_status_module \ # nginx状态模块
--add-module=/usr/local/src/ngx_devel_kit-0.3. \ # lua模块
--add-module=/usr/local/src/lua-nginx-module-0.10. # lua扩展模块
make && make install
mkdir /usr/local/nginx-1.10./conf/vhost
ln -s /usr/local/nginx-1.10./sbin/nginx /bin/nginx

编译安装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./
./configure \
--prefix=/usr/local/nginx-1.10. \ # nginx安装目录
--with-http_ssl_module \ # 支持 SSL
--with-http_stub_status_module \ # nginx状态模块
--add-module=/usr/local/src/ngx_devel_kit-0.3. \ # lua模块
--add-module=/usr/local/src/lua-nginx-module-0.10. # lua扩展模块
make
make install

遇到的报错:

# nginx -t
nginx: error while loading shared libraries: libluajit-5.1.so.: cannot open shared object file: No such file or directory # 解决: 查找文件,创建软连接
find / -name "libluajit-5.1.so.2"
ln -s /usr/local/lib/libluajit-5.1.so. /lib64/

第一个lua脚本

在server块中添加
location /lua { default_type 'text/html'; content_by_lua_file conf/lua/test.lua;    # 相对于nginx安装目录 }

# 编写lua脚本
[root@yunwei-test conf]# pwd
[root@yunwei-test conf]# /usr/local/nginx-1.10./conf
[root@yunwei-test conf]# mkdir lua && cd lua
[root@yunwei-test conf]# vim test.lua
ngx.say("hello world"); # 启动nginx
[root@yunwei-test conf]# nginx -t
[root@yunwei-test conf]# nginx 浏览器访问:10.0.3.56/lua
显示 hello world,表示正常

nginx + lua获取url请求参数

有个需求就是获取 url 中 clientId 参数的值,根据clientid中的参数upstream到不同服务器,url有GET和POST请求。

代码如下:

upstream sdk_proxy {
server 127.0.0.1:;
keepalive ;
} upstream default_sdk {
server 127.0.0.1:;
keepalive ;
} server {
listen ;
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 = {"","",""} if (request_method == "GET") then
local arg = ngx.req.get_uri_args()["clientId"] or
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
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

最新文章

  1. [译]Modern Core Graphics with Swift系列
  2. HTTP缓存&代理
  3. 1奶茶店创业成本: 2发饰品加盟店创业成本 3眼镜行业店创业成本 从“程序员转行卖烧饼”想到IT人创业
  4. sqlserver中的表值函数和标量值函数
  5. Spring学习总结(二)——静态代理、JDK与CGLIB动态代理、AOP+IoC
  6. AngularJS中的digest循环$apply
  7. java中的静态代码块、构造代码块、构造方法
  8. eclipse中Preferences的一些设置
  9. canvas实现绘画
  10. OS X EI Capitan 10.11.1快速升级方法介绍
  11. BZOJ 1008 越狱 (组合数学)
  12. FPGA与Deep Learning
  13. C# 定时关机小程序
  14. pwn学习之一
  15. WCF 重载
  16. 解决win7无法运行bat批处理文件的方法
  17. 图论.DP
  18. rook 排错记录 + Orphaned pod found kube-controller-manager的日志输出
  19. laravel iis搭建
  20. 数据库 - SQLite3 中的数据类型

热门文章

  1. 3ds Max建模,Blend设计,VS2008控制WPF的3D模型例子
  2. ios-利用键盘通知处理键盘出现时遮挡控件问题
  3. javascript-DOM学习
  4. abp框架(aspnetboilerplate)扩展系统表
  5. 从PRISM开始学WPF(八)导航Navigation?
  6. NUGET源不存在,安装Nuget包提示“本地源不存在”
  7. 发布ActiveX控件
  8. 微信小程序把玩(二十七)audio组件
  9. Android多线程(三)
  10. 论文阅读计划2(Deep Joint Rain Detection and Removal from a Single Image)