简体   繁体   中英

Node.js - Getting the host IP address while on Amazon EC2

How can I get the IP address (elastic IP) of the current Node.js host/server on Amazon EC2?

Calling req.connection.address() is useless because EC2 uses an elastic IP. In fact, the IP that shows up using the ifconfig command is not the same as the IP which was used to access the server from outside (which is what I need). How can I get the elastic IP automatically?

From within the instance, you can query for metadata about the instance by sending requests to http://169.254.169.254 .

More information can be found here: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html

您可以根据文档使用过滤器ip-address在API中使用DescribeInstances ,以获取实例的公共IP。

阅读有关标头和弹性IP的信息后,我建议headers['REMOTE_ADDR']给出您要查找的地址(除非在负载均衡器后面)。

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