TL;DR
Try to include the needed ssl
configuration in your application,
that way FastAPI
would know how to serve server over HTTPS
.
Please try the following:
app = FastAPI(
ssl_keyfile="/path/to/your/private.key",
ssl_certfile="/path/to/your/certificate.crt"
)
The Reason You Get This Error
You get the mentioned error about mixing both secure and insecure elements in your website. It seems like you access your domain it has the needed SSL
certificates, but when you access the static files that are stored locally you are fetching things from localhsot
without knowing they exist. FastAPI
the way you've configured it does not know how to serve those files over HTTPS
. Thus, you get the exact problem of accessing https://www.example.com
and http://localhost
at the same time which is no longer supported by chrome.
Resolving the Issue
In order to create a FastAPI
app and configure it in a way it is served securely, you need to mention the ssl
keys and certificates as part of the configuration on the application. That way, when you are accessing the site, you'll have HTTPS
. Adding the needed ssl_keyfile
and ssl_certfile
as parameters to FastAPI
constructor should do the trick. (Please see the above code snippet)
HTTPS Is More Complex than That
I highly recommend taking the time to dive deep into the subject and read more about the subject at FastAPI
documentation site. As mentioned there:
It is easy to assume that HTTPS is something that is just "enabled" or not.
But it is way more complex than that.
Some Things to Notice Before Hitting Production
You should consider multiple factors when you deploy your site.
According to your description of the error, it seems like something might not be configured correctly.
For some reason, the server access localhost
in order to fetch the logo of the site/application. This might work when you are in the development area, but when you reach production environment is might break things. I would expect the server to fetch all static files from the server (using the domain) without trying to access some local stuff. Make sure the server is configured the right way.
Further Reading
If you are looking for some further examples about configuration for running the server manually, you can read more about that in the following blog posts:
If you are interested in checking the best practices regarding deployment settings, the FastAPI
documentation site would be a good start - covering the basics and offering some ground rules: