首页 > 其他 > 详细

openresty(nginx)中使用lua脚本获取请求IP地址的代码

时间:2019-11-22 20:57:50      阅读:566      评论:0      收藏:0      [点我收藏+]
人狠话不多,直接上代码:
-------------------------------------------------------------------------------------

local
headers=ngx.req.get_headers() local clientIP = headers["x-forwarded-for"] if clientIP == nil or string.len(clientIP) == 0 or clientIP == "unknown" then clientIP = headers["Proxy-Client-IP"] end if clientIP == nil or string.len(clientIP) == 0 or clientIP == "unknown" then clientIP = headers["WL-Proxy-Client-IP"] end if clientIP == nil or string.len(clientIP) == 0 or clientIP == "unknown" then clientIP = ngx.var.remote_addr end -- 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照‘,‘分割 if clientIP ~= nil and string.len(clientIP) >15 then local pos = string.find(clientIP, ",", 1) clientIP = string.sub(clientIP,1,pos-1) end

 

人狠话不多,直接上代码:

 

openresty(nginx)中使用lua脚本获取请求IP地址的代码

原文:https://www.cnblogs.com/gxyandwmm/p/11913896.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!