Under What Conditions can req.connection.remoteAddress be Blank (ExpressJS)

时间:2017-07-17 15:27:45

标签: node.js express

I have an express app running under Node.js v6.9.4, serving both via HTTP and HTTPS.

I've noticed a few times that requests coming in have had blank remoteAddress data.

Under what conditions can a request have no / blank remoteAddress?

2 个答案:

答案 0 :(得分:4)

remote address is blank when behind a proxy

https://github.com/hapijs/hapi/issues/1210

you can use

const ip = req.headers['x-forwarded-for'] || (req.connection && req.connection.remoteAddress) || ''

答案 1 :(得分:0)

代理

后面的表达
  

虽然如果未设置应用程序变量信任代理,应用程序将无法运行,但除非配置了信任代理,否则它将错误地将代理的IP地址注册为客户端IP地址。

获取IP地址的替代解决方案由@ trushar-gupta

提供

您还可以了解有关 Express代理的更多信息  here