Lua获取Nginx的Post请求数据并写入Redis

1.环境安装

  1. lnmp.conf 设置 Enable_Nginx_Lua='y'。然后按通常情况安装。
  2. lnmp ./addons.sh安装redis,如果连接远程redis服务器不用装。
  3. 安装lua
ubuntu安装lua apt-get install lua 
centos安装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

如果readline.h不存在:

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. 下载 lua-resty-redis 和 dkjson.lua 。

//操作Redis的
https://github.com/openresty/lua-resty-redis
//解析json的
http://dkolf.de/src/dkjson-lua.fsl/home

cd lua-resty-redis  执行 make install
cp dkjson.lua  /usr/local/lib/lua

2.配置nginx

http:{
...
lua_package_path '/usr/local/lib/lua/?.lua;';
lua_package_cpath '/usr/local/lib/lua/?.so;'; #如果用cjson的话
...

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

3. 写lua代码

--ngx.say(package.path) --查看本文件的库路径
local redis = require "resty.redis"
local dkjson = require "dkjson"
--local cjson = require('cjson');


--创建实例
local redis_instance = redis:new()
--设置超时(毫秒)
redis_instance:set_timeout(3000)

--建立连接
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身份验证
local auth,err = redis_instance:auth("test123");
if not auth then
    ngx.say("failed to authenticate : ",err)
    return close_redis(redis_instance)
end

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

--Redis关闭
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

--获取请求body数据
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

--获取请求头信息
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)
--解析body
local obj,pos,err = dkjson.decode(Body,1,nil)
--ngx.say(obj.eventType, "
") 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 --如果是"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")

代码放到/usr/local/nginx/conf/lua 目录下,重启nginx

你可能感兴趣的:(nginx,lua)