Skip to content

Reverse proxy and sub-apps

Each request builds its URLs from the ASGI root_path, so setup() takes no prefix parameter.

Mounted sub-apps

Call setup() on the sub-app. It doesn't need to know where it gets mounted:

import fastapi_csp_docs

sub_app = FastAPI(docs_url=None, redoc_url=None)
fastapi_csp_docs.setup(sub_app)

app = FastAPI()
app.mount("/api", sub_app)
URL Response
/api/docs Swagger UI, loading /api/docs/swagger-initializer.js
/api/docs/swagger-initializer.js Script with the schema at /api/openapi.json and the redirect at /api/docs/oauth2-redirect
/api/redoc ReDoc, with the schema at /api/openapi.json and the CSS at /api/redoc/redoc.css

The full app is examples/mounted_subapp_app.py.

Behind a reverse proxy

For a proxy that strips a path prefix, start the server with that prefix, as FastAPI's Behind a Proxy guide describes:

uvicorn main:app --root-path /api

The pages then produce the same URLs as under a mount.

Asset URLs

Asset URLs that start with a single / get the prefix too. See URLs under a prefix.