Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
9 tasks done
tiangolo opened this issue Jan 16, 2024 Discussed in #10948 · 13 comments
Closed
9 tasks done

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

tiangolo opened this issue Jan 16, 2024 Discussed in #10948 · 13 comments
Labels
bug Something isn't working

Comments

@tiangolo
Copy link
Member

tiangolo commented Jan 16, 2024

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

@RRRajput
Copy link

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

@Kludex
Copy link
Member

Kludex commented Jan 19, 2024

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

@insistence
Copy link

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

@tiangolo
Copy link
Member Author

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! ☕

@insistence
Copy link

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

@insistence
Copy link

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

@RRRajput
Copy link

RRRajput commented Feb 5, 2024

@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).

@insistence
Copy link

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.

@RRRajput
Copy link

RRRajput commented Feb 6, 2024

Ah Indeed!

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

@insistence
Copy link

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

@msmitherdc
Copy link

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

@Mattiam2

This comment was marked as duplicate.

@RRRajput
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants