When a reverse proxy forwards /api/v1/items to a FastAPI app unchanged, why does fastapi run --root-path /api/v1 answer 404, and what works instead?
answer
- does the proxy strip or keep
- what the server does to the path
- the prefix arrives twice
- router prefix, docs routes moved
basics
~20 sUvicorn's --root-path prepends the prefix to a path that already carries it, and Starlette strips it only once, so /items/ never matches. Declare the prefix on the routes with an APIRouter prefix, or set FastAPI(root_path=...) instead of the server option.
solid answer
~30 sUvicorn's `--root-path` (which `fastapi run` passes through) sets the ASGI `root_path` **and** prepends it to `path`, because it assumes the proxy removed the prefix. With a proxy that keeps it, `/api/v1/items/` becomes `/api/v1/api/v1/items/`; Starlette strips `root_path` once before matching, `/api/v1/items/` is left, and a route declared as `/items/` gets a 404. Two setups work: put the prefix on the routes with `APIRouter(prefix="/api/v1")` and start without `--root-path`, moving `docs_url` and `openapi_url` under the prefix too; or keep routes at `/items/` and use `FastAPI(root_path="/api/v1")`, which only sets `root_path`, so Starlette strips the prefix once and the docs and `servers` entry come out right.
code
python · 15 linesfrom fastapi import APIRouter, FastAPI
# The proxy forwards /api/v1/... unchanged, so the prefix belongs to the routes.
api_v1 = APIRouter(prefix="/api/v1")
@api_v1.get("/items/")
def list_items():
return [{"id": 1}]
# Move the docs page and schema under the prefix the proxy routes to this app.
app = FastAPI(docs_url="/api/v1/docs", openapi_url="/api/v1/openapi.json")
app.include_router(api_v1)
# Start the server without --root-path.go deeper
Recall the rule: the server's --root-path is for a proxy that removes the prefix; if the app receives the prefix, it has to be handled another way.
Explain the 404: Uvicorn prepends root_path to an already-prefixed path and Starlette strips it only once, so the declared route never matches.
Diagnose a broken migration between proxy styles from logged scope paths, choose router prefix or constructor root_path, and move the docs routes with it.
Standardise one proxy style across services so the start command and route layout follow from it instead of being chosen per team.
## Two proxy styles A reverse proxy that publishes an API under `https://example.com/api/v1` can forward requests in one of two ways: - **Stripping**: the proxy removes `/api/v1`, and the application server receives `/items/`. - **Keeping**: the proxy forwards the path unchanged, and the application server receives `/api/v1/items/`. Proxy configuration itself is out of scope here. What matters is that FastAPI and Uvicorn are configured for the style the proxy actually uses, and that the server's `--root-path` option is written for the stripping style only. ## Why --root-path answers 404 here The ASGI scope carries both `root_path` and `path`, and the ASGI specification has `path` include the `root_path`. Uvicorn's `--root-path` therefore does two things: it sets `root_path`, and it **prepends** that value to the path it received, assuming the proxy removed it. Starlette later removes a leading `root_path` from `path` before matching routes. With a keeping proxy: 1. the proxy sends `/api/v1/items/`; 2. Uvicorn builds `path` as `/api/v1/api/v1/items/`; 3. Starlette strips `root_path` once, leaving `/api/v1/items/`; 4. a route declared as `/items/` does not match, and FastAPI answers 404. This usually appears when a team changes the proxy from stripping to keeping and leaves `--root-path` in the start command. ## What works with a keeping proxy | Routes declared as | Configuration | Result | |---|---|---| | `/items/` | `fastapi run --root-path /api/v1` | 404: the prefix arrives twice | | `/api/v1/items/` via `APIRouter(prefix="/api/v1")` | no root_path | routes match; schema `paths` are the public paths | | `/items/` | `FastAPI(root_path="/api/v1")` | routes match; docs and `servers` use `/api/v1` | | `/items/` | nothing | 404: nothing removes the prefix | **Router prefix.** `APIRouter(prefix="/api/v1")`, or `include_router(router, prefix="/api/v1")`, makes the declared paths the public ones. The schema lists `/api/v1/items/` and needs no `servers` entry. FastAPI's own docs routes are not part of any router, so with defaults they stay at `/docs` and `/openapi.json`, outside what the proxy routes to the app. Move them with `FastAPI(docs_url="/api/v1/docs", openapi_url="/api/v1/openapi.json")`. **Constructor root_path.** `FastAPI(root_path="/api/v1")` only writes `scope["root_path"]` when the app is called; it does not prepend anything to `path`. The path the keeping proxy sent already starts with `/api/v1`, Starlette strips it once, and `/items/` matches. The docs page at `/api/v1/docs` fetches `/api/v1/openapi.json`, and the schema gets a `/api/v1` server. Because of that lenient strip, the same app also answers `/items/` without the prefix when called directly. **Mounting.** Building the app without the prefix and mounting it in an outer app at `/api/v1` also works: the mount sets `root_path` for the inner app. How `app.mount` works is covered with routers and mounted apps. ## Choosing between them - The **router prefix** puts the public path in the code and in the schema's `paths`, so nothing depends on a scope field the server or app sets; the cost is moving the docs routes as well. - The **constructor root_path** keeps routes identical to a stripping deployment, so code can move between proxy styles; the prefix then lives in app configuration, and the app also answers unprefixed paths. - The **server option** is the right tool only for a stripping proxy. ## Switching a running service between styles Proxy changes and app deploys are usually separate releases, so plan the order: 1. Before the proxy stops stripping, make the app accept both forms. `FastAPI(root_path="/api/v1")` with unprefixed routes does this, because Starlette strips the leading prefix when it is present and matches the bare path when it is not. 2. Remove `--root-path` from the start command in the same release, so the server stops prepending. 3. Change the proxy, then check the docs page and one endpoint through the public URL. 4. If you prefer router prefixes long term, move to them in a later release, together with `docs_url` and `openapi_url`. Skipping step 2 is what produces the doubled prefix: the proxy now sends `/api/v1/items/` and the server still adds `/api/v1` in front of it. ## Diagnosing it 1. Log `request.scope["path"]` and `request.scope["root_path"]` from one path operation, or read Uvicorn's access log: a path showing the prefix twice means `--root-path` meets a keeping proxy. 2. Request the server directly at `/items/`: if that answers while the proxy's URL returns 404, the prefix handling is wrong, not the route. 3. Remove `--root-path` and pick one of the working setups above.
- With routes under APIRouter(prefix="/api/v1") behind a keeping proxy, does the schema need a servers entry?No. The schema's `paths` already list `/api/v1/items/`, the public path, so "Try it out" builds correct URLs against the page's origin. Adding a `servers` entry of `/api/v1` would put the prefix in front of those paths a second time.
- Why does FastAPI(root_path="/api/v1") work with a keeping proxy when --root-path does not?The constructor parameter only sets `scope["root_path"]`; it does not touch `path`. The path from a keeping proxy already starts with `/api/v1`, Starlette strips that leading `root_path` once, and `/items/` matches. The server option prepends the prefix to the path as well, so it arrives twice.
- Keeps --root-path after the proxy stops stripping the prefix
- Expects an APIRouter prefix to move the /docs and /openapi.json routes
- Thinks the proxy style makes no difference to the start command
- Adds a /api/v1 servers entry on top of router-prefixed paths