在我的socket.io代码中,
socket.sockets.on('connection', function(client){
var ip = client.handshake.address.address;
..
}
我的服务器位于代理后面,因此IP地址始终返回 127.0.0.1。如何正确获取远程地址?
编辑:我正在使用 http-proxy。
在我的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
socket.handshake.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
proxy_pass
,以创建一个新的标头,然后node就可以使用它。别忘了在此之后调用sudo systemctl restart nginx
。