Fastapi: [BUG] Unable to correctly reverse proxy FastAPI

Created on 21 Apr 2020  路  8Comments  路  Source: tiangolo/fastapi

Describe the bug

When using FastAPI behind a proxy that adds path elements like /api/1.0/, there is no way to correctly display the swagger

To Reproduce

  1. Configure a reverse proxy to send /api/1.0/ to your FastAPI server
  2. access swagger for the API
  3. Observe that all your objects are prefixed with /api/1.0

See screenshot

Expected behavior

/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.

Screenshots

image

Environment

  • OS: alpine docker image
  • FastAPI Version 0.54.1
  • Python version 3.7.3

Additional context

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.

answered bug

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

All 8 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zero0nee picture zero0nee  路  3Comments

vnwarrior picture vnwarrior  路  3Comments

danielgtaylor picture danielgtaylor  路  3Comments

updatatoday picture updatatoday  路  3Comments

scheung38 picture scheung38  路  3Comments