Using Application Load Balancers to Handle Multiple Domain Redirects

DLT Labs
DLT Labs
Nov 30, 2020 · 7 min read
Using Application Load Balancers to Handle Multiple Domain Redirects
Using Application Load Balancers to Handle Multiple Domain Redirects

I hope everyone has been following safety measures and staying inside to be healthy. The time bygone and the present have been unfamiliar in long stretches, forcing us all to find ways to peck our mind in different areas.

Since you are here, let me share a problem where I applied a similar idea.

In my time helping develop high-performance infrastructure and services at DLT Labs™, I’ve learned a lot about the role of load balancers while making them scalable.

One of the cloud providers we work with is Amazon Web Services, and today I thought I’d talk a little about what they call an Application Load Balancer (ALB), and what is needed to set one up.

Amidst all ongoing activities, one day my team witnessed a bottleneck caused by a requirement for multiple domains to be redirected to a single domain. On looking out for possible solutions, I came across a few. Naturally, as solutions go, they presented certain limitations too.

Magically, an Application Load Balancer can help provide a one-stop solution to clearly sweep-off this problem. Thereafter, you can go and explore more to take advantage of its different use cases in various areas. Now, let’s get familiar with the problem, related approach, and its solution.

#The problem

For example, let's say there are three domains: “testprac1.example.com”, “testprac2.example.com”, and “testprac3.example.com” and all these domains are required to serve HTTP/HTTPS requests.

Requests of any either nature — HTTP or HTTPS for each of the domains above, must be involuted into the same request — i.e., a request to “testprac2.example.com” should be redirected to “testprac1.example.com”. Likewise, a request to “testprac3.example.com” should be redirected to “testprac1.example.com”.

In case you are hosting static data, Amazon S3 and Amazon Route53 would come into the picture, or you can create several domains for each record to be served.

This isn’t the simplest solution, as it isn’t what we call a feels-right-sort-of-solution!

Undoubtedly, I too felt the same, and ultimately found a gem of a source. What I found, I am going to put all of that for you in the following solution.

The solution

This will allow us to dictate a set of rules. These rules can make it either redirect or forward the incoming request(s) to the corresponding destination(s) using a mapping form of mechanism.

We will use ALB which provides the capability to redirect requests from one domain to the newer domain that we intend to serve.

>> Finding the ALB option in AWS

Image for post
Image for post
  • In EC2, using the service navigation pane, find Load Balancing, and select Load Balancers.
Image for post
Image for post
  • Click on Create Load Balancer as displayed in the picture, following which a passage will be opened with several steps to describe and create a Load Balancer.
Image for post
Image for post
  • Select Application Load Balancer here
Image for post
Image for post

Note — For the ease of follow-up, further detail of setup, configurations, etc have been described via the next set of steps provided.

>> Creating the Application Load Balancer

1. Building the Application Load Balancer requires some basic information

  1. Internet-facing/internal*
  2. Ipv4 address type

*Note: For the internet-facing scheme, public subnet(s) would be used, and for internal LB, private subnet(s) would be used.

Image for post
Image for post

Choose the listeners here, followed by selecting the Availability Zones and add-on services that can be opted into as per your choice.

Image for post
Image for post

*Note: For the internet-facing scheme, public subnet(s) would be used, and for internal LB, private subnet(s) would be used.

Click Next on the console.

2. Select a certificate and the relevant security policy

Image for post
Image for post

Click Next on the console.

3. Assign a security group

Image for post
Image for post

Click Next on the console

4. Select optimal health check settings also for your target

Target groups leverage added ports, protocol in the relevant VPC with the specified instance.

Click Next on the console

5. Register your target instance

Click Next on the console

6. Verify your details

Click on Create.

We have created the load balancer with the steps taken so far, Superb!

>> Fine-tuning our load balancer to handle multiple domain redirects

*Note: Assuming you have chosen HTTP protocol while creating ALB, the steps will incorporate the HTTP listener.

1. Setting up the Listener

Image for post
Image for post

A tabular format of listeners contains details in multiple columns. Here you will see the ‘Rules’ column.

2. Set rules for HTTP listener

Image for post
Image for post

For the HTTP listener, click View/edit rules. Initially, it should look like this:

Image for post
Image for post

Please note — Rules use the IF-THEN construct, which corresponds to Conditions-Actions

Now, let’s begin adding conditions and actions.

3. Configure Listener, HTTP, View/edit & Insert rules

Image for post
Image for post

Go to the ‘Add Rule’ button and click on ‘Insert Rule’.

In the IF column, click on the Add condition then select Host Header. You should see this:

Image for post
Image for post

Set value for Host header to “testprac2.example.com”, and click the blue tick.

In the IF column, click again on Add condition, then select Path. Set the path to ‘*’, like so:

Image for post
Image for post

Note — you can customize the Path according to the use-case.

Now, for the Action column, select Redirect to.

Provide your choice here for protocol and port number. In the drop-down menu, select ‘Custom host, path, query(optional)’ and provide the new domain and path as shown below with the code ‘301-permanently moved’.

Image for post
Image for post

It should look like this once all the correct details are in place:

Image for post
Image for post

To better understand what we just did, this last step redirects an HTTP request for “testprac2.example.com” to “testprac1.example.com” in the form of an HTTPS request.

For the other domains, we can repeat the steps in point 3, with relevant Host, Headers, etc.

It should finally look like this:

Image for post
Image for post

So far, the redirection achieved is for HTTP port 80. Similarly, you can add listeners using HTTP/HTTPS. All you need to do is re-iterate through step 1 to step 3 in this section, with the relevant details.

Good work! We have completed the setup of the Application Load Balancer, and your motive has been achieved to serve the use case — to handle multiple domain redirects. You can customize the ALB further, to your taste and requirement.

Keep learning and sharing!

Amazon, Amazon Web Services, AWS, Amazon S3, Amazon Route 53, and Amazon EC2 are trademarks of Amazon.com, Inc. or its affiliates in the United States and/or other countries. DLT Labs is a trademark of DLT Global, Inc.

Author — Aman Gupta, DLT Labs

About the Author: Aman is part of the core DevOps & Infrastructure team at DLT Labs and is skilled in AWS, C++, DS-A, and open-source tools. He loves playing Badminton and enjoys e-gaming.

The Startup

Medium's largest active publication, followed by +754K people. Follow to join our community.

DLT Labs

Written by

DLT Labs

DLT Labs is a global leader in Distributed Ledger Technology and Enterprise Products. To know more, head over to: https://www.dltlabs.com/

The Startup

Medium's largest active publication, followed by +754K people. Follow to join our community.

DLT Labs

Written by

DLT Labs

DLT Labs is a global leader in Distributed Ledger Technology and Enterprise Products. To know more, head over to: https://www.dltlabs.com/

The Startup

Medium's largest active publication, followed by +754K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store