我有NGINX作为反向代理运行,该代理将所有http和https流量转发到我的node.js应用程序,该应用程序侦听localhost:port
但是我遇到的问题是node.js应用程序将所有传入请求都视为来自:: ffff:127.0.0.1
如何更改NGINX配置,以使真实IP可以通过并转发到node.js应用程序?
server {
listen 80;
listen [::]:80;
listen 443;
listen [::]:443;
root /var/www/example.com/html;
index index.html index.htm;
server_name example.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:myport;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
# Requests for socket.io are passed on to Node on port x
location ~* \.io {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:myport;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
编辑:express.js / node.js应用程序处理req.ip并具有app.enable('trust proxy');在启动时
答案 0 :(得分:1)
Express.js官方网站上有此guide。说明:
app.enable('trust proxy')
in js。proxy_set_header X-Forwarded-For $remote_addr
req.ip
属性中读取客户端IP地址答案 1 :(得分:0)
这可以解决以上NGINX配置问题。
svn co