Express - req.ip returns 127.0.0.1 Express - req.ip returns 127.0.0.1 express express

Express - req.ip returns 127.0.0.1


You need to pass the appropriate X-Forwarded-For header to your upstream. Add these lines to your upstream config:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;


According to the express documentation:

Express behind proxies

When running an Express app behind a proxy, set (by using app.set()) the application variable trust proxy to one of the values listed in the following table.

You can set it to a boolean, ip address, number or a custom function. If you want to just get the client's proxy to your express app's req.ip, you can just set it to true.

app.set('trust proxy',true); app.get("/", (req, res)=>{    console.log(req.ip); });