Search code examples
node.jsnginxservergeolocation

Getting IP Address and Location of my own server instead of visiting User's


I have a MEAN Stack application deployed on a server and am using Nginx as proxy server to handle the request and redirect to application's particular port.

For example: User accesses the URL http://www.abcxyz.com/ and Nginx hits the actual application URL http://www.abcxyz.com:1234/, as my application is running on port 1234.

Now, for analytics purpose I wrote a short script in node.js which fetches IP and location of the users visiting my website. I can see the externally searched queries but I am getting the IP address and location of my own server. Why is that?

Is that because of Nginx? How can I resolve this and fetch the user's actual location?

Update: I have added an Answer, which works for me. Cheers ;-)


Solution

  • If you're using express:

    Setting a non-false trust proxy value results in three important changes:

    The value of req.hostname is derived from the value set in the X-Forwarded-Host header, which can be set by the client or by the proxy.

    X-Forwarded-Proto can be set by the reverse proxy to tell the app whether it is https or http or even an invalid name. This value is reflected by req.protocol.

    The req.ip and req.ips values are populated with the list of addresses from X-Forwarded-For.

    http://expressjs.com/es/guide/behind-proxies.html

    app.enable('trust proxy');
    app.all("*", (req, res, next) => {
    
        console.log(req.ip, req.ips);
        //Do whatever you want or call next()       
    });
    

    If you're not using express, you can get the IP from the header: x-forwarded-for

    let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
    

    If you're not getting the correct IP, you need to have the correct settings in Nginx:

    http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header

    proxy_set_header X-Forwarded-For $remote_addr;

    or to append the remote users IP to any existing X-Forwarded-For value:

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;