Option 1
You could use HTTPSRedirectMiddleware
to enforce all incoming requests to http
being redirected to the secure scheme instead.
from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware
app = FastAPI()
app.add_middleware(HTTPSRedirectMiddleware)
Option 2
In addition to the above, you could use relative URLs instead of using url_for()
in your Jinja2 template; for instance:
<a href="/upload_schedule">Link text</a>
In this way, the scheme of the URL will remain https
.
Option 3
You could have your own url_for
custom function to replace the scheme of the URL, similar to the approach demonstrated in Option 2 of this answer.
If you are behind a proxy server, such as Nginx, you may also try passing --proxy-headers
parameter to Uvicorn, as described here.