大网数据广告活动
网站运营与故障问答

Getting IP address and location of my own server instead of visiting user's

· 52CE 我爱测

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 hit…

问题原文

原帖链接 · Dynamic Remo · CC BY-SA 3.0

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?

已采纳解答

原帖链接 · Dynamic Remo · CC BY-SA 3.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-04-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。