- Newest
- Most votes
- Most comments
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.
Relevant content
- asked 3 years ago
- asked 2 months ago
- asked 2 years ago
Thank you! Would Option 1 only work with REST APIs? I am using HTTP API and I can't find request parameter mapping.