請(qǐng)問站長和群友們有沒解決方案?
因?yàn)橛昧薾ginx做了wss代理,所以 $_SERVER 得到的ip值實(shí)際上是nginx服務(wù)器的ip,并不是客戶端的實(shí)際ip,如果是nginx代理在本機(jī)則$_SERVER得到的可能是127.0.0.1。
設(shè)置nginx代理http header
解決方法是在nginx代理上設(shè)置下header,加個(gè)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來得到客戶端的實(shí)際ip地址了