It is easy to assume that HTTPS is something that is just "enabled" or not.
But it is way more complex than that.
If you are in a hurry or don't care, continue with the next sections for step by step instructions to set everything up with different techniques.
To learn the basics of HTTPS, from a consumer perspective, check https://howhttps.works/.
Now, from a developer's perspective, here are several things to have in mind while thinking about HTTPS:
- For HTTPS, the server needs to have "certificates" generated by a third party.
- Those certificates are actually acquired from the third-party, not "generated".
- Certificates have a lifetime.
- They expire.
- And then they need to be renewed, acquired again from the third party.
- The encryption of the connection happens at the TCP level.
- That's one layer below HTTP.
- So, the certificate and encryption handling is done before HTTP.
- TCP doesn't know about "domains". Only about IP addresses.
- The information about the specific domain requested goes in the HTTP data.
- The HTTPS certificates "certify" a certain domain, but the protocol and encryption happen at the TCP level, before knowing which domain is being dealt with.
- By default, that would mean that you can only have one HTTPS certificate per IP address.
- No matter how big your server is or how small each application you have on it might be.
- There is a solution to this, however.
- There's an extension to the TLS protocol (the one handling the encryption at the TCP level, before HTTP) called SNI.
- This SNI extension allows one single server (with a single IP address) to have several HTTPS certificates and serve multiple HTTPS domains/applications.
- For this to work, a single component (program) running on the server, listening on the public IP address, must have all the HTTPS certificates in the server.
- After obtaining a secure connection, the communication protocol is still HTTP.
- The contents are encrypted, even though they are being sent with the HTTP protocol.
It is a common practice to have one program/HTTP server running on the server (the machine, host, etc.) and managing all the HTTPS parts : sending the decrypted HTTP requests to the actual HTTP application running in the same server (the FastAPI application, in this case), take the HTTP response from the application, encrypt it using the appropriate certificate and sending it back to the client using HTTPS. This server is often called a TLS Termination Proxy.
Before Let's Encrypt, these HTTPS certificates were sold by trusted third-parties.
The process to acquire one of these certificates used to be cumbersome, require quite some paperwork and the certificates were quite expensive.
But then Let's Encrypt was created.
It is a project from the Linux Foundation. It provides HTTPS certificates for free. In an automated way. These certificates use all the standard cryptographic security, and are short lived (about 3 months), so the security is actually better because of their reduced lifespan.
The domains are securely verified and the certificates are generated automatically. This also allows automating the renewal of these certificates.
The idea is to automate the acquisition and renewal of these certificates, so that you can have secure HTTPS, for free, forever.