Starlette behind reverse proxy, url_for does not return externally valid urls
See original GitHub issueSuppose I have my application reverse proxied, such that http://my.domain/foo (e.g. Traefik or Nginx) has the upstream http://localhost:8000 (the Starlette app). url_for
will generate urls relative to the upstream url. How do I make it produce urls relative to the external url (with prefix /foo)?
For other projects, e.g. Flask/Werkzeug, this can be handled by setting SCRIPT_NAME
in the request environment (scope) if the ‘X-Forwarded-Prefix’ header is set. It does not look like there is an equivalent option in Starlette.
Am I missing something? Otherwise, consider this a feature request.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:11 (3 by maintainers)
Top Results From Across the Web
Starlette's url_for doesn't create links with https scheme ...
Let's encrypt works perfectly with the whole domain, no issues with the certificate. nginx · reverse-proxy · url-for · uvicorn · starlette.
Read more >How to handle relative urls correctly with a reverse proxy
How do I fix this? So far my reverse proxy has this on Server A (www.example.com): <Location /folder> ProxyPass http://test.
Read more >tiangolo/fastapi - Gitter
I would like to use fastapi as a reverse proxy to these other running instances "asynchronously" allowing for multiple users and handling authentication...
Read more >Flask Url_For Not Using Https Behind Nginx Reverse Proxy
Starlette behind reverse proxy urlfor does not return externally valid urls #604. This is what makes it different from a standard direct connection...
Read more >Behind a Proxy - FastAPI
In some situations, you might need to use a proxy server like Traefik or Nginx with a configuration that adds an extra path...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I really just think Starlette shouldn’t be trying to generate URLs with authority, here. The rule seems to be that, unless the authority you want to use is known to you in advance (such as having it stored in an environment variable or a config file), you should just specify the URL as an absolute path and leave it at that. It’s what Flask’s
url_for
does and what Django’sHttpRequest.build_absolute_uri(location)
does if you omit thelocation
parameter. Now thatroot_path
is properly acknowledged (see #690), URLs generated this way should be valid for a client making requests to a Starlette app from behind proxy.Trying to automatically build URLs with authority using the routing headers like that is just going to lead to issues. One should still be able to build authority-form URLs, but it shouldn’t be the default.
I am confused as to why this, and #592 , have both been closed, @tomchristie - do you regard this as something that should not be fixed in Starlette?