tiangolo/fastapi

"Behind a Proxy" changed behaviour in the new version 0.109.0 (in Uvicorn)

tiangolo opened this issue · 13 comments

Discussed in #10948

This is related to a change in Starlette and Uvicorn, making them more formally follow the ASGI spec, Starlette already does since version 0.35.0 (recently), Uvicorn needs an equivalent change, in process on: encode/uvicorn#2213

More details in the Starlette PR: encode/starlette#2400

Originally posted by neugeeug January 12, 2024

First Check

  • I added a very descriptive title here.
  • I used the GitHub search to find a similar question and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

uvicorn rest.main:server --host 0.0.0.0 --port 8080 --reload --root-path /proxy

server = FastAPI()

# separate spec at /v1/docs
server.mount("/v1", app_v1)
# separate spec at /v2/docs
server.mount("/v2", app_v2)

#where app_v1, app_v2 is like below
app_v1 = FastAPI(openapi_tags=tags_metadata, title="Pet store")

Description

When I run the uvicorn with the --root-path option for the above setup, the server is expecting that I include the defined root path in every request e.g:
http://localhost:8080/v2/docs is not working (404)
http://localhost:8080/proxy/v2/docs is working

IN version 0.108.0 It works as described here https://fastapi.tiangolo.com/advanced/behind-a-proxy/

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.109.0

Pydantic Version

2.5.3

Python Version

3.11.5

Additional Context

No response

Just to clear it up, in 0.110.0, the behavior would be the same as 0.108.0?

This was solved in the latest uvicorn: https://github.com/encode/uvicorn/releases/tag/0.26.0

Now using uvicorn works well, but the root_path parameter of the FastAPI object still doesn't work

Thanks @insistence, please create a new discussion question with your use case and an example that I can copy-paste to see what is not working, thanks! ☕

Thanks @insistence, please create a new discussion question with your use case and an example that I can copy-paste to see what is not working, thanks! ☕

Thanks for your quick reply! The new discussion question is #11033

import uvicorn
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles


app = FastAPI(
    root_path='/proxy'
)


@app.get("/app")
def read_main():
    return {"message": "Hello World from main app"}


app.mount("/profile", StaticFiles(directory="static"))


if __name__ == '__main__':
    uvicorn.run(app='app:app')

I already have a directory called static, which contains a file such as test.txt. When I run the code, I can only access it by http://127.0.0.1:8000/proxy/profile/test.txt in a browser. I can't access it by http://127.0.0.1:8000/profile/test.txt in a browser.
But when I run the following code, I can access it by http://127.0.0.1:8000/profile/test.txt in a browser.

import uvicorn
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles


app = FastAPI()


@app.get("/app")
def read_main():
    return {"message": "Hello World from main app"}


app.mount("/profile", StaticFiles(directory="static"))


if __name__ == '__main__':
    uvicorn.run(app='app:app', root_path='/proxy')

I would like to confirm if this is a bug in FastAPI. Can you help me? The discussion question is #11033

@insistence this looks like expected behavior because when you add a root_path it will be added before ALL the paths (including the statically mounted directories).

What confuses me is that the running results of these two pieces of code are inconsistent. This seems to be inconsistent with the description in the official document (https://fastapi.tiangolo.com/advanced/behind-a-proxy/#setting-the-root_path-in-the-fastapi-app). According to the official documentation, the results obtained by setting the root_path parameter to uvicorn or to FastAPI should be consistent.

Ah Indeed!

I'm sorry I missed that part. So the problem is that uvicorn's root path is not propagated to fastapi.

@tiangolo @Kludex Excuse me, is this a bug?

I'm also having an issue with 026 post, see encode/uvicorn#2244 (comment)

Issue correlated with yours.

When I pass the root_path until 0.108 the request url visible in fastapi included the root_path, from 0.109 onwards it doesn't.

Example fastapi 0.108:
image

Example fastapi 0.109.2:
image

Also the redirect from trailing slash to without trailing slash doesn't include the root_path anymore, I could fix that by passing through the proxy also the root_path but this shouldn't be correct I think.
I just have to know if this is the correct behaviour for now on or not.

@Mattiam2 can you please log an issue for this? Because I think writing here we would not get attention easily since this issue shows up as Closed.