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

时间:2013-12-08 21:33:23

标签: php nginx proxy websocket ratchet

我正在设置一个带有棘轮腹板的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_addr)进行IP过滤。因为我必须使用这个代理解决方案,所以当使用函数stream_socket_get_name时,我将始终将ip 127.0.0.1作为远程ip。

就我调查此问题而言,棘轮启动了一个stream_socket_server,然后开始侦听输入连接。 这些连接已经是流而不是http请求所以我无法使用$ _SERVER等。

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

此致

马库斯

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我遇到了同样的问题,但更复杂,因为我在负载均衡器前面有2个代理服务器,然后是另一个代理服务器,然后是另一个用于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);

Makre肯定你传递了一个标题:X-Real-IP,X-Forwarded-For in NGINX。

同时添加CloudFlare IP&#39>:

# 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;

我真的希望这会帮助其他有需要的人,如果你认为有改进的余地,请提出建议!

干杯!