Tiago Bértolo
Tiago Bértolo

Reputation: 4333

Unable to print the IP of a request on an expressjs server behind nginx?

I tried the following solution:

app.enable('trust proxy');
...
console.log(req.ip);                        // gives ::ffff:127.0.0.1
console.log(req.ips);                       // gives []
console.log(req.header('x-forwarded-for')); // gives undefined 
console.log(req.connection.remoteAddress);  // gives ::ffff:127.0.0.1

Is there any other way?

Is this a symptom of a bigger problem?

Upvotes: 0

Views: 304

Answers (1)

rsp
rsp

Reputation: 111258

If you want to use the X-Forwarded-For header then you need to set it with nginx first:

proxy_set_header X-Forwarded-For $remote_addr;

See the docs:

Upvotes: 4

Related Questions