[nginx]-nginx使用lua模块处理和转发post请求

安装LuaJIT

cd /opt
wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
tar -xvf LuaJIT-2.0.2.tar.gz
cd LuaJIT-2.0.2
make install

# 下面这条解决找不到libluajit-5.1.so.2的报错
ln -s /usr/local/lib/libluajit-5.1.so.2 /usr/lib/

安装nginx

wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
wget http://nginx.org/download/nginx-1.14.0.tar.gz 

tar -xvf v0.3.0.tar.gz
tar -xvf v0.10.9rc7.tar.gz
tar -xvf nginx-1.14.0.tar.gz

cd nginx-1.14.0
./configure --prefix=/usr/local/nginx --add-module=../ngx_devel_kit-0.3.0 --add-module=../lua-nginx-module-0.10.9rc7  --with-http_ssl_module  --with-http_stub_status_module  --with-http_gzip_static_module

make && make install
mkdir -p /usr/local/nginx/lua
/usr/local/nginx/sbin/nginx

安装lua

cd /opt
yum install libtermcap-devel ncurses-devel libevent-devel readline-devel -y 
wget http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar -xzvf lua-5.1.4.tar.gz
cd lua-5.1.4
make linux test
make && make install

安装lua-cjson

5.1和5.3的Makefile生成文件差很多 踩坑了

cd /opt
wget http://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz
tar zxvf lua-cjson-2.1.0.tar.gz
cd lua-cjson-2.1.0
cmake CMakeLists.txt
make
make install

5.1的Makefile 如下

修改nginx.conf如下

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
           proxy_pass http://127.0.0.1:8085;
        }
        location /hello{
        default_type 'text/plain';
        content_by_lua 'ngx.say("hello,lua")';
        }
         location /lua {
             content_by_lua_file "/usr/local/nginx/lua/dispatch.lua";
         }
        location @lua_api_suc {
		 content_by_lua_block {
              ngx.say("转发端口8085")
         }
         }
        location @lua_api_err {
         content_by_lua_block {
              ngx.exec("/")
         }
        }
    }
}

创建/usr/local/nginx/lua/dispatch.lua如下

local request_method = ngx.var.request_method
local arg=nil
if request_method == "GET" then
         arg = ngx.req.get_uri_args()
elseif request_method == "POST" then
         ngx.req.read_body()
         arg = ngx.req.get_post_args()
end

ngx.req.read_body()
local data = ngx.req.get_body_data()

local cjson = require("cjson")
local json = cjson.decode(data)
for k,v in pairs(json) do
    ngx.say("参数输出--")
    ngx.say(k..":"..v)
end
local ip = json["ip"]
--ngx.say("获取到的ip:",ip)

if ip ~= nil and ip == '8085' then
--    ngx.say("进入ip=8085")
--    ngx.exec('@lua_api_suc')
    ngx.say("参数输出111--")
else
--    ngx.exec('@lua_api_err')
    ngx.say("参数输出222--")
end

reload配置

/usr/local/nginx/sbin/nginx -s reload 

使用postman模拟post请求 发送json类型的数据

{
    "ip":"8085"
}

修改dispatch.lua 如下 修改注释的配置

local request_method = ngx.var.request_method
local arg=nil
if request_method == "GET" then
         arg = ngx.req.get_uri_args()
elseif request_method == "POST" then
         ngx.req.read_body()
         arg = ngx.req.get_post_args()
end

ngx.req.read_body()
local data = ngx.req.get_body_data()

local cjson = require("cjson")
local json = cjson.decode(data)
--for k,v in pairs(json) do
 --   ngx.say("参数输出--")
--    ngx.say(k..":"..v)
--end
local ip = json["ip"]
--ngx.say("获取到的ip:",ip)

if ip ~= nil and ip == '8085' then
--    ngx.say("进入ip=8085")
    ngx.exec('@lua_api_suc')
--    ngx.say("参数输出111--")
else
    ngx.exec('@lua_api_err')
--    ngx.say("参数输出222--")
end

如果需要转发http请求 nginx.conf可能需要修改成如下

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location /hello{
        default_type 'text/plain';
        content_by_lua 'ngx.say("hello,lua")';
        }
         location / {
             content_by_lua_file "/usr/local/nginx/lua/dispatch.lua";
         }
        location @lua_api_suc {
proxy_pass http://localhost:8080;
         }
        location @lua_api_err {
proxy_pass http://localhost:8080;
         }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爷来辣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值