req.ip返回127.0.0.1
我使用nginx为反向代理运行端口3000上的快速服务器。
req.ip总是返回127.0.0.1,req.ips返回一个空数组
app.enable('trust proxy');
在启用/不启用信任代理时,x-forwarded-for不起作用:
var ip_addr = req.headers['X-FORWARDED-FOR'] || req.connection.remoteAddress;
nginx配置:
server { listen 80; server_name localhost; access_log /var/log/nginx/dev_localhost.log; location / { proxy_pass http://localhost:3000; 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; } }
我如何获取请求客户端的IP地址?
您需要将适当的X-Forwarded-For
标头传递给上游。 将这些行添加到上游配置中:
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme;链接地址: http://www.djcxy.com/p/32545.html