While the answer from @alessioalex works, there's another way as stated in the Express behind proxies section of Express - guide.
app.set('trust proxy', true)
to your express initialization code.req.ip
or req.ips
in the usual way (as if there isn't a reverse proxy)Optional reading:
req.ip
or req.ips
. req.connection.remoteAddress
does't work with this solution.'trust proxy'
are available if you need something more sophisticated than trusting everything passed through in x-forwarded-for
header (for example, when your proxy doesn't remove preexisting x-forwarded-for header from untrusted sources). See the linked guide for more details.x-forwarded-for
header, there are two possibilities.
proxy_set_header X-Forwarded-For $remote_addr;
to your configuration.