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

标签 php nginx proxy websocket ratchet

我正在设置一个带有 Ratchet 网络套接字的网络套接字应用程序。我的设置如下:我(必须)使用 nginx 服务器作为反向代理转发请求到我的 websocket Ratchet 服务器:

    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。

据我调查此问题,ratchet 启动一个 stream_socket_server,然后开始监听传入连接。 这些连接已经是流而不是 http 请求,所以我无法使用 $_SERVER 等东西。

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

问候

马库斯

最佳答案

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

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

关于php - 如何在 Ratchet websocket 中获取 nginx-key X-Real-IP 的代理值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20459396/

相关文章:

javascript - 通过 Ajax 在 URL 中传递变量

PHP/MySQL INNER JOIN 将行数增加三倍?

url - 从 Nginx 服务器上的特定 url 中删除 index.php

rest - 如何在 Heroku 上使用 REST API 后端优化静态文件服务

php - laravel file_get_contents ('php://input')不工作

PHP : calculate day difference between stored datetime in mysql and current datetime for token expiry

python - 在 nGINX 上部署 Django 应用程序

c - 我想在nginx模块开发中获取子请求的响应

apache - Exo 平台删除地址端口 8080

python - 简单的 python localhost 代理 - 几乎可以工作