我有一个nginx和节点的问题,因为当我想获取用户节点的ip时,在我的localhost工作正常(没有使用nginx)但在我的服务器不能正常工作.我正在研究并看到节点no是第一个接收ip的节点,是nginx并且在nginx之后将请求发送到节点.然后节点接收的ip是我的服务器而不是用户的ip.看一下配置服务器nignx:
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;
}
Run Code Online (Sandbox Code Playgroud)
我使用"req.connection.remoteAddress"来了解用户的IP,控制台显示我的服务器的IP.有人知道如何解决这个问题?
感谢:D
----------- 2016年4月20日--------
我可以解决这个问题,在nginx文件设置上使用这一行
proxy_set_header X-Real-IP $remote_addr;
Run Code Online (Sandbox Code Playgroud)
和node.js
req.headers['x-forwarded-for']
Run Code Online (Sandbox Code Playgroud)
Zeb*_*kle 13
您可以使用以下设置配置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;
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用HTTP标头X-Real-IP的req.headers["X-Real-IP"].
proxy_set_header X-Real-IP $remote_addr;没有为我工作
在代理后面运行 Express 应用程序时,必须将应用程序变量信任代理设置为 true。Express 提供了一些其他信任代理值,您可以在其文档中查看这些值,但目前,我们不必介意它们。
\n言归正传,以下是向您的应用程序显示访问者\xe2\x80\x99s IP 地址的步骤:
\napp.set('trust proxy', true)在您的 Express 应用程序中。proxy_set_header X-Forwarded-For $remote_addr您的服务器块的 Nginx 配置。req.header('x-forwarded-for')您现在可以从\n或req.connection.remoteAddress;读取客户端\xe2\x80\x99s IP 地址。就像下面的 Nginx 配置
\n\n\nRun Code Online (Sandbox Code Playgroud)\nlocation / {\n proxy_pass http://localhost:3001;\n proxy_http_version 1.1;\n proxy_set_header Upgrade $http_upgrade;\n proxy_set_header Connection 'upgrade';\n proxy_set_header Host $host;\n proxy_set_header X-Forwarded-For $remote_addr; # this line\n proxy_cache_bypass $http_upgrade; \n }\n
| 归档时间: |
|
| 查看次数: |
6351 次 |
| 最近记录: |