简体   繁体   English

在socket.io中使用代理时没有获取远程地址

[英]Not getting remote address while using proxy in socket.io

In my socket.io code, 在我的socket.io代码中,

socket.sockets.on('connection', function(client){ 
    var ip = client.handshake.address.address;
    ..
}

ip always returns 127.0.0.1 and this is because the server sits behind a proxy. ip总是返回127.0.0.1,这是因为服务器位于代理后面。 How do I get remote address properly ? 如何正确获取远程地址?

Edit: I am using http-proxy 编辑:我正在使用http-proxy

yes, this is working for me. 是的,这对我有用。

client.handshake.headers['x-forwarded-for'] || client.handshake.address.address;

I am properly getting the remote IP address and not 127.0.0.1 我正在获取远程IP地址而不是127.0.0.1

在版本> 1.0中,语法类似:

socket.handshake.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;

For people who may come from Google, using NGINX: none of these worked for me, I followed the answer in this other question and modified my proxy_pass , to create a new header that node could then use. 对于可能来自Google的人,使用NGINX:这些都不适用于我,我在其他问题中按照答案修改了我的proxy_pass ,以创建一个节点可以使用的新标题。 Don't forget to call sudo systemctl restart nginx after. 不要忘记之后调用sudo systemctl restart nginx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM