获取Nginx代理后WebSocket、Socket的真实IP

注意:nginx代理的配置需要加上保留真实IP的相关配置才能获取到真实iIP

1、Nginx配置

server {
       listen 8989;
       server_name 192.168.1.220;
	   
	   client_max_body_size 50m;
	   
       #access_log  logs/access2.log;
	   #error_log  logs/error2.log;
	   
	   #webSocket访问代理
       location ^~ /Test_WEB_SOCKET/ {
			proxy_pass http://192.168.1.178:8088;
			
			# WebScoket Support
			proxy_http_version 1.1;
			# webscoekt超时时间,如果没有做心跳之类的动作需要配置
			proxy_read_timeout 700s;
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";
			
			proxy_set_header Origin "";
			proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			# 在多级代理的情况下,记录每次代理之前的客户端真实ip
			proxy_set_header HTTP_X_FORWARDED_FOR $remote_addr;
			# 指定修改被代理服务器返回的响应头中的location头域跟refresh头域数值
			proxy_redirect default;
			proxy_set_header X-NginX-Proxy true;
			break;
		}
		
		#socket访问代理
		location ^~ /socket.io/ {
			proxy_pass http://192.168.1.178:9901;
			
			# WebScoket Support
			proxy_http_version 1.1;
			proxy_read_timeout 700s; # webscoekt超时时间,如果没有做心跳之类的动作需要配置
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";
			
			proxy_set_header Origin "";
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header Host $host;
			proxy_set_header X-NginX-Proxy true;
			break;
		}
    }

2、工具类

package com.xxx;

import com.corundumstudio.socketio.SocketIOClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.web.socket.WebSocketSession;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

public class SocketIpUtil {

    /**
     * 获取nginx代理后webSocket的真实IP
     *
     * @param session
     * @return
     */
    public static String getWebSocketIp(WebSocketSession session) {
        String ip = "";
        //获取Nginx代理后的真实ip地址
        List<String> list = new ArrayList<>();
        HttpHeaders handshakeHeaders = session.getHandshakeHeaders();
        if (handshakeHeaders.containsKey("x-real-ip")) {
            list = handshakeHeaders.get("x-real-ip");
        } else if (handshakeHeaders.containsKey("x-forwarded-for")) {
            list = handshakeHeaders.get("x-forwarded-for");
        } else if (handshakeHeaders.containsKey("http_x_forwarded_for")) {
            list = handshakeHeaders.get("http_x_forwarded_for");
        }
        if (list != null && list.size() > 0) {
            ip = list.get(0);
        } else {
            //如果未经过Nginx代理
            InetSocketAddress remoteAddress = session.getRemoteAddress();
            if (remoteAddress != null) {
                ip = remoteAddress.getAddress().getHostAddress();
            }
        }
        return ip;
    }

    /**
     * 获取nginx代理后socket的真实IP
     *
     * @param client
     * @return
     */
    public static String getSocketIp(SocketIOClient client) {
        //获取Nginx代理后的真实ip地址
        String ip = client.getHandshakeData().getHttpHeaders().get("X-Real-IP");
        if (StringUtils.isBlank(ip)) {
            ip = client.getHandshakeData().getHttpHeaders().get("X-Forwarded-For");
        }
        if (StringUtils.isBlank(ip)) {
            //如果未经过Nginx代理
            ip = client.getHandshakeData().getAddress().getAddress().getHostAddress();
        }
        return ip;
    }
}

你可能感兴趣的:(Java工具类,nginx,websocket,java)