-
-
Notifications
You must be signed in to change notification settings - Fork 692
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
♻️ 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
This file contains 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
…e ASGI spe, related to Starlette 0.35.0
…path in the ASGI scope
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. |
The access logs are fine with this PR. 👍 |
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
Kludex
changed the title
♻️ Update
♻️ Update 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
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_path
handling (from--root-path
CLI option) to include the root path prefix in the full ASGIpath
as 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-path
it 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/42
example.com
, so it takes this request/api/v1/items/42
and removes the prefix it has configured of/api/v1
http://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 port80
nor HTTPS port443
http://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_for
in 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_path
the value in the CLI option:/api/v1
. But the path it received by the pure HTTP protocol was only/items/42
because the HTTP proxy sent the request to the local Uvicorn running on localhost.path
have the full path used by the clients, including any path prefix, it has to prepend that path prefix provided by the CLI--root-path
to the ASGI scope keypath
.Checklist