Lua gets the post request data of nginx and writes it to redis

Time:2019-11-8

1. Environmental installation

  1. Lnmp.conf sets enable? Nginx? Lua =’y ‘. Then install as usual.
  2. LNMP. / addons.sh install redis. If you are connected to a remote redis server, you do not need to install it.
  3. Install Lua
Ubuntu install Lua apt get install Lua 
CentOS installation Lua
curl -R -O http://www.lua.org/ftp/lua-5.3.0.tar.gz
tar zxf lua-5.3.0.tar.gz
cd lua-5.3.0
make linux test
make install

If readLine. H does not exist:

ubuntu: sudo apt-get install libreadline-dev 
centos: yum install libtermcap-devel ncurses-devel libevent-devel readline-devel
  1. mkdir /usr/local/nginx/conf/lua
  2. Download Lua resty redis and dkjson.lua.

//Operating redis
https://github.com/openresty/lua-resty-redis
//Parsing JSON’s
http://dkolf.de/src/dkjson-lua.fsl/home

CD Lua resty redis execute make install
cp dkjson.lua  /usr/local/lib/lua

2. Configure nginx

http:{
...
lua_package_path '/usr/local/lib/lua/?.lua;';
Lua? Package? Cpath '/ usr / local / lib / Lua /?. so;';? If cjson is used
...

server{
    location /lua
        {
            lua_need_request_body on ;
            default_type 'text/html';
            content_by_lua_file conf/lua/test.lua;
        }
    }
...
}

3. Write Lua code

--Ngx.say (package. Path) -- view the library path of this file
local redis = require "resty.redis"
local dkjson = require "dkjson"
--local cjson = require('cjson');


--Create instance
local redis_instance = redis:new()
--Set timeout (MS)
redis_instance:set_timeout(3000)

--Establish a connection
local host = "127.0.0.1"
local port = 6379
local ok, err = redis_instance:connect(host, port)
if not ok then
    ngx.say("connect to redis error : ", err)
    return close_redis(redis_instance)
end

--Redis authentication
local auth,err = redis_instance:auth("test123");
if not auth then
    ngx.say("failed to authenticate : ",err)
    return close_redis(redis_instance)
end

-- selection Library
local db,err = redis_instance:select("8");
if not db then
    ngx.say("failed to selectdb : ",err)
    return close_redis(redis_instance)
end

--Redis off
local function close_redis(redis_instance)
    if not redis_instance then
        return
    end
    local ok,err = redis_instance:close();
    if not ok then
        ngx.say("close redis error : ",err);
    end
end

--Get request body data
local request_method = ngx.var.request_method
local args = nil
local Body = nil
local CurTime = nil
local CheckSum = nil
local MD5 = nil
local host = nil

if "GET" == request_method then
    args = ngx.req.get_uri_args()
elseif "POST" == request_method then
    ngx.req.read_body()
    args = ngx.req.get_post_args()
    Body = ngx.req.get_body_data() 
end

--Get request header information
local receive_headers = ngx.req.get_headers()
for k, v in pairs(receive_headers) do
     if k == "host" then host = v end
     if k == "curtime" then  CurTime = v end
     if k == "checksum" then  CheckSum = v end
     if k == "md5" then  MD5 = v end
end
ngx.say(host)
-- parsing body
local obj,pos,err = dkjson.decode(Body,1,nil)
--ngx.say(obj.eventType, "<br/>")

local json_string = nil
if (obj.eventType == "1") or (obj.eventType == "5") then
    local a = {["CHECKSUM"] = CheckSum, ["CURTIME"] = CurTime, ["MD5"] = MD5,["Body"] = Body,["Host"] = host}
    json_string = dkjson.encode(a,{ indent = true })
    --ngx.say(a["CHECKSUM"])
    --json_string =( string.format("CHECKSUM:%s--&||&--CURTIME:%s--&||&--MD5:%s--&||&--BODY:%s",CheckSum,CurTime,MD5,Body) );
end

--If it is a parameter passed like "name = 11 & sex = 22"
--[[
for key, val in pairs(args) do
    if type(val) == "table" then
        ngx.say(key, ": ", table.concat(val, ", "))    
    else
        --ngx.say(key,':',val)
        --redis_instance.call('set',key)    
        end
    end
end
]]--

ngx.say(json_string)
redis_instance:lpush('post_list', json_string)
close_redis(redis_instance)

ngx.say("OK")

Put the code in / usr / local / nginx / conf / Lua directory and restart nginx