- Newest
- Most votes
- Most comments
This feature just dropped a few days ago. No need for extra services to do this. This can be handled right in the ALB.
answered a year ago
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; }
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
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:
-
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.
-
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.
-
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.
-
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:
- Implement the solution as close to the edge as possible (e.g., using CloudFront Functions) to reduce latency.
- Ensure your solution is scalable and doesn't introduce significant processing overhead.
- Monitor and log the path rewrites to track any potential issues or unexpected behaviors.
- Make sure your application can handle both the original and modified paths gracefully during the transition period.
- 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
Relevant content
asked 5 months ago
- AWS OFFICIALUpdated 3 months ago
