使用Nginx和Node获取用户IP

19

我在使用nginx和node时遇到了问题,因为当我想使用node获取用户IP地址时,在我的本地主机上可以正常工作(没有使用nginx),但是在服务器上不像应该那样工作。我进行了研究,并发现node并不是第一个接收IP地址的,而是nginx先接收请求,然后将请求发送给node。因此,node接收到的IP地址是我的服务器IP地址,而不是用户的IP地址。请查看nginx的配置:

location / {
        proxy_pass https://fotogena.co:8000;  <-nginx send req to node
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_connect_timeout   1000;
        proxy_send_timeout      1500;
        proxy_read_timeout      2000;
}

我使用 "req.connection.remoteAddress" 来获取用户的 IP 地址,但控制台显示的是我的服务器 IP。有人知道如何解决这个问题吗?

谢谢 :D

-----------2016-04-20--------

我通过在 Nginx 文件设置中添加以下行来解决了这个问题。

proxy_set_header X-Real-IP $remote_addr;

和 node.js

->

and node.js

req.headers['x-forwarded-for']

req.ip 是什么? - Bwaxxlo
你在使用什么作为你的Node Web服务器?Express?还是其他什么? - Kevin B
感谢您发布答案。它今天帮了我很大的忙。 - kakopappa
3个回答

30

您可以通过以下设置配置NGINX以传递客户端的IP地址:

location / {
        proxy_pass https://fotogena.co:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;  # This line.
        proxy_connect_timeout   1000;
        proxy_send_timeout      1500;
        proxy_read_timeout      2000;
}
你可以使用HTTP头X-Real-IP,从req.headers["x-real-ip"]中获取。

当我添加了这一行代码 "proxy_set_header X-Real-IP $remote_addr;" 并重新启动Nginx时,显示了失败。 - SonickSeven
@SonickSeven你能否发布一下你日志的Pastebin链接? - Piper McCorkle
2
@SonickSeven 使用 nginx -t 命令来检查 nginx 配置。它会给你更有用的错误信息。 - Alexey Ten
6
nginx的默认设置都是小写字母:x-real-ip -> req.headers["x-real-ip"]。 - buycanna.io
2
x-real-ip - 需要用小写字母书写 - req.headers ['x-real-ip'] - Nastro
显示剩余2条评论

9

proxy_set_header X-Real-IP $remote_addr; 对我没用

当在代理后运行 Express 应用时,您必须将应用程序变量 trust proxy 设置为 true。Express 提供了一些其他的 trust proxy 值,您可以在它们的文档中查看,但目前我们不需要考虑它们。

不多说了,下面是揭示访问者 IP 地址的步骤:

  1. 在您的 Express 应用中设置 app.set('trust proxy', true)
  2. 在您的服务器块的 Nginx 配置中添加 proxy_set_header X-Forwarded-For $remote_addr
  3. 现在您可以从 req.header('x-forwarded-for')req.connection.remoteAddress 读取客户端的 IP 地址;

像下面这样在 Nginx 配置中:

location /  {
            proxy_pass    http://localhost:3001;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;  # this line
            proxy_cache_bypass $http_upgrade; 
    }

1
这些设置对我很有效。此外,我使用了Express的req.ip来检索IP。 - nthpixel

0
我做了以下几个步骤:
1. 在nginx文件中添加了proxy_set_header X-Real-IP $remote_addr; 2. 添加了app.set('trust proxy', true) 我的Express应用程序(v4.18.2)位于Nginx(v1.18.0 Ubuntu)之后,并且使用了一个上游块。同时,这是一个HTTPS连接。
以下是一些返回值的情况:
- req.ip 返回127.0.0.1 - req.header('x-Real-IP') 返回以192、172或162开头的IPv4地址 - req.headers['x-forwarded-for'] 返回我在https://nordvpn.com/what-is-my-ip/offer-site/验证时得到的真实IP地址
因此,我认为建议的方法有效,但选择正确的方法来获取值仍然有些棘手。 源代码

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接