Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx how to get the request client ipaddress

I have ngnix proxying to a nodejs server. I am trying to read the request client ip address/host name in my nodejs, but it's always

::ffff:127.0.0.1

But in my nginx access log, I can see the client ip address printed, not sure why my nodejs server can't get it.

x.x.x.x - - [24/Aug/2017:14:28:01 -0700] "GET ...."
like image 995
PMat Avatar asked Aug 24 '17 21:08

PMat


People also ask

How do I find the IP address of a request object?

Access the socket field of the Express request object, and then look up the remoteAddress property of that field. The remoteAddress will be the address of the client that sent the request. If you are running the app in development, this will be the loopback address: 127.0. 0.1 for IPv4 and ::1 for IPv6.


2 Answers

Add the following to your nginx configuration stanza that proxies to NodeJS:

proxy_set_header X-Real-IP $remote_addr;

Now you can read the header 'X-Real-IP' in NodeJS

like image 106
Akber Choudhry Avatar answered Sep 30 '22 19:09

Akber Choudhry


Use proxy_set_header X-Real-IP $remote_addr; as setting in location block where you are proxying the request.

In the server use the attribute "HTTP_X_REAL_IP"

For Example in python: request.environ.get('HTTP_X_REAL_IP') (or) request.environ.get('HTTP_X_REAL_IP',request.environ.get('REMOTE_ADDR'))

like image 39
SANDEEP MACHIRAJU Avatar answered Sep 30 '22 18:09

SANDEEP MACHIRAJU