I'm currently using ECS Fargate and have a setup where one task definition runs two containers: a frontend container that listens on port 2000 and a backend container that listens on port 3000. The frontend uses Nginx to proxy requests from /api to the backend container. My Application Load Balancer (ALB) forwards traffic to the frontend container, and I've set up a Route 53 hosted zone for my domain.
I'd like to separate these into two distinct task definitions—one for the frontend and another for the backend. I want to configure the ALB so it continues forwarding standard traffic to the frontend while routing all requests under the /api path to the backend. How can I achieve this?
3 Answers
While this isn't specific to Fargate, consider using an internal endpoint for your backend, like AWS CloudMap. It can simplify service discovery in a microservices architecture.
To set this up, you'll need to create listener rules on your ALB. Set up two target groups—one for the frontend and another for the backend. In your ALB listener rules, configure it to direct traffic based on the request path. You might not even need Nginx here, as the ALB can handle most basic routing by itself. If you're only using Nginx for serving static content, consider switching to an S3 bucket for that, and use CloudFront in front of it to target your ALB. Here are some helpful links for listener rules and CloudFront integration: [Listener Rules](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-listeners.html#listener-rules), [CloudFront WAF](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-integrations.html#cloudfront-waf), [CloudFront Overview](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Introduction.html).
Just to clarify, my frontend is built with Node.js, and the backend is an API. I'm mainly using Nginx to route requests from mydomain.com/api to the backend container. Ideally, I want to implement path-based routing so everything under /api forwards to the backend container.
Nginx is often used for serving static files in addition to proxying backend requests.