Php 如何在Ratchet websocket中获取nginx密钥X-Real-IP的代理值

Php 如何在Ratchet websocket中获取nginx密钥X-Real-IP的代理值,php,nginx,proxy,websocket,ratchet,Php,Nginx,Proxy,Websocket,Ratchet,我正在使用ratchet websocket设置websocket应用程序。我的设置如下:我(必须)使用nginx服务器作为反向代理,将请求转发到我的websocket棘轮服务器: location /websocket { proxy_pass http://websocket; proxy_http_version 1.1; proxy_set_header X-Real-IP $remote_addr; proxy_s

我正在使用ratchet websocket设置websocket应用程序。我的设置如下:我(必须)使用nginx服务器作为反向代理,将请求转发到我的websocket棘轮服务器:

    location /websocket {
        proxy_pass http://websocket;
        proxy_http_version 1.1;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_redirect off;
    }
在我的websocket应用程序中,我需要获得X-Real-IP的值($remote\u addr),用于IP过滤。因为我必须使用这个代理解决方案,所以在使用函数stream\u socket\u get\u name时,我将始终获得ip 127.0.0.1作为远程ip

就我所调查的这个问题而言,ratchet启动了一个stream_socket_服务器,然后开始监听输入连接。 这些连接已经是流,而不是http请求,所以我不能使用像$\u服务器之类的东西

有人知道如何检索这个值吗

问候


Marcus

在方法上的
中传递的每个ConnectionInterface对象都包含一个Guzzle RequestInterface对象,其中包含来自初始请求的HTTP头:

$conn->WebSocket->request->getHeader('X-Real-IP');

我也有同样的问题,但更复杂的是,我在负载均衡器前面有两个代理服务器,然后是另一个代理服务器,然后是另一个用于websocket的代理服务器。。。我花了一些时间来正确地传递标题,但是当您代理WebSocket服务器时,事情会变得有趣

我将分享我在Ratchet服务器应用程序中获取访问者真实IP的功能,即使在CloudFlare后面:

/**
     * Get the real visitor IP
     * @author nboros
     * @param Ratchet WebSocket $conn
     * @return string $ip
     */
    function GetRealUserIp($conn) {

        $HTTP_X_REAL_IP         = $conn->WebSocket->request->getHeader('X-Real-IP');
        $HTTP_X_FORWARDED_FOR   = $conn->WebSocket->request->getHeader('X-Forwarded-For');
        $HTTP_CF_CONNECTING_IP  = $conn->WebSocket->request->getHeader('CF_CONNECTING_IP');
        $REMOTE_ADDR            = $conn->remoteAddress;

        if(filter_var($HTTP_X_REAL_IP, FILTER_VALIDATE_IP))
        {
            $ip = $HTTP_X_REAL_IP;
        } 
        elseif(filter_var($HTTP_X_FORWARDED_FOR, FILTER_VALIDATE_IP))
        {
            $ip = $HTTP_X_FORWARDED_FOR;
        } 
        elseif(filter_var($HTTP_CF_CONNECTING_IP, FILTER_VALIDATE_IP))
        {
            $ip = $HTTP_CF_CONNECTING_IP;
        } 
        else
        {
            $ip = $REMOTE_ADDR;
        }

        return $ip;
    }
我通常在“onOpen()”回调中调用函数:

$this->GetRealUserIp($conn);
确保您正在传递其中一个头:NGINX中的X-Real-IP、X-Forwarded-For

还添加了CloudFlare IP:

# CloudFlare IPs
# List from: https://www.cloudflare.com/ips-v4
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 104.16.0.0/12;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 131.0.72.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 199.27.128.0/21;

# List from: https://www.cloudflare.com/ips-v6
set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2405:8100::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2c0f:f248::/32;
set_real_ip_from 2a06:98c0::/29;

real_ip_header X-Real-IP;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
并传递标题:

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
我真的希望这将有助于其他需要帮助的人,如果你认为有改进的余地,请提出建议

干杯