Liana
Liana

Reputation: 100

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();
});

Upvotes: 7

Views: 3749

Answers (1)

YouneL
YouneL

Reputation: 8361

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;

Upvotes: 8

Related Questions