2

I'm connecting to my app, which is running on an AWS EC2 instance. I'm trying to get the client IP address but it is showing up as 127.0.0.1

I've tried retrieving it with both req.ip and req.connection.remoteAddress. Is there a way to get the IP address that's not the localhost IP?

jordan
  • 9,570
  • 9
  • 43
  • 78

1 Answers1

7

If you're proxying requests through something like Nginx, then you can configure express to respect the X-Forwarded-For header when getting req.ip:

app.set('trust proxy', 'loopback');

http://expressjs.com/api.html#app.set

Emmett
  • 14,035
  • 12
  • 56
  • 81
  • A variation of this worked for me when building an app at glitch.me. I used `app.set('trust proxy', true)` based on [this documentation](http://expressjs.com/en/api.html#trust.proxy.options.table) The link you provided is redirected to the top of the express API documentation page. I'm sure it's just because the page has changed since you posted this answer. – Vince Mar 19 '18 at 03:03