Automatic HTTPS & the Internal CA
Caddy's best-known feature is that HTTPS needs no configuration: name a site and it gets a certificate. ACME & Let's Encrypt explains the protocol behind it. This lesson looks at the rules Caddy follows, its internal CA for private names (which also makes it a convenient development and lab CA), and on-demand TLS, the feature SaaS platforms use to serve thousands of customer domains.
When automatic HTTPS turns on
Caddy enables it for a site when the address has a host name (or IP) and no explicit http:// scheme or HTTP-only port. Then it:
- obtains a certificate: from Let's Encrypt or ZeroSSL for public names; from its internal CA for
localhost, IP addresses and names under.localhost,.local,.internaland.home.arpa, which no public CA can certify; - redirects HTTP to HTTPS on port 80 (
http_port); - renews automatically, well before expiry (by default when about a third of the lifetime is left).
| You write | Result |
|---|---|
shop.example.com { … } |
public certificate via ACME, port 80 redirects |
localhost { … }, app.internal { … } |
certificate from the internal CA |
http://shop.example.com { … } or :8080 { … } |
plain HTTP, no certificate |
tls internal |
use the internal CA even for a public-looking name |
tls ops@shop.example |
ACME with that account email |
tls cert.pem key.pem |
your own files, no automation |
global auto_https off / disable_redirects |
turn the feature (or only the redirects) off |
Public certificates need the CA to reach the server on port 80 or 443, or a DNS plugin for the DNS-01 challenge (wildcards); none of that is possible in the sandbox, so its sessions use the internal CA, which works offline and behaves the same way from the client's side.
The internal CA
cat > Caddyfile <<'EOF'
{
skip_install_trust # don't try to add the root to the system trust store (needs root)
http_port 8080
https_port 8443
}
shop.internal:8443, api.internal:8443 {
tls internal
respond "hello from {host}"
}
EOF
caddy start --config Caddyfile > caddy.log 2>&1 && echo "caddy started"
for i in $(seq 50); do curl -sk https://127.0.0.1:8443/ -o /dev/null && break; sleep 0.1; done
ls .local/share/caddy/pki/authorities/local/
ROOT=.local/share/caddy/pki/authorities/local/root.crt
R="--resolve shop.internal:8443:127.0.0.1 --resolve api.internal:8443:127.0.0.1 --resolve shop.internal:8080:127.0.0.1"
curl -s $R --cacert $ROOT https://shop.internal:8443/; echo
curl -s $R --cacert $ROOT https://api.internal:8443/; echo
curl -s -o /dev/null -w "http -> %{http_code} %{redirect_url}\n" $R http://shop.internal:8080/x
openssl s_client -connect 127.0.0.1:8443 -servername shop.internal -showcerts < /dev/null 2>/dev/null | grep -E '^ 0 s:|^ i:' | head -2
openssl s_client -connect 127.0.0.1:8443 -servername shop.internal < /dev/null 2>/dev/null | openssl x509 -noout -ext subjectAltName -dates > leaf.txt
grep DNS leaf.txt | xargs
start=$(sed -n 's/notBefore=//p' leaf.txt); end=$(sed -n 's/notAfter=//p' leaf.txt)
echo "leaf lifetime: $(( ($(date -d "$end" +%s) - $(date -d "$start" +%s)) / 3600 )) hours"
caddy stop > /dev/null 2>&1caddy started
intermediate.crt
intermediate.key
root.crt
root.key
hello from shop.internal
hello from api.internal
http -> 308 https://shop.internal/x
0 s:
i:CN = Caddy Local Authority - ECC Intermediate
DNS:shop.internal
leaf lifetime: 12 hours
What the session shows:
- Caddy created a CA in its data directory (
~/.local/share/caddy/pki/authorities/local/): a root and an intermediate, each with its key. Leaf certificates are signed by the intermediate. Clients trust the root;caddy trustinstalls it into the system store (it needs root, which is why the session passes--cacertinstead). - The leaf certificate has no subject, only the names in subjectAltName, which is all modern clients check.
- Internal leaf certificates live for 12 hours and are renewed automatically in the background. Short lifetimes make revocation nearly unnecessary; they also mean a Caddy that is stopped for a day serves expired certificates when it comes back until it renews them (it does so at start).
- The HTTP port redirected to HTTPS, keeping the path. The target has no port: Caddy redirects to the standard port 443, which is right on a real server and wrong in a lab that moved HTTPS to 8443.
Uses beyond development: an internal service mesh-lite where every service is fronted by Caddy with tls internal and the root is distributed to clients; acme_server turns Caddy into an ACME server for your network, so other machines get certificates from it with ordinary ACME clients.
On-demand TLS
A SaaS product where customers point their own domains (shop.customer.com) at your servers can't list the names in a config. On-demand TLS obtains a certificate during the first TLS handshake for a name, after asking your application whether that name is allowed (the ask endpoint must return 200). Without the check, anyone could point a domain at you and make you request certificates until you hit the CA's rate limits.
cat > ask.py <<'EOF'
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse, parse_qs
CUSTOMERS = {"alice.shop.test", "bob.shop.test"} # in real life: a database lookup
class H(BaseHTTPRequestHandler):
def do_GET(self):
domain = parse_qs(urlparse(self.path).query).get("domain", [""])[0]
self.send_response(200 if domain in CUSTOMERS else 403); self.end_headers()
def log_message(self, *a): pass
HTTPServer(("127.0.0.1", 9100), H).serve_forever()
EOF
python3 ask.py &
cat > Caddyfile <<'EOF'
{
skip_install_trust
http_port 8080
https_port 8443
on_demand_tls {
ask http://127.0.0.1:9100/check
}
}
https:// {
tls internal {
on_demand
}
respond "storefront for {host}"
}
EOF
for i in $(seq 50); do curl -s "127.0.0.1:9100/?domain=x" -o /dev/null && break; sleep 0.1; done
caddy start --config Caddyfile > caddy.log 2>&1 && echo "caddy started"
ROOT=.local/share/caddy/pki/authorities/local/root.crt
for h in alice.shop.test bob.shop.test mallory.example; do
printf '%-16s %s\n' "$h:" "$(curl -sS --resolve $h:8443:127.0.0.1 --cacert $ROOT https://$h:8443/ 2>&1)"
done
grep -c '"certificate obtained successfully"' caddy.log
caddy stop > /dev/null 2>&1; kill %1caddy started
alice.shop.test: storefront for alice.shop.test
bob.shop.test: storefront for bob.shop.test
mallory.example: curl: (35) OpenSSL/3.0.13: error:0A000438:SSL routines::tlsv1 alert internal error
2
Two certificates were issued during the handshakes; for mallory.example the ask endpoint said no, and Caddy aborted the handshake with a TLS alert. With a public ACME CA instead of tls internal, the same configuration serves real customer domains. Rate limits and first-request latency (issuing takes a few seconds) are the operational concerns; Caddy stores the certificates, so it only happens once per domain.
Where certificates live and why that matters
All of this state (CA keys, certificates, ACME accounts) lives in Caddy's storage: by default the data directory of the user running Caddy. Several Caddy instances behind a load balancer must share storage (a shared filesystem, or storage plugins for Redis, Consul, S3 and others), or each instance will request its own certificates, multiplying the load on the CA and defeating ACME HTTP challenges that land on the wrong instance. Caddy coordinates instances through locks in that shared storage.
Test yourself
Which site address makes Caddy obtain a public certificate via ACME?
:8080http://shop.example.comshop.example.comlocalhost
Why must on-demand TLS use an ask endpoint?
- For speed
- Without it, anyone pointing a domain at your server would make Caddy request certificates, exhausting CA rate limits
- ACME requires it
- To store certificates
How long were the internal CA's leaf certificates valid in the session?
- 90 days
- 1 year
- 12 hours, renewed automatically
- Forever