Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get client real IP when using nginx server in front of node as proxy

I'n need to get client real IP address in nodeJs, when using nginx proxy server I always get my localhost(127.0.0.1). Can anyone help me to solv this problem? Here is my code

app.set('trust proxy', 'loopback');
app.use(function(req, res, next) {
  app.ipInfo = req.headers['x-forwarded-for'] ||
    req.connection.remoteAddress ||
    req.socket.remoteAddress ||
    req.connection.socket.remoteAddress;
  next();
});
like image 802
Liana Avatar asked Nov 30 '17 11:11

Liana


People also ask

How do I find my Nginx client IP address?

To configure an original client IP address identification, you can use the NGINX module ngx_http_realip_module. This module allows redefining the value of $remote_addr used by the Wallarm node to get a client IP address.

Can I use Nginx as a forward proxy?

By using the nginx forward proxy we can masking the location and IP for gaining access to services. Nginx forward proxy will continuing the request on behalf of the client. At the time when the host server will accept the request then only we can see the IP of the nginx proxy server.

Is Nginx forward or reverse proxy?

Nginx is an open source web server that can also serve as a reverse proxy. Apart from being used to host websites, it's also one of the most widely used reverse proxy and load balancing solutions.


1 Answers

In nginx if you want to pass through the IP address of the remote user to your backend web server you have to set X-Forwarded-For header to this remote ip, Like this:

proxy_set_header X-Forwarded-For $remote_addr;
like image 58
YouneL Avatar answered Nov 15 '22 07:11

YouneL