How to Configure Location used for Redirects #465
Unanswered
KentonParton
asked this question in
Q&A
Replies: 3 comments 1 reply
|
Hi @bnusunny, is it possible to achieve the above? Appreciate your help! |
1 reply
|
This is not a Lambda Web Adapter issue. It's generally how FastAPI works behind a proxy or CDN. Here is how to get it working:
from fastapi import FastAPI, Request
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.types import ASGIApp
from urllib.parse import urlparse, urlunparse
class RewriteRedirectMiddleware(BaseHTTPMiddleware):
def __init__(self, app: ASGIApp):
super().__init__(app)
async def dispatch(self, request: Request, call_next):
response = await call_next(request)
if 'Location' in response.headers:
original_location = response.headers['Location']
forwarded_host = request.headers.get('X-Forwarded-Host')
if forwarded_host:
parsed_url = urlparse(original_location)
new_url = urlunparse(parsed_url._replace(netloc=forwarded_host))
response.headers['Location'] = new_url
return response
app = FastAPI()
app.add_middleware(RewriteRedirectMiddleware)
@app.get("/")
async def root():
return {"headers": "Hello world!"}
@app.get("/playground/")
async def playground(request: Request):
return {"headers": request.headers} |
0 replies
|
Lambda Function URL does not support custom domain name. It requires the |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi all,
I have the following setup:
my-application.example.com --> CloudFront --> Lambda (aws-lambda-web-adapter - FastAPI)
The FastAPI application has a route
/playground. When I openmy-application.example.com/playgroundin the browser, it redirects to the lambda URLhttps://xxx.lambda-url.us-east-1.on.aws/playground/instead of the Route53 URL. I suspect this is because CloudFront is forwarding aLocationof the Lambda URL instead ofmy-application.example.com.Is there a way to override this behaviour in aws-lambda-web-adapter or will I need to update
Locationusing Lambda@Edge?Thanks!
All reactions