Nginx添加Lua扩展模块

Nginx添加Lua扩展模块

编译安装LuaJIT

wget  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 http://nginx.org/download/nginx-1.10.3.tar.gz
tar -xf v0.3.0.tar.gz

wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
tar xf v0.10.8.tar.gz

 

重新编译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

# 解决:
 ln -s /usr/local/lib/libluajit-5.1.so.2 /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.2/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 10.0.3.36:2443;
    keepalive 64;
}

upstream default_sdk {
    server 10.0.3.26:2443;
    keepalive 64;
}


server {
    listen 2443;
    server_name 10.0.3.56;
   
    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 clientID = "IOS_8.010_tyGuest.appStore.0-hall9980.arow.main"
                
                if (request_method == "GET") then
                       local arg = ngx.req.get_uri_args()["clientId"] or 0    # 取url参数
                       if (arg == clientID) then
                            ngx.exec("@sdk")       # 在内部重定向
                       end
                elseif (request_method == "POST") then
                        ngx.req.read_body()
                        local arg = ngx.req.get_post_args()["clientId"] or 0
                        if (arg == clientID) then
                            ngx.exec("@sdk")
                        end
                end
       ';
 
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://default_sdk;
    }

    location  @sdk {
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://sdk_proxy;
    }
}

 

参考:

https://segmentfault.com/q/1010000011130967

 

转载于:https://www.cnblogs.com/root0/p/10873867.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值