13

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

user644745
  • 5,673
  • 9
  • 54
  • 80

3 Answers3

28

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

user644745
  • 5,673
  • 9
  • 54
  • 80
7

In version > 1.0, the syntax is similar:

socket.handshake.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
Jack Guy
  • 8,346
  • 8
  • 55
  • 86
0

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. Don't forget to call sudo systemctl restart nginx after.

Jericho
  • 115
  • 1
  • 2
  • 11