我尝试了以下解决方案:
app.enable('trust proxy');
...
console.log(req.ip); // gives ::ffff:127.0.0.1
console.log(req.ips); // gives []
console.log(req.header('x-forwarded-for')); // gives undefined
console.log(req.connection.remoteAddress); // gives ::ffff:127.0.0.1
还有别的办法吗?
这是更大问题的征兆吗?
最佳答案
如果你想使用 X-Forwarded-For header ,那么你需要先用 nginx 设置它:
proxy_set_header X-Forwarded-For $remote_addr;
查看文档:
关于node.js - 无法打印nginx后面的expressjs服务器上的请求的IP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43213370/