简体   繁体   English

为什么req.socket.address()。address显示的IP地址与实际的公共IP地址不同?

[英]Why does req.socket.address().address show a different IP address than my actual public IP address?

I am writing a small application in Node.js which requires me to obtain the IP address. 我正在Node.js中编写一个小应用程序,需要我获取IP地址。 I am doing this by using req.socket.address().address which gives me the output of home -> 127.0.0.1. 我通过使用req.socket.address()。address来执行此操作,它为我提供了home-> 127.0.0.1的输出。 My understanding is this is because its behind some kind of VPN. 我的理解是,这是因为其背后存在某种VPN。

However, in the AWS console that I'm using my public IP is a different value. 但是,在我使用的公共IP的AWS控制台中,它是一个不同的值。 My question is how can AWS determine my IP address and the Node.js req.socket.address().address method gives me the generic IP of localhost? 我的问题是AWS如何确定我的IP地址,而Node.js req.socket.address()。address方法为我提供了本地主机的通用IP?

You should be able to use EC2 Instance Metadata to get your public IP on an EC2 instance. 您应该能够使用EC2实例元数据来获取EC2实例上的公共IP。

Metadata categories can be found from the following endpoint (call it from your EC2 instance): 可以从以下端点找到元数据类别(从EC2实例调用它):

http://169.254.169.254/latest/meta-data/ http://169.254.169.254/latest/meta-data/

Append a category to the end of the previously mentioned URL to get the metadata value. 将类别附加到前面提到的URL的末尾以获取元数据值。 In your case, I think this is what you are looking for: 就您而言,我认为这是您想要的:

http://169.254.169.254/latest/meta-data/public-ipv4 http://169.254.169.254/latest/meta-data/public-ipv4

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

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