6

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.example.com/ and Nginx hits the actual application URL http://www.example.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?

4

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

server {
    listen 80;
    listen [::]:80;
    server_name example.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 ;-)

  • In your node.js code you need to ensure the request has been passed from your own server. Otherwise, someone can connect to your app on port 1234 directly and spoof any IP address using x-forwarded-for. – DisgruntledGoat Apr 15 '17 at 13:16
  • To ensure that, do we need to listen like this? let server = app.listen("1234", 127.0.0.1 , calback); right? or how can we do that? – Dynamic Remo Apr 15 '17 at 13:22
1

Because your main application server is behind a proxy server, you will naturally be seeing the IP address of the proxy server, since this is effectively the "client" making the request to your application server.

You need the IP address of the client that makes the request to the proxy server - the originating IP address. The only way to get this information is if the proxy server passes on the IP address that made the request. Proxy servers commonly do this with the X-Forwarded-For HTTP request header.

X-Forwarded-For: <client-ip-address>

However, this header can contain multiple IP addresses (comma separated) if multiple proxy servers have been passed through:

X-Forwarded-For: <client-ip-address>, <previous-proxy-ip>
  • i tried fetching the IP in this manner but no success. I have placed app.enable('trust proxy'); in my server.ts file right after my following code import { serverApi, createTodoApi } from './backend/api'; app.get('/data.json', serverApi); app.use('/api', createTodoApi()); and accessing the variables req.ip in my backend/api.ts file. Where its returning ::ffff:127.0.0.1, which is not a valid ip address to fetch the location out of it. Waiting for your response, Thanks! – Dynamic Remo Apr 13 '17 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.