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 "certificate" 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 is your server and how small each application you have there might be. But...

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 server multiple HTTPS domains/applications.

For this to work, a single component (program) running in the server, listening in the public IP address, must have all the HTTPS certificates in the server.

After having a secure connection, the communication protocol is the same HTTP.

It goes encrypted, but the encrypted contents are the same HTTP protocol.

It is a common practice to have one program/HTTP server runing in 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 ofter called a TLS Termination Proxy.

Let's Encrypt

Up to some years ago, 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.

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 increased, by reducing their lifespan.

The domain's are securely verified and the certificates are generated automatically. This also allows automatizing the renewal of these certificates.

The idea is to automatize the acquisition and renewal of these certificates, so that you can have secure HTTPS, free, forever.

Traefik

Traefik is a high performance reverse proxy / load balancer. It can do the "TLS Termination Proxy" job (apart from other features).

It has integration with Let's Encrypt. So, it can handle all the HTTPS parts, including certificate acquisition and renewal.

It also has integrations with Docker. So, you can declare your domains in each application configurations and have it read those configurations, generate the HTTPS certificates and serve HTTPS to your application, all automatically. Without requiring any change in its configuration.

With this information and tools, continue with the next section to combine everything.

Docker Swarm mode cluster with Traefik and HTTPS

You can have a Docker Swarm mode cluster set up in minutes (about 20 min) with a main Traefik handling HTTPS (including certificate acquisition and renewal).

By using Docker Swarm mode, you can start with a "cluster" of a single machine (it can even be a $5 USD / month server) and then you can grow as much as you need adding more servers.

To set up a Docker Swarm Mode cluster with Traefik and HTTPS handling, follow this guide: