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不存在:html
ubuntu: sudo apt-get install libreadline-dev centos: yum install libtermcap-devel ncurses-devel libevent-devel readline-devel
//操做Redis的
https://github.com/openresty/lua-resty-redis
//解析json的
http://dkolf.de/src/dkjson-lua.fsl/homelinux
cd lua-resty-redis 執行 make install cp dkjson.lua /usr/local/lib/lua
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; } } ... }
--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, "<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 --若是是"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 目錄下,重啓nginxnginx