簡體   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