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.

Question: producing https addresses using url_for

See original GitHub issue

I’m looking for a way to produce static file routes with https prefixes instead of the default http. For example, say I have the following template:

<script src="{{ url_for('static', path='file.js') }}"></script>

I would like it to produce:

<script src="https://my-app.herokuapp.com/static/file.js"></script>

I don’t find information about this on the documentation, so I was wondering if it was possible? I can’t use the <script src="/static/file.js"></script> form because I want to use the generated html on a different domain.

In case it makes a difference: I’m using fastAPI and the app is running on heroku.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

20reactions
tiangolocommented, Aug 6, 2019

Note: If in a hurry, skip to the end.

The way this information is passed to the app is via the ASGI spec itself. The server (Uvicorn, Hypercorn, or any other) is the one that tells the app if it is running on HTTPS or HTTP.

If you provide the HTTPS certificates to the ASGI server (e.g. Uvicorn) it will know it is running on HTTPS and pass that information to the framework (Starlette, FastAPI, or anything else).

But it’s very common to have a “TLS Proxy” on top of it. That would be something that has the HTTPS certificates, handles the connection, and passes the pure HTTP to the thing running behind (in this case, Uvicorn, running your Starlette/FastAPI app). Examples of programs that can run as TLS Proxies include Nginx, HAProxy, Traefik (I recommend Traefik 😉 ). The same would be done by Heroku or jwilder’s Docker image (based on Nginx).

But these TLS Proxies (and actually many other layers and servers) create some HTTP headers to let the thing that runs after them know that they are handling HTTPS for them.

But by default, none of the intermediate parts (Nginx, Traefik, Uvicorn) receive and accept those HTTPS headers from outside, as that would be a security risk. But if you know that the specific part (e.g. Uvicorn) is behind a TLS Proxy, you can normally configure/override it to receive those HTTP headers about the HTTPS connection.

In Uvicorn, the command parameter is --proxy-headers.

9reactions
tomchristiecommented, Nov 1, 2019

Note that the latest version of uvicorn now matches gunicorn behaviour - by default it will accept any X-Forwarded-For and X-Forwarded-Scheme headers if the client IP is in --forwarded_allow_ips (Defaults to the $FORWARDED_ALLOW_IPS environment variable, or else "127.0.0.1")

Read more comments on GitHub >

github_iconTop Results From Across the Web

force.com sites - Custom URL for https urls in Community
I created a CName record and mapped it to proper site. Its working fine for all HTTP URL. But after a user get...
Read more >
Are HTTPS URLs encrypted? - ssl
I was asking myself this question when making an HTTP request from a native (not browser based) App. I'm guessing this may interest...
Read more >
Salesforce URL Hacking for Lightning – Tutorial
URL Hacks are a handy way of creating records with specific ... and Mailing Address fields, as well as relate the Contact to...
Read more >
Creating a URL
An absolute URL contains all of the information necessary to reach the resource in question. You can also create URL objects from a...
Read more >
What happens when you type a URL into your browser?
We can do a DNS lookup to find the IP address of the server based on the ... With HTTPS, the data exchanged...
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