繁体   English   中英

获取远程客户端IP地址NodeJS,ExpressJs

[英]Get remote client ip address NodeJS, ExpressJs

我正在尝试使用Express js获取客户端的IP地址,代码如下:

var ip;
if (req.headers['x-forwarded-for']) {
    ip = req.headers['x-forwarded-for'].split(",")[0];
} else if (req.connection && req.connection.remoteAddress) {
    ip = req.connection.remoteAddress;
} else {
    ip = req.ip;
}
ip = (ip.length < 15 ? ip : (ip.substr(0, 7) === '::ffff:' ? ip.substr(7) : undefined));


console.log('ip address',ip);

但是,每当我获取本地ip地址而不是公共ip地址时 ,如何获取公共IP地址而不是本地ip地址? 有人可以帮我吗?

提前致谢。

检查x-forwarded-for标头的最后一个IP。

例如,在aws上:

列表中的最后一个IP地址是客户端的IP地址http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/x-forwarded-headers.html#x-forwarded-for

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM