♻️ Update root_path handling (from --root-path CLI option) to include the root path prefix in the full ASGI path as per the ASGI spec#2213
Merged
Conversation
…e ASGI spe, related to Starlette 0.35.0
…path in the ASGI scope
tiangolo
marked this pull request as ready for review
January 13, 2024 12:45
Contributor
Author
|
Note, @Kludex has a question about how the access log works with this, we'll check that later, but DO NOT MERGE before that is resolved. |
Owner
|
The access logs are fine with this PR. 👍 |
Contributor
Author
|
Thanks @Kludex! 🚀 Then I think this is ready for review. 🤓 |
Closed
9 tasks
Kludex
requested changes
Jan 16, 2024
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Kludex
approved these changes
Jan 16, 2024
root_path handling (from --root-path CLI option) to include the root path prefix in the full ASGI path as per the ASGI spec, equivalent to Starlette 0.35.0root_path handling (from --root-path CLI option) to include the root path prefix in the full ASGI path as per the ASGI spec
8 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
♻️ Update
root_pathhandling (from--root-pathCLI option) to include the root path prefix in the full ASGIpathas per the ASGI spec, equivalent to Starlette 0.35.0Why
Uvicorn doesn't receive the scope from another ASGI component on top, it creates it from the raw HTTP protocol.
When it receives the CLI option
--root-pathit means that Uvicorn is behind another HTTP frontend proxy (like Traefik or Nginx) that is receiving that "root path" prefix and stripping it.More or less like this:
https://example.com/api/v1/items/42example.com, so it takes this request/api/v1/items/42and removes the prefix it has configured of/api/v1http://127.0.0.1:8000, notice that it's not HTTPS, only HTTP, and it's only on localhost, not the open web IP, it's also on port8000, not in the standard HTTP port80nor HTTPS port443http://127.0.0.1/items/42/items/42, but doesn't know of the prefix/api/v1, but the app might need to create someurl_forin templates or such, so it needs to also know the original path prefix it should use.--root-path=/api/v1, the same path prefix used by the HTTP proxy, to let it know that it is being served to the external clients at that path prefix.root_paththe value in the CLI option:/api/v1. But the path it received by the pure HTTP protocol was only/items/42because the HTTP proxy sent the request to the local Uvicorn running on localhost.pathhave the full path used by the clients, including any path prefix, it has to prepend that path prefix provided by the CLI--root-pathto the ASGI scope keypath.Checklist