Dynamic Remo
Dynamic Remo

Reputation: 531

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 ;-)

Upvotes: 2

Views: 3454

Answers (2)

Dynamic Remo
Dynamic Remo

Reputation: 531

I would like to say thank you to @Marcos Casagrande for his answer. Although i had to work around for additional 3hours after his answer but will accept his lead as an answer to my question.

Here's the Nginx Conf which works for me, placed at the path /etc/nginx/conf.d/abcxyz.com.conf.

server {
    listen 80;
    listen [::]:80;
    server_name abcxyz.com;

    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    location / {
        proxy_pass "http://localhost:1234/";
    }
}

No other Nginx Conf required, as of my previous implementation i was having another conf file at the /etc/nginx/sites-available/default directory which was conflicting as a Duplicate Server Name. Lastly, i would like to add that use Nginx Testing command before resetting the changes. Which was a huge help. Command: nginx -t.

And in Node.js, this is the most appropriate way to access Client's IP Address.

var getClientAddress = (req.headers['x-forwarded-for'] || '').split(',')[0] 
                       || req.connection.remoteAddress;
console.log(getClientAddress);

Cheers ;-)

Upvotes: 0

Marcos Casagrande
Marcos Casagrande

Reputation: 40434

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;

Upvotes: 1

Related Questions