Skip to content

How to rewrite paths in Amazon Application Load Balancer?

0

Hi guys, I need help with my domain https://x.com/hi/alo/.... I want to remove the /hi segment from incoming requests so that they are processed as https://x.com/alo/... before being forwarded to the target group.

Current Setup: Using AWS CloudFront and an Application Load Balancer (ALB). I want to achieve this without using a Lambda@Edge function. Questions: Is there a way to configure CloudFront or ALB to remove the /hi segment directly? What are the best practices for modifying request paths in this scenario? Thanks for your help!

asked 2 years ago10.6K views

4 Answers
2

This feature just dropped a few days ago. No need for extra services to do this. This can be handled right in the ALB.

https://aws.amazon.com/blogs/networking-and-content-delivery/introducing-url-and-host-header-rewrite-with-aws-application-load-balancers/

answered a year ago

1
Accepted Answer

As the bot said, the ALB doesn't support any custom modifications to requests. In CloudFront, you can rewrite the URL with a CloudFront Function. They are extremely lightweight, only taking microseconds to run, cost significantly less than Lambda@Edge functions, and get executed at CloudFront edge locations, rather than getting forwarded to Regional Edge Cache locations for execution.

The example function below should work when you associate it with your cache behaviour in CloudFront with viewer request as the event type. If you modify the code, note that after processing, CloudFront requires the uri field to start with a forward slash. Otherwise, CloudFront will reject the request. The example considers it by including the trailing slash as the fourth character in the /hi/ string literal it looks for as the prefix and only removing the first three characters with substring, leaving the trailing slash of the prefix as the first character of the final URI.

Notably, the URL https://example.com/hi would not get changed but https://example.com/hi/ or https://example.com/hi/image.jpeg would:

async function handler(event) {
    var request = event.request;
    if (request.uri.startsWith("/hi/")) {
        request.uri = request.uri.substring(3);
    }
    return request;
}

If you also want the plain /hi path to get modified, ways are many, but a simple one is to check for it explicitly:

async function handler(event) {
    var request = event.request;
    if (request.uri.startsWith("/hi/")) {
        request.uri = request.uri.substring(3);
    } else if (request.uri == "/hi") {
        request.uri = "/";
    }
    return request;
}
EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

1

Transforms are now available on ALB.

host-header-rewrite

Rewrites the host header in the request. The transform uses a regular expression to match a pattern in the host header and then replaces it with a replacement string.

url-rewrite

Rewrites the request URL. The transform uses a regular expression to match a pattern in the request URL and then replaces it with a replacement string.

answered a year ago

0

Unfortunately, the Application Load Balancer (ALB) does not have a built-in feature to directly rewrite or remove path segments from incoming requests before forwarding them to the target group. The ALB primarily focuses on routing based on the existing path rather than modifying it.

However, there are a few alternative approaches you could consider:

  1. Use CloudFront Functions: Instead of Lambda@Edge, you can use CloudFront Functions to modify the request path. CloudFront Functions are lightweight and can perform simple path manipulations more efficiently than Lambda@Edge.

  2. Modify your application logic: If possible, adjust your backend application to handle requests with the "/hi" segment and process them as if the segment wasn't there.

  3. Use a reverse proxy: You could set up a reverse proxy (like NGINX) between your ALB and your application servers. This proxy can handle the path rewriting before forwarding requests to your application.

  4. Content-based routing: While this doesn't remove the path segment, you could use the ALB's content-based routing feature to route requests with "/hi" to the same target group as requests without it. This way, your application would receive both types of requests and could handle them accordingly.

Best practices for modifying request paths in this scenario:

  1. Implement the solution as close to the edge as possible (e.g., using CloudFront Functions) to reduce latency.
  2. Ensure your solution is scalable and doesn't introduce significant processing overhead.
  3. Monitor and log the path rewrites to track any potential issues or unexpected behaviors.
  4. Make sure your application can handle both the original and modified paths gracefully during the transition period.
  5. If using a reverse proxy for rewriting, ensure it's highly available and doesn't become a single point of failure.

Remember that while these alternatives can work, they may introduce additional complexity or potential points of failure. Carefully consider the trade-offs and test thoroughly before implementing in a production environment.
Sources
Application Load Balancer | Elastic Load Balancing | Amazon Web Services
Application Load Balancers - Elastic Load Balancing

answered 2 years ago

EXPERT

reviewed 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.