简体   繁体   中英

AWS ECS dynamic port mapping + nginx + app

I have a typical ECS infrastructure with a single app behind an ALB. I leverage dynamic host mapping for CD process (ECS can deploy a new container on the same host without port collision).

Now I want to add an nginx container in front of it (for SSL from ALB to EC2). The problem is, in nginx config, I have to specify the app endpoint with the port. With the port being assigned dynamically, I cannot hardcode this value into nginx config. How should I deal with this?

I don't think trying to reach this dynamic port makes a lot of sense...

Currently your have only one nginx server running, so you have an application load balancer, that directs incoming traffic on port 80 to an EC2 instance, at the random port corresponding to your web server container.

<ALB domain name>:80 -> <container EC2 instance IP>:<container dynamic port>

But if your service was scaling up, you would have two containers, running on two different ports, possibly on different EC2 instances.

<ALB domain name>:80 -> <container EC2 instance IP>:<dynamic port>
                     -> <container2 EC2 instance IP>:<another dynamic port>

Your ALB would contact in round-robin each of these containers alternatively.

Mapping to one of these containers on its dynamic port directly would be losing the advantage of the load balancer by bypassing it.

So your proxy that adds SSL has to reach the load balancer itself, on its internal domain name (or the one you would have assigned in Route 53), on port 80.

You can use JWilder Nginx Proxy docker container. This allows you to do the dynamic mapping using environmental variables which is configurable in ECS.

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