setup.lua
1.01 KB
----- 内部域名如果http头中带有 X-YOHO-IP,则是PC/h5过来的请求,直接用这个作为real ip
----- 经过动态CDN,如果x-forward-for 只有1个IP地址,则取第一个;>=2个IP地址,例如 ip1,ip2,ip3,ip4, 则取倒数第二个,即ip3
----- 请求不经过动态CDN,取 x-forward-for 中最后一个IP地址,例如 ip1,ip2,ip3,ip4,取ip4
local realIp = ngx.req.get_headers()["X-YOHO-IP"]
if realIp and (string.find(ngx.var.http_host,"yohoops%.org") or string.find(ngx.var.http_host,"yohops%.com")) then
return realIp
end
local xForwardFor = ngx.var.http_x_forwarded_for;
if xForwardFor then
local ips={}
string.gsub(xForwardFor,'[^,]+',function(w) table.insert(ips, w) end )
if string.find(ngx.var.http_host,"api%-dc",1) == 1 then
if #ips >= 2 then
realIp=ips[#ips-1]
elseif #ips<=1 then
realIp=ips[1]
end
else
realIp=ips[#ips]
end
end
-- if not exist, then setup remote_addr
if realIp == nil then
realIp = ngx.var.remote_addr;
end
return realIp;