I have a html file and I configured the URL of the links as below.
app.mount("/static", StaticFiles(directory="static"), name="static")
{{ url_for('static', path='css/style.css') }}
I have got the domain creation for the application with nginx and haproxy.
When the html page is loaded the css and js stylings are not being loaded as it is using http instead of https.
Flask url_for generates http instead of https when running by docker
FastAPI links created by url_for in Jinja2 template use HTTP instead of HTTPS
I went with some of the links on similar issue but didn’t help as it is for html files.
When I hardcoded it worked. How can I make this static. I am using the uvicorn fastapi.
2
Answers
One way to ensure that
url_for
always generates https URLs is to set thePREFERRED_URL_SCHEME
configuration option tohttps
in your application. This will instructurl_for
to use thehttps
scheme unless explicitly overridden. Here is an example:In my case, FastAPI and uvicorn was behind reverse proxy on fly.io and have to add
--proxy-headers
and--forwarded-allow-ips '*'
to my uvicorn command. Full example:To find IP address:
How it’s works?
--proxy-headers
is enabled, uvicorn pass proxy headers further:X-Forwarded-Proto
,X-Forwarded-For
, …X-Forwarded-For
header.--forwarded-allow-ips
)X-Forwarded-Proto