分类: LINUX
2015-11-21 19:48:04
listen 80;
server_name localhost;
location /{
root html;
index index.html index.htm;
proxy_set_header X-real-ip $remote_addr; # 可直接获取客户端IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 通过代理服务器获取客户端IP
}
-----------------------------------------------------------------------------
创建Client_ip.lua 程序,通过读取ngx.var.remote_addr 值,即可获得IP值。
local ClientIP = ngx.req.get_headers()["X-real-ip"]
ngx.say("IP:",ClientIP)
ngx.say(type(ClientIP))
if ClientIP ==nil then
ClientIP = ngx.req.get_headers()["X-Forwarded-For"]
ngx.say("IP:",ClientIP)
ngx.say(type(ClientIP))
end
local ip_addr = tostring(ngx.var.remote_addr)
--if ClientIP ==nil then
-- ClientIP = ngx.var.remote.addr
--end
ngx.say("IP:",ClientIP)
ngx.say("IP:",ip_addr)