Not getting remote address while using proxy in so

2019-02-03 12:25发布

In my socket.io code,

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. How do I get remote address properly ?

Edit: I am using http-proxy

2条回答
爷、活的狠高调
2楼-- · 2019-02-03 12:54

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

查看更多
Emotional °昔
3楼-- · 2019-02-03 12:58

In version > 1.0, the syntax is similar:

socket.handshake.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
查看更多
登录 后发表回答