目前大部分获取ip的方式:beat.getRequest().getRemoteAddr()
但是,如果通过nginx反向代理的话,就获取不到真实ip,是获取的nginx的ip
需要:添加 proxy_set_header X-Real-IP $remote_addr;
server { listen 80 ; server_name price.kuche.com; location / { root html; proxy_set_header X -Real- IP $remote_addr; proxy_pass http:backServers ; } }
添加一个表头声明
然后这样取
public static String getRemoteAddrIp(HttpServletRequest request) { String ipFromNginx = getHeader(request, "X-Real-IP" ); System.out.println( "ipFromNginx:" + ipFromNginx); System.out.println( "getRemoteAddr:" + request.getRemoteAddr()); return StringUtil.isEmpty(ipFromNginx) ? request.getRemoteAddr() : ipFromNginx; } private static String getHeader(HttpServletRequest request, String headName) { String value = request.getHeader(headName); return !StringUtils.isBlank(value) && !"unknown".equalsIgnoreCase(value) ? value : "" ; }