How to remove /stage/ part of URL for API Gateway requests forwarded to an Application Load Balancer?

0

I've integrated an HTTP API in API Gateway and configured it for 3 stages (dev, staging, prod). The invoke URL for the API ends with the staging environment.

When I configure API Gateway with an application load balancer the API Gateway sends the URL to the ALB, and it includes the stage. E.g. /staging/this-route... Because I run the HTTP servers that these requests will be forwarded to in separate environments, I don't want to pass on the staging part of the route, I just want to pass along /this-route...

What is the easiest way to do this? I can't find out how to strip the staging URL in the ALB itself. Is modifying the nginx configuration on my HTTP server to map routes the only way to do this? It's annoying to have to introduce 'stage aware' dependencies in this way. I could also build 3 different API Gateway APIs, but this seems to defeat the purpose of the API Gateway's staging environment support.

Note: I had previously been using a simple HTTP URL integration in API gateway and could just specify the URL of my server. But I found once I introduced the ALB I could not get this to work - I had to change to the 'VPC Load Balancer' type of integration.

1回答
0

Solution 1: Modify API Gateway Integration Request Mapping Navigate to API Gateway Console: Select your API and the method (e.g., GET). Integration Request: Go to the Integration Request section. Mapping Templates: In the Mapping Templates section, create a new mapping template for application/json (or the appropriate content type). Add Template: Add a template that strips the stage from the request path:

{
  "stage": "$context.stage",
  "path": "$context.path",
  "stageVariables": {
    "stage": "$context.stage"
  }
}

Rewrite URL Path: Use a Velocity Template Language (VTL) snippet to remove the stage from the path: velocity

#set($context.path = $context.path.remove("/$context.stage"))

Solution 2: ALB URL Rewrite (Advanced) Although complex, you can use AWS WAF or Lambda@Edge in conjunction with CloudFront to rewrite the URL path.

Solution 3: Modify NGINX Configuration (Server-Side) Update nginx.conf: Add location blocks to strip the stage prefixes and proxy the request: nginx

server {
    listen 80;
    server_name example.com;

    location /dev/ {
        rewrite ^/dev/(.*)$ /$1 break;
        proxy_pass http://backend;
    }

    location /staging/ {
        rewrite ^/staging/(.*)$ /$1 break;
        proxy_pass http://backend;
    }

    location /prod/ {
        rewrite ^/prod/(.*)$ /$1 break;
        proxy_pass http://backend;
    }

    location / {
        proxy_pass http://backend;
    }
}

Recommendation The most straightforward and maintainable solution is to handle the path modification in API Gateway using the request mapping template. This keeps your backend servers unaware of the staging environment and leverages API Gateway's powerful mapping capabilities. This method avoids the complexity of ALB URL rewrites and the need for stage-aware configurations in your backend servers.

profile picture
回答済み 1ヶ月前
  • Thank you! Would Option 1 only work with REST APIs? I am using HTTP API and I can't find request parameter mapping.

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ