Workerman 用了WSS无法用$_SERVER['REMOTE_ADDR']取得用户IP

0

请问站长和群友们有没解决方案?

已邀请:

walkor

赞同来自:

因为用了nginx做了wss代理,所以 $_SERVER 得到的ip值实际上是nginx服务器的ip,并不是客户端的实际ip,如果是nginx代理在本机则$_SERVER得到的可能是127.0.0.1。


设置nginx代理http header
解决方法是在nginx代理上设置下header,加个proxy_set_header X-Real-IP $remote_addr;,将真正的ip地址通过http header的方式传递过来。类似下面的配置


    server {
listen 4431;

ssl on;
ssl_certificate /etc/nginx/conf.d/ssl/laychat/laychat.pem;
ssl_certificate_key /etc/nginx/conf.d/ssl/laychat/laychat.key;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:50m;
ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

location /
{
proxy_pass http://127.0.0.1:7272;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $remote_addr;
}
}

workerman读取http header
在start_gateway.php中Worker::runAll()前加上下面的代码


    $gateway->onConnect = function($connection)
{
$connection->onWebSocketConnect = function($connection , $http_header)
{
$_SESSION = $_SERVER;
};
};

Events.php
Events.php中就可以通过$_SESSION来得到客户端的实际ip地址了

要回复问题请先登录注册