When using FastAPI behind a proxy that adds path elements like /api/1.0/, there is no way to correctly display the swagger
/api/1.0/ to your FastAPI server/api/1.0See screenshot
/api/1.0 should not be part of API documentation.
OpenAPI documents this here
There should be a way to configure Server URL to /api/1.0 like indicated in the examples.

Using Traefik as reverse proxy. Traefik like other proxies, has the ability to either :
Pass the request as-is with /api/1.0/, or strip the part from the URL and put it in the X-Forwarded-Prefix header. Maybe that could be used as the server URL for Swagger.
You can use openapi_prefix (https://fastapi.tiangolo.com/advanced/sub-applications-proxy/) but that's not really viable when the application doesn't know under which prefix it's running from.
Ideally, it should be resolved dynamically based on the X-Forwarded-Prefix. We are using Traefik and having the same problem. We are having a PathPrefix on our route, and using the StripPrefix middleware, which will strip the prefix, and pass it to the X-Forwarded-Prefix header.
That's exactly my situation. Traefik passes all the information but they are not leveraged by fastapi. I switched to Connexion since then
Related to #829. @ybizeul raises a good point that the application's root path should probably be included in a server object in the generated OpenAPI documentation and omitted from the routes in the API.
FastAPI's current behavior (not specifying a servers array) falls back to the specification's default, which is this:
If the servers property is not provided, or is an empty array, the default value would be a Server Object with a url value of /.
So equivalent to:
servers:
- url: '/'
Passing the root_path was addressed in https://github.com/tiangolo/fastapi/pull/1199 :rocket:
Available in FastAPI 0.56.0.
Here are new docs on running FastAPI Behind a Proxy with a Traefik example.
It could be passed to Uvicorn as:
$ uvicorn app:main --root-path=/api/1.0
About reading the X-Forwarded-Prefix, that would be a feature request to Uvicorn, to read it when using --proxy-headers and set the root_path in the ASGI scope with it.
If it is implemented there, it will be automatically supported here.
From what I see in the documentation you point me to, it's not fixed at all and /api/blah is still part of the path for every path
Ah, you are right, I didn't interpret your original issue description correctly.
You expected it to _not_ contain the prefix and still work.
I have to investigate how the servers would interact with the rest.
This was fixed/implemented by @rkbeatss in https://github.com/tiangolo/fastapi/pull/1596. :tada:
Available in FastAPI version 0.59.0. :rocket:
New docs here: https://fastapi.tiangolo.com/advanced/behind-a-proxy/#additional-servers
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.
Most helpful comment
This was fixed/implemented by @rkbeatss in https://github.com/tiangolo/fastapi/pull/1596. :tada:
Available in FastAPI version
0.59.0. :rocket:New docs here: https://fastapi.tiangolo.com/advanced/behind-a-proxy/#additional-servers