nginx how to get the request client ipaddress

PMat picture PMat · Aug 24, 2017 · Viewed 10.6k times · Source

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 ...."

Answer

Akber Choudhry picture Akber Choudhry · Aug 24, 2017

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