Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not getting remote address while using proxy in socket.io

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

like image 418
user644745 Avatar asked Jun 25 '12 02:06

user644745


2 Answers

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

like image 150
user644745 Avatar answered Oct 31 '22 20:10

user644745


In version > 1.0, the syntax is similar:

socket.handshake.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
like image 36
Jack Guy Avatar answered Oct 31 '22 20:10

Jack Guy