简体   繁体   English

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

[英]Get remote client ip address NodeJS, ExpressJs

I am trying to get client's ip address with Express js, code is as follows: 我正在尝试使用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);

But every time I am getting localhost ip address not Public ip address , so how can I get public ip address instead of localhost ip address? 但是,每当我获取本地ip地址而不是公共ip地址时 ,如何获取公共IP地址而不是本地ip地址? can anyone help me please ? 有人可以帮我吗?

Thanks in advance. 提前致谢。

Check the last IP of the x-forwarded-for header. 检查x-forwarded-for标头的最后一个IP。

For example, on aws : 例如,在aws上:

the last IP address in the list is the IP address of the client http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/x-forwarded-headers.html#x-forwarded-for 列表中的最后一个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