This is just additional information for this answer.
If you are using nginx
, you would add proxy_set_header X-Real-IP $remote_addr;
to the location block for the site. /etc/nginx/sites-available/www.example.com
for example. Here is a example server block.
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.1.1:3080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
After restarting nginx
, you will be able to access the ip in your node
/express
application routes with req.headers['x-real-ip'] || req.connection.remoteAddress;