location /xxx/ { proxy_pass http://192.168.4.5:8080/xxx/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
public String getRemoteIp(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); logger.info("===========x-forwarded-for======="+ip);//这一步一般能获取到 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); logger.info("===========Proxy-Client-IP======="+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(); } final String[] arr = ip.split(",");for (final String str : arr) { if (!"unknown".equalsIgnoreCase(str)) { ip = str; break; } } return ip; }