What to do when nginx swallows the client’s IP address?

const geoip = require('geoip-lite');  // ...  const clientIpAddress = req.connection.remoteAddress;  const geoLocation = clientIpAddress
&& geoip.lookup(clientIpAddress)
|| {};
server {
server_name api.example.com;
listen 443 ssl http2;
listen [::]:443 ssl http2 ipv6only=on;
# ...ssl certificates here... # Pass all requests to our local Node.js server
location / {
proxy_pass http://localhost:8200;
}
}

The solution

    # Pass all requests to our local Node.js server
location / {
proxy_pass http://localhost:8200;
# Pass the original IP address in an http header
proxy_set_header X-Forwarded-For $remote_addr;
}
  const clientIpAddress = req.headers['x-forwarded-for']
|| req.connection.remoteAddress;

Caveat

  const clientIpAddress = config.trustXForwardedFor
? req.headers['x-forwarded-for'])
: req.connection.remoteAddress;

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store