question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Starlette behind reverse proxy, url_for does not return externally valid urls

See original GitHub issue

Suppose 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
sm-Fifteencommented, Jan 27, 2020

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’s HttpRequest.build_absolute_uri(location) does if you omit the location parameter. Now that root_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.

2reactions
jonathanunderwoodcommented, Jan 27, 2020

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found