• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Nginx添加Lua扩展模块

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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 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.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

# 查看之前的编译参数
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

# 解决: 查找文件,创建软连接
find / -name "libluajit-5.1.so.2" 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    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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
如何安装nginx_lua_module模块发布时间:2022-07-22
下一篇:
nginx图片处理笔记(http-image-filter-module、lua)发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap