node.js - 无法打印nginx后面的expressjs服务器上的请求的IP?

标签 node.js express nginx

我尝试了以下解决方案:

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/

相关文章:

javascript - 插入后无法返回正确的响应

node.js - 如何读取 Handlebars 中对象数组的属性?

ssl - Nginx SSL 证书

nginx - 使用带有Google登录插件的Ingress NGINX Controller在GKE中公开Jenkins

android - 在 iOS/Android 中运行 node.js

javascript - Firebase 配置的哪些部分位于 Nodejs Web 应用程序的服务器端和客户端

node.js - 由于大量请求,pm2 挂起并且无法重新启动进程

node.js - 使用 multer 和 upload.array 上传图像时 req.file 未定义

nginx - 如何在调用 on_publish 后更改 nginx rtmp 流名称

node.js - 限制 Node.js 中的异步调用