How to Get Visitor’s IP Address in Nginx and Express.js Application
Activating the forwarding of IP addresses requires some configuration on your server. This tutorial shows you how to obtain a visitor’s IP address in 3 simple steps.
1 min readOct 14, 2019
Express App Behind Proxies
When running an Express app behind a proxy, you have to set the application variable trust proxy
to true
. Express offers a few other trust proxy values which you can review in their documentation, but for now, we don’t have to mind them.
Without further ado, these are the steps to reveal a visitor’s IP address to your app:
app.set('trust proxy', true)
in your Express app.- Add
proxy_set_header X-Forwarded-For $remote_addr
in the Nginx configuration for your server block. - You can now read off the client’s IP address from the
req.ip
property.