在我的 socket.io 代码中,
socket.sockets.on('connection', function(client){
var ip = client.handshake.address.address;
..
}
ip 总是返回 127.0.0.1,这是因为服务器位于代理后面。如何正确获取远程地址?
编辑:我正在使用 http-proxy
是的,这对我有用。
client.handshake.headers['x-forwarded-for'] || client.handshake.address.address;
我正确获取远程 IP 地址而不是 127.0.0.1
在 > 1.0 版本中,语法类似:
socket.handshake.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
对于可能来自 Google,使用 NGINX 的人:这些都不适合我,我按照另一个问题中的答案并修改了我的proxy_pass
, 以创建一个节点可以使用的新标头。之后别忘了打电话sudo systemctl restart nginx
。