IpKit.getRealIp有时候可以获得到真实ip,有时候不能,前端NGINX

image.png

都是调用的同一个方法,为什么会有两种不同的结果?

评论区

杜福忠

2018-01-05 23:47

把NGINX的配置贴出来看看,有
proxy_set_header Host $host;
proxy_set_header x-forwarded-for $remote_addr;
吗?
看看kit工具里面写的:
/**
* IpKit 获取 ip 地址的工具类
*/
public class IpKit {

public static String getRealIp(HttpServletRequest request) {
String ip = request.getHeader("X-Real-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("Proxy-Client-IP");
}
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.getRemoteAddr();
}
return ip;
}
所以NGINX得带着传过来的才有的

热门反馈

扫码入社