Caddy: How Your Server Gets HTTPS Automatically
Outline
Transcript
0:00 Caddy is a web server: it serves web requests and can manage certificates, identity documents a browser checks before trusting a server, for HTTPS, the web's protected connections. An expired certificate can break those connections even while the server is running. We'll trace a configured name through getting a certificate, saving it, using it for a connection and replacing it before expiry, then check what actually survives restarting Caddy. We're following Caddy's official docs and recorded versions of its source code and CertMagic, its certificate-management library, alongside a contained local experiment.
0:32 The source links are in the description. Start with the certificate on screen. It binds a hostname, the server name in an address, to a public key, with a digital signature, its signing service's verifiable approval. The server keeps the matching private key secret. During connection setup, a client, the software connecting to it, checks proof that the server holds that key. The public key lets it check without learning the private one. That setup is part of TLS, the protocol behind the protected connection in HTTPS.
1:04 The certificate helps verify the server; the connection then protects the traffic. That signing service is the certificate authority, also called the issuer. The browser has to trust that authority for its signature to establish trust. Yes, both relationships matter. For example, a client that doesn't trust this issuer will reject its certificate, even when the server can prove possession of its key. And even an accepted certificate expires. The server has to replace it before that deadline. So the server's alive, and the browser still says no.
1:35 What actually happens when we put that hostname in Caddy's configuration? Now follow the hostname into Caddy's automatic HTTPS setup. It collects eligible configured names and passes them to its TLS management code, which calls CertMagic. CertMagic was extracted from Caddy's certificate work, so that boundary has a purpose. The web server supplies the names, while the reusable library handles their certificates over time. My first instinct would be to request a certificate during startup and keep it in memory, the working copy inside one process, a running instance of Caddy.
2:08 What if we have one saved? If the certificate is already in memory and managed, meaning CertMagic is tracking it and replacing it when needed, that work continues. Otherwise, the management code tries loading a saved certificate first. If loading fails, this process still has no certificate. Why not ask the issuer for another one? Because unreadable storage doesn't tell us the certificate is absent. Suppose Caddy can't read its storage folder. The highlighted condition returns that storage error, rather than requesting a certificate.
2:39 Only missing stored material leads this path to obtain one, then load it. So a second certificate wouldn't answer the question of what was already saved. We still need to find out why that read failed. And when loading succeeds, the cache, the in-memory copy kept for reuse, supplies connections without a new issuer request for each one. Let's follow the missing-certificate branch to the issuer before checking our local restart evidence. The authority issues the certificate; a browser still decides whether it trusts that issuer.
3:10 Next, follow that public issuance path through ACME, the protocol that lets a client automate certificate requests with an authority. Here, Caddy is that client. Which gives us a second conversation alongside the browser connection. Caddy talks to the authority to get a certificate, then presents the resulting certificate to a browser. Exactly. HTTP-01 uses ordinary, unencrypted web requests: the authority fetches a particular resource to validate control of that name. It's one supported method, not the only one.
3:39 I'm not following one detail yet. If Caddy is the client making the request, why is the authority's arrow pointing back toward our server? Because requesting a name isn't proof that you control it. The authority requests a particular response, its challenge, then checks the answer through that hostname's HTTP endpoint, the web address serving it. At the address on screen, the hostname is the name being checked; the token is this challenge's value. Caddy serves the response there. For example, suppose that name sends the authority to a different server with no challenge response.
4:14 Validation fails even though the Caddy process asking for the certificate is running. And reaching the right server still isn't enough. The response combines the token with information from Caddy's account for certificate requests, whose key is separate from its server key. So does passing this HTTP check itself create the certificate, or is there another request involved? There's a certificate request containing the server's public key and requested names. Validation establishes the required control, while issuance produces the signed certificate for that key and those names.
4:47 That keeps the server's private key on the server. The public response to a challenge and the public key in a certificate request have different jobs; neither is the private key. Once we load the issued certificate, connection setup can select it for this hostname. It uses what's in memory rather than repeating this entire authority conversation. And this public path is an explanation from the code and protocol, not the experiment we ran. Our actual test is confined to a local authority, so its client trust has to be explicit.
5:20 Now contrast that public path with our actual local test. It uses a private authority whose root certificate identifies the issuer a client can choose to trust. Then I'm not sure a successful local connection tells us much about an ordinary browser. What exactly did the test client trust? Only the local authority supplied explicitly for this test. The default client trust rejected the connection; giving the client that root made the HTTPS verification succeed. So both results belong in the evidence.
5:49 The rejection demonstrates that creating a certificate locally hasn't quietly made its issuer trusted by every client. Now follow process A on screen. It obtains a certificate, and Caddy saves the certificate and private key in storage that we're keeping across the restart. The short identifier beside it is a fingerprint, a value computed from the certificate's bytes. Next, we can compare that identifier with what a later connection sees. We stop that process and start a new one against the same storage.
6:17 Process B is genuinely new, so its predecessor's memory can't be the explanation. And the connection to process B returns the same certificate fingerprint. The bytes identifying the certificate have survived, even though the process using them has changed. Yes, that matches the load-before-obtain branch we just traced. The server can recover its managed certificate from durable storage, meaning storage that remains available after the process exits. For example, keeping the configuration but throwing away its certificate storage would remove that particular recovery path.
6:47 A configuration file doesn't contain the certificate material we just saw reused. That's why I think the restart has to preserve the identity. Otherwise, a successful reconnect could hide the fact that we'd simply issued another certificate. We've changed the process without changing the certificate. But this certificate still expires, so preserving it alone won't keep future connections working indefinitely. What makes Caddy replace that certificate without restarting? Because the certificate is managed, CertMagic's background maintenance checks it and queues renewal, the work of obtaining a replacement, when needed.
7:20 The result is loaded back into the in-memory collection used for connections. A client gets the certificate selected by the running process, so saving a replacement and loading it into memory are both necessary. Suppose our scheduled task already renews the files and reloads the server. What does Caddy handle inside the process? That can work. Caddy's maintenance loads a replacement for future connections. CertMagic also checks shared storage, where another server process may already have renewed the certificate, and can reload that result.
7:50 For example, in our local run we accelerated the test with a 2-minute certificate and 1-second maintenance checks. Those are lab settings, not advice for a production server. With those lab settings, the first row shows certificate A's issue and expiry times. Keep process B in view, then follow the maintenance event into the second row, where certificate B has a later expiry. And the maintenance log records queuing renewal, attempting it and successfully renewing. A new connection then sees a changed fingerprint, while process B stays alive.
8:24 Nice. Does this short lifetime tell us when a public certificate would renew? No. These are deliberately short-lived private certificates. We measured replacement before expiry in this run, not a public renewal schedule. And we only checked new connections, not what happens to an established connection during renewal. Finally, use that evidence to separate three diagnostic boundaries. First, can the issuer validate the requested name through the method being used? That's the issuance boundary. For the public HTTP method we traced, the authority needs to reach the expected challenge resource and verify its response.
8:59 Next comes persistence. The restart test depended on recovering both the certificate and private key from storage that remained available, despite losing the old process's memory. And the 3rd boundary is client trust. Our default-trust rejection remains a useful result, because a successfully issued local certificate still needs an issuer that client accepts. For example, if a client rejects our private issuer, retrying public challenge handling isn't a diagnosis of that result. We already know which trust setting changed the outcome.
9:29 Likewise, if a new process has lost its stored certificate material, pointing at yesterday's successful issuance doesn't show that today's process can recover it. A reused certificate after restart and a replaced certificate without restart are evidence of different work. Those separate checks let us see which job failed. Thanks for listening to Learning Podcasts.