简体   繁体   中英

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. I am doing this by using req.socket.address().address which gives me the output of home -> 127.0.0.1. My understanding is this is because its behind some kind of VPN.

However, in the AWS console that I'm using my public IP is a different value. 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?

You should be able to use EC2 Instance Metadata to get your public IP on an EC2 instance.

Metadata categories can be found from the following endpoint (call it from your EC2 instance):

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

Append a category to the end of the previously mentioned URL to get the metadata value. In your case, I think this is what you are looking for:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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