Java之获取Nginx代理之后的客户端IP
Nginx代理接口之后,后台获取的IP地址都是127.0.0.1,解决办法是需要配置Nginx搭配后台获取的方法,获得设备的真实地址。我们想要获取的就是nginx代理日志中的这个IP
nginx配置
首先在nginx代理的对应location增加如下配置
#保留代理之前的host 包含客户端真实的域名和端口号
proxy_set_header Host $host;
#保留代理之前的真实客户端ip
proxy_set_header X-Real-IP $remote_addr;
#这个Header和X-Real-IP类似,但它在多级代理时会包含真实客户端及中间每个代理服务器的IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#表示客户端真实的协议(http还是https)
proxy_set_header X-Forwarded-Proto $scheme;
示例
server {listen 9010;server_name 127.0.0.1;location ^~ /tob_server/ {#代理地址proxy_pass http://127.0.0.1:9012/tob_server/;#保留代理之前的host 包含客户端真实的域名和端口号proxy_set_header Host $host; #保留代理之前的真实客户端ipproxy_set_header X-Real-IP $remote_addr; #这个Header和X-Real-IP类似,但它在多级代理时会包含真实客户端及中间每个代理服务器的IPproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#表示客户端真实的协议(http还是https)proxy_set_header X-Forwarded-Proto $scheme;#解决跨域add_header Access-Control-Allow-Origin *;#解决nginx转发丢失cookie的问题proxy_cookie_path /tob_server/ /;#解决options请求if ($request_method = 'OPTIONS') {add_header 'Access-Control-Max-Age' 1728000;add_header 'Content-Type' 'text/plain; charset=utf-8';add_header 'Content-Length' 0;return 204;}}location \ {root html;index index.html index.htm;}#解决代理异常error_page 404 500 502 503 504 /50x.html;location = /50x.html {root html;}
}
java获取客户端IP
获取客户端IP地址工具类
public class IpUtils
{private static Logger logger = LoggerFactory.getLogger(IPUtils.class);/*** 获取客户端IP* * @param request 请求对象* @return IP地址*/public static String getIpAddr(HttpServletRequest request){if (request == null){return "unknown";}String ip = request.getHeader("x-forwarded-for");if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ip = request.getHeader("Proxy-Client-IP");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ip = request.getHeader("X-Forwarded-For");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ip = request.getHeader("WL-Proxy-Client-IP");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ip = request.getHeader("X-Real-IP");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ip = request.getRemoteAddr();}return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : getMultistageReverseProxyIp(ip);}/*** 从多级反向代理中获得第一个非unknown IP地址** @param ip 获得的IP地址* @return 第一个非unknown IP地址*/public static String getMultistageReverseProxyIp(String ip){// 多级反向代理检测if (ip != null && ip.indexOf(",") > 0){final String[] ips = ip.trim().split(",");for (String subIp : ips){if (false == isUnknown(subIp)){ip = subIp;break;}}}return StringUtils.substring(ip, 0, 255);}/*** 检测给定字符串是否为未知,多用于检测HTTP请求相关** @param checkString 被检测的字符串* @return 是否未知*/public static boolean isUnknown(String checkString){return StringUtils.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);}
}
这样就可以通过代码获取到客户端的真实IP地址了。
参考文章:
https://blog.csdn.net/u012812482/article/details/126066550