我想从post请求中获取ip。
var ip = req.ip || req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
(req.connection.socket ? req.connection.socket.remoteAddress : null)
res.json({
success: true,
message: 'ip'+ip
});
但它返回一个null
值。我正在服务器上使用(它不是本地主机)
有什么解决办法吗?
所有解决方案均返回null
;
最佳答案
如果它位于 Nginx 等反向代理后面,请尝试使用 ngx_http_core_module 中的嵌入变量。 (至少这就是我多年来所做的)。
Nginx配置文件
....
location / {
proxy_set_header X-Real-IP $remote_addr;
....
Node.js
req.headers["x-real-ip"]
关于javascript - 如何从请求nodejs获取ip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56644999/