An Application Load Balancer enables you to set up a listener with rules that direct incoming requests to target groups based on the URL. This capability is unique to Application Load Balancers and is not offered by other load balancer types like Classic Load Balancer, Network Load Balancer, and Gateway Load Balancer. The path pattern rules only apply to the path of the URL and do not consider the URL’s query parameters.
Path-based routing allows you to host multiple microservices behind a single ALB, directing traffic to the appropriate service based on the requested path.
Important Purposes of Path-based Routing in a Web Application Architecture
1. Traffic Segmentation:
-
-
- By directing requests based on URL paths, you can segment traffic to different backend services or applications.
- For example, you might have multiple microservices or versions of your application running behind the ALB. Path-based routing allows you to route specific paths to the appropriate service.
-
2. Versioning and A/B Testing:
-
-
- When deploying new features or changes, you can use path-based routing to gradually roll out updates.
- For instance, you can route a specific path (e.g., “/v2/”) to a newer version of your application while keeping the default path (“/”) pointing to the stable version.
-
3. Content Isolation:
-
-
- Different paths can serve distinct content or functionality.
-
4. SEO and User Experience:
-
-
- Path-based routing helps improve search engine optimization (SEO) by organizing content logically.
- Clear paths make URLs more user-friendly and descriptive, enhancing the overall user experience.
-
5. Multi-Tenancy:
-
-
- In multi-tenant applications, path-based routing allows you to serve different tenants (organizations or users) from the same infrastructure.
- Each tenant can have its own path (e.g., “/tenant1/”, “/tenant2/”) leading to their isolated resources.
-
6. Custom Error Pages:
-
-
- You can configure custom error pages (e.g., 404 Not Found) for specific paths.
- For example, if a user accesses a non-existent path, you can display a tailored error page.
-
In summary, path-based routing provides flexibility, scalability, and efficient resource utilization by directing incoming requests to the appropriate backend based on their paths. It’s a powerful tool for managing complex web applications and ensuring a seamless user experience.
Without further ado, let’s proceed to the implementation guide. This blog will guide you through the steps to set up path-based routing with an Application Load Balancer (ALB) on AWS.
Implementation Guide
-
Launching EC2 Instances:
-
- Launch two EC2 instances (App1 and App2) using Ubuntu Server 24.04 AMI.
- Create a new key pair (e.g., “path-based-routing”) or use an existing one.
- Configure security groups to allow SSH and HTTP traffic.
-
-
Installing NGINX Web Server:
-
- Connect to both App1 and App2 instances via SSH.
- Install the NGINX web server by running the following commands:
sudo apt update sudo apt install nginx
- Verify successful installation by opening the Public IPv4 address in a browser using HTTP.
-
-
Creating a Simple UI:
-
- On both instances, create simple HTML files. Connect to your instance via SSH and run the following commands:
- For App1:
sudo vi /var/www/html/demo1.html
<!DOCTYPE html> <html> <head> <title>Welcome to App1!</title> </head> <body> <h1>Welcome to App1!</h1> </body> </html>
- For the App2:
sudo vi /var/www/html/demo2.html
<!DOCTYPE html> <html> <head> <title>Welcome to App2!</title> </head> <body> <h1>Welcome to App2!</h1> </body> </html>
- For App1:
- On both instances, create simple HTML files. Connect to your instance via SSH and run the following commands:
-
-
Creating Target Groups:
-
- Set up two target groups (tg1 and tg2) with Protocol as HTTP and Port 80.
- Register App1 instances with tg1 (set HealthCheckPath to “/demo1.html”).
- Register App2 instances with tg2 (set HealthCheckPath to “/demo2.html”).
-
-
Creating an Application Load Balancer (ALB):
-
- Create an Internet-facing ALB named “demo” with IPV4 address type.
- Select a VPC and at least two Availability Zones.
- Configure security groups.
- Set up listeners: Protocol HTTP, Port 80, forward to tg1 (no path condition).
-
-
Define Listener Rules Based on Path Patterns:
-
- Go to the Listener details pane.
- Change Routing actions to “Return fixed response.”
- Add rules:
- For requests to “/demo1.html,” forward to tg1.
- For requests to “/demo2.html,” forward to tg2.
-
-
Test Path-Based Routing:
-
- Access your ALB using different paths:
- http://your-alb-dns-name/demo1.html
- http://your-alb-dns-name/demo2.html
- Verify that requests are correctly routed to the respective pages.
- Access your ALB using different paths:
-
Thats it! You’ve successfully set up path-based routing with an ALB, directing requests to different instances based on URL paths.