How to Redirect Requests Based on the URL with Amazon ELB
Amazon Elastic Load Balancer (ELB) is a service that distributes incoming traffic across multiple targets, such as Amazon EC2 instances, containers, IP addresses, Lambda functions, and virtual appliances. ELB can help you improve the availability, scalability, and performance of your applications.
One of the common use cases for ELB is to redirect requests based on the URL path or host header. For example, you may want to route requests to different versions of your application, such as http://example.com/APPNAME/service
for the current version and http://example.com/APPNAME/v2.2/service
for the old version. Or you may want to redirect HTTP requests to HTTPS for security reasons.
In this blog post, we will show you how to achieve these scenarios with ELB using two different types of load balancers: Application Load Balancer (ALB) and Classic Load Balancer (CLB).
Application Load Balancer
ALB is a Layer 7 load balancer that supports content-based routing. This means that you can create rules that match the URL path or host header of the incoming requests and route them to different target groups. A target group is a set of targets that can handle the requests.
To create an ALB, you need to specify a listener, which is a process that checks for connection requests using the protocol and port that you configure. You can create HTTP or HTTPS listeners for your ALB. If you use HTTPS, you need to deploy at least one SSL server certificate on the listener.
You can also use an HTTPS listener to offload the work of encryption and decryption to your load balancer so that your applications can focus on their business logic. ALB supports HTTP/2 and WebSocket protocols as well.
To redirect requests based on the URL path with ALB, you need to create rules for your listener that specify the conditions and actions. The conditions can be based on the URL path or host header of the requests. The actions can be forward, redirect, or fixed-response.
For example, if you want to route requests to different versions of your application based on the URL path, you can create two target groups: one for the current version and one for the old version. Then you can create a rule for your listener that matches the URL path /APPNAME/v2.2/service
and forwards the requests to the old version target group. You can also create a default rule that forwards all other requests to the current version target group.
If you want to redirect HTTP requests to HTTPS, you can create a rule for your HTTP listener that matches all requests and redirects them to HTTPS using the same host and path. You can also configure the redirect action to use a different port, host, or path if needed.
Classic Load Balancer
CLB is a Layer 4 load balancer that supports TCP and SSL/TLS protocols. It bases its load balancing decisions solely on the TCP connection but ignores the application payload. CLB provides basic application level support for HTTP/HTTPS/SSL by terminating SSL connections and inserting X-Forwarded-* headers.
However, CLB does not support content-based routing or redirect actions. This means that you cannot redirect requests based on the URL path or host header with CLB. You can only forward requests to different target groups based on the protocol and port of the listener.
For example, if you want to route requests to different versions of your application based on the URL path, you cannot do it with CLB. You need to use ALB or another solution.
If you want to redirect HTTP requests to HTTPS with CLB, you cannot do it at the load balancer level either. You need to do it at the application level by checking the X-Forwarded-Proto header and sending a 301 redirect response if it is http
. Alternatively, you can use ALB or Network Load Balancer (NLB) instead of CLB.
NLB is a Layer 4 load balancer that supports TCP and TLS protocols. It passes encrypted traffic through to the targets without decrypting it. This allows you to ensure that the targets decrypt HTTPS traffic instead of the load balancer.
Conclusion
In this blog post, we have shown you how to redirect requests based on the URL with Amazon ELB using two different types of load balancers: Application Load Balancer and Classic Load Balancer. We have also compared their features and limitations.
We hope this post has helped you understand how to use ELB for your applications. If you have any questions or feedback, please leave a comment below.