Path-Based Routing with Application Load Balancer (AWS ALB): Efficiently Directing Traffic Based on URL Paths

Home » AWS » Path-Based Routing with Application Load Balancer (AWS ALB): Efficiently Directing Traffic Based on URL Paths

Path-Based Routing with Application Load Balancer (AWS ALB): Efficiently Directing Traffic Based on URL Paths

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.

Path-Based Routing with Application Load Balancer (AWS ALB): Efficiently Directing Traffic Based on URL Paths

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.
Tutorials dojo strip

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

  1. 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.

        Path-Based Routing with Application Load Balancer (AWS ALB): Efficiently Directing Traffic Based on URL Paths

  2. 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.

        Path-Based Routing with Application Load Balancer (AWS ALB): Efficiently Directing Traffic Based on URL Paths

  3. 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>
  4. 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”).

        Path-Based Routing with Application Load Balancer (AWS ALB): Efficiently Directing Traffic Based on URL Paths

  5. 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).
  6. Define Listener Rules Based on Path Patterns:

      • Go to the Listener details pane.
      • Change Routing actions to “Return fixed response.”
        Path-Based Routing with Application Load Balancer (AWS ALB): Efficiently Directing Traffic Based on URL Paths
      • Add rules:
        • For requests to “/demo1.html,” forward to tg1.
        • For requests to “/demo2.html,” forward to tg2.

          Path-Based Routing with Application Load Balancer (AWS ALB): Efficiently Directing Traffic Based on URL Paths

  7. 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.

        Path-Based Routing with Application Load Balancer (AWS ALB): Efficiently Directing Traffic Based on URL Paths Path-Based Routing with Application Load Balancer (AWS ALB): Efficiently Directing Traffic Based on URL Paths

Thats it! You’ve successfully set up path-based routing with an ALB, directing requests to different instances based on URL paths.

Tutorials Dojo portal

Founder Spotlight: Jon Bonso

jon bonso

Enroll Now – Our Google Cloud Certification Exam Reviewers

Tutorials Dojo Exam Study Guide eBooks

tutorials dojo study guide eBook

FREE AWS Exam Readiness Digital Courses

Subscribe to our YouTube Channel

Tutorials Dojo YouTube Channel

FREE AWS, Azure, GCP Practice Test Samplers

Recent Posts

Written by: Nestor Mayagma Jr.

Nestor is a web developer and member of the AWS Community Builder. He continuously strives to expand his knowledge and expertise in AWS to foster personal and professional growth. He also shares his insights with the community through numerous AWS blogs, highlighting his commitment to Cloud Computing technology. In his leisure time, he indulges in playing FPS and other online games.

AWS, Azure, and GCP Certifications are consistently among the top-paying IT certifications in the world, considering that most companies have now shifted to the cloud. Earn over $150,000 per year with an AWS, Azure, or GCP certification!

Follow us on LinkedIn, YouTube, Facebook, or join our Slack study group. More importantly, answer as many practice exams as you can to help increase your chances of passing your certification exams on your first try!

View Our AWS, Azure, and GCP Exam Reviewers Check out our FREE courses

Our Community

~98%
passing rate
Around 95-98% of our students pass the AWS Certification exams after training with our courses.
200k+
students
Over 200k enrollees choose Tutorials Dojo in preparing for their AWS Certification exams.
~4.8
ratings
Our courses are highly rated by our enrollees from all over the world.

What our students say about us?