我正在设置一个带有 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/