大网数据广告活动
服务器运维问答

How to automatically update nginx upstream server list when aws ec2 hostname changes or increases?

· 52CE 我爱测

I want to setup autoscaling in AWS. I don't want to use Elastic Load Balancer. Autoscalling in Amazon creates EC2 instances seamlessly during demand spikes to maintain performance, and decreases automatically during demand lulls …

问题原文

原帖链接 · Luis Lobo Borobia · CC BY-SA 3.0

I want to setup autoscaling in AWS. I don't want to use Elastic Load Balancer.

Autoscalling in Amazon creates EC2 instances seamlessly during demand spikes to maintain performance, and decreases automatically during demand lulls to minimize costs.

Since this EC2 instances are created automatically, their host names are unknown to NGINX.

I know and already have upstream setup in nginx to 10 EC2 instances.

I want to be able to add/update/delete automatically server names to my upstream nginx configuration, when autoscaling adds/updates/deletes EC2 instances.

已采纳解答

原帖链接 · Upendra · CC BY-SA 3.0

This can be achieved by using Amazon SDK ( I am almost done with it, will put it on github ), utilizing the SNS, EC2 and Autoscaling service.

I have followed the below steps to achieve this:

  1. Enable HTTP notification and subscribed my webserver.
  2. Added a lifecycle-hook with heartbeat of 1 min (to wait for 1 min before terminating) to my autoscaling group for terminating server
  3. Created an index file to parse the message to detect what kind of message it is ( i.e Launch or Terminate)
  4. Once type of event is decided i queried EC2 to get the private ip of the instance
  5. In case of Launch wait till header 200 is recieved and then add the ip to nginx config and reload
  6. In case of Terminate remove the IP from config and reload nginx

Please find the script here https://github.com/singhupendra/aws-autoscale

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2013-07-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。