Research fastapi/uvicorn behaviour when proxied by AWS ALB, behind a non-root (/
)
url path - /api
in these examples. ALB doesn't rewrite the url request, so
when "mounting" a FastAPI application under a prefix (for ex. /api
) the application
gets the whole url path (for ex. /api/a/b/c
).
uv sync
uv run pytest
FASTAPI_ROOT_PATH=/api/ uv run uvicorn demo:app
Expected response from all 3 variants:
$ curl http://127.0.0.1:8000/api/
{"message":"Hello World","root_path":"/api/"}
$ curl -s http://127.0.0.1:8000/api/openapi.json | jq '.servers[0].url'
"/api"
- https://fastapi.tiangolo.com/advanced/behind-a-proxy/
- https://fastapi.tiangolo.com/advanced/sub-applications/
uv run uvicorn --root-path=/app/v1 --interface wsgi demo:wsgi_app
uv run uvicorn --root-path=/app/v1 demo:asgi_app
uv run uvicorn --root-path=/app/v1 demo:starlette