103 lines
2.5 KiB
Plaintext
103 lines
2.5 KiB
Plaintext
server {
|
|
listen 9999;
|
|
resolver 233.5.5.5 233.6.6.6 114.114.114.114 valid=3600s;
|
|
|
|
location ~.*\.(jpg|png|jpeg|gif|js|css|ico)$ {
|
|
proxy_pass http://$http_host$request_uri;
|
|
}
|
|
|
|
location / {
|
|
allow 222.73.199.34;
|
|
deny all;
|
|
|
|
default_type 'text/plain';
|
|
access_by_lua_block {
|
|
|
|
function conv2str (o)
|
|
local rs = ""
|
|
if type(o) == "string" then
|
|
rs = o
|
|
elseif type(o) == "nil" then
|
|
rs = "nil"
|
|
elseif type(o) == "number" then
|
|
rs = tostring(o)
|
|
elseif type(o) == "boolean" then
|
|
rs = tostring(o)
|
|
elseif type(o) == "table" then
|
|
for k,v in pairs(o) do
|
|
if type(v)=="string" then
|
|
rs = rs..k.."="..v.."&"
|
|
else
|
|
rs = rs..k.."="..conv2str(v).."&"
|
|
end
|
|
end
|
|
if string.find(rs, "&", string.len(rs)-1) ~= nil then
|
|
rs = string.sub(rs, 0, string.len(rs)-1)
|
|
end
|
|
else
|
|
error("cannot serialize a " .. type(o))
|
|
end
|
|
return rs
|
|
end
|
|
|
|
function postargs()
|
|
ngx.req.read_body()
|
|
local post=""
|
|
local args, err = ngx.req.get_post_args() or {}
|
|
post=conv2str(args)
|
|
return post
|
|
end
|
|
|
|
|
|
function logall()
|
|
local remote=ngx.var.remote_addr or "-"
|
|
local host=ngx.var.http_host or "-"
|
|
local nowtime=ngx.var.time_local or "-"
|
|
local reqMethod=ngx.req.get_method() or "-"
|
|
local reqUri=string.gsub(ngx.var.request_uri, "?.*", "") or "-"
|
|
local args=""
|
|
local post=""
|
|
local headers=ngx.req.get_headers()
|
|
local cookies=conv2str(headers["Cookie"])
|
|
local useragent=conv2str(headers["User-Agent"])
|
|
local header=conv2str(headers)
|
|
local line=""
|
|
|
|
args = conv2str(ngx.req.get_uri_args())
|
|
if reqMethod == "POST" then
|
|
args = postargs()
|
|
end
|
|
|
|
line = '{"method":"'..reqMethod..'", "host":"'..host..'", "uri":"'..reqUri..'", "args":"'..args..'", "cookie":"'..cookies..'", "agent":"'..useragent..'", "remote":"'..remote..'", "nowtime":"'..nowtime..'"}'
|
|
--ngx.say(line)
|
|
|
|
local redis = require "resty.redis"
|
|
local red = redis:new()
|
|
red:set_timeout(1000) -- 1 sec
|
|
local ok, err = red:connect("172.22.1.44", 2000)
|
|
if not ok then
|
|
ngx.say("failed to connect: ", err)
|
|
return
|
|
end
|
|
local res, err = red:auth("SEC")
|
|
red:hmset("proxy."..host, "http://"..host..reqUri, line)
|
|
red:close()
|
|
|
|
end
|
|
|
|
local ret,err = pcall(logall)
|
|
|
|
if ret then
|
|
return
|
|
else
|
|
ngx.say("failed ", err)
|
|
end
|
|
}
|
|
proxy_pass http://$http_host$request_uri;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
}
|
|
}
|
|
|
|
|