On-demand TLS is the Caddy feature for SaaS products that allow customers to point their own domain at your service. Instead of pre-issuing certificates for every customer at signup, Caddy issues the cert during the first TLS handshake against the new hostname — millisecond delay on first request, no delay afterward. This article is how we deploy that for clients in production, including the ask-endpoint that prevents abuse and the storage backing that keeps issuance lockstep across nodes.
How to verify
A working on-demand setup looks like this from the outside:
curl -I https://newcustomer.example.com/ # first hit, slow
curl -I https://newcustomer.example.com/ # second hit, instant
sudo journalctl -u caddy --since '10m ago' | grep -iE 'on.demand|ask|certificate'
sudo ls -la /var/lib/caddy/.local/share/caddy/certificates/acme-v02.api.letsencrypt.org-directory/
curl -s 'https://your-ask-endpoint.example.com/ask?domain=newcustomer.example.com'
The first request takes 1–5 seconds while Caddy completes the ACME challenge. The second is instant. The journal shows the on-demand path being hit. The ask-endpoint returns 200 for permitted domains, anything else for denied.
What’s happening
On-demand TLS reverses the normal Caddy issuance flow. Without it, Caddy enumerates every hostname in the config at boot, issues a cert for each, and rejects connections to hostnames it has not issued for. With it, Caddy accepts the TLS handshake for an unknown hostname, calls the ask-endpoint to verify the domain is allowed, issues a cert in real time, and completes the handshake. The TLS-ALPN-01 challenge requires no DNS or HTTP plumbing; the ACME challenge happens on port 443 during the handshake.
The ask-endpoint is the abuse control. Without it, anyone pointing any domain at your Caddy IP triggers an ACME order — that is how you exhaust the Let’s Encrypt rate limit (50 certs per registered domain per week, but more importantly the 300 new orders per account per 3 hours limit) and how you become an unintentional certificate generator for whoever owns the IP after a DHCP shuffle. The endpoint is a simple HTTP GET that Caddy hits before issuing: GET /ask?domain=newcustomer.example.com. A 2xx response means “allow”, anything else means “deny.” Most teams query their customer database or a Redis allowlist.
Storage matters more under on-demand than for static configs. Every TLS handshake against an unknown hostname triggers a storage read for an existing cert before falling back to issuance. If storage is on local disk and you have ten Caddy nodes behind a load balancer, the same cert gets issued ten times the first time each node sees the hostname. Use shared storage — Redis, Consul, or S3 — when running multi-node on-demand.
The procedure
-
Configure the ask-endpoint. This endpoint is the source of truth for which domains your platform allows. Keep it fast and cheap — it runs on every cold cert lookup:
{ on_demand_tls { ask https://api.example.com/internal/caddy-ask interval 2m burst 5 } }intervalandburstare the rate limit Caddy enforces on issuance for hostnames the ask-endpoint approves — the safety net if the ask-endpoint approves a domain you should not have approved. -
Enable on-demand on the catch-all site block. This is what catches “any hostname pointed at us”:
:443 { tls { on_demand } reverse_proxy backend.internal:8080 }The
:443site block has no hostname — it matches any host name presented during the TLS handshake. Thetls { on_demand }inside it activates on-demand for any hostname that the ask-endpoint approves. -
Implement the ask-endpoint. A minimal Node.js example, but the shape is the same in any language:
// GET /internal/caddy-ask?domain=<hostname> app.get('/internal/caddy-ask', async (req, res) => { const domain = req.query.domain; if (!domain) return res.status(400).end(); const allowed = await db.customDomains.exists({ hostname: domain }); return res.status(allowed ? 200 : 404).end(); });Caddy only checks the HTTP status. Anything 2xx is allow, anything else is deny. Keep response time under 200ms — Caddy will wait, and the client is on the TLS handshake clock.
-
Switch storage to shared backing. For a multi-node deployment, install one of the storage plugins (caddy modules and plugins) and point Caddy at the shared backend:
{ storage redis { host redis.internal port 6379 db 0 tls true timeout 10 } }With Redis storage, the first node to handshake for a new hostname acquires a lock, performs the ACME order, writes the cert, and releases the lock. Subsequent nodes read the cert from Redis instead of repeating the issuance.
-
Customer onboarding flow. The bit your application owns:
- Customer adds
newcustomer.example.comin your dashboard. - Your app inserts a row into the
customDomainstable. - Your app prompts the customer to add a DNS A or CNAME record pointing at your Caddy IP/CNAME.
- Caddy idles. No issuance happens yet.
- The first browser hit to
https://newcustomer.example.com/reaches Caddy, the TLS handshake starts, Caddy asks the ask-endpoint, the endpoint says yes, Caddy issues, the handshake completes. - Subsequent requests serve from the cached cert.
- Customer adds
-
Pre-issuance for SLO-sensitive launches. If a customer is launching a campaign and cannot afford the first-request delay, pre-issue manually:
curl -X POST https://your-caddy:2019/load \ -H 'Content-Type: application/json' \ -d @prewarm.jsonThe prewarm config adds the hostname to a non-catch-all block briefly; Caddy issues the cert; you remove it. The cert lives in shared storage and serves from there.
Operational notes
- The ACME TLS-ALPN-01 challenge requires port 443 reachable from Let’s Encrypt’s validators. If your load balancer rewrites the SNI or terminates TLS, the challenge fails. Run on-demand on the TLS-terminating layer, not behind one.
- Let’s Encrypt rate limits hit hard at scale. The hard limits are 300 new orders per account per 3 hours and 50 certs per registered domain per week. For a 10,000-tenant SaaS, you will exceed these without DNS-01 — switch to DNS-01 for the wildcard or accept that fast onboarding for a thousand domains takes weeks of background issuance.
- The ask-endpoint is on the TLS handshake critical path. A 5-second response means a 5-second TLS handshake. Cache the lookup in your app for popular hostnames.
- Shared storage failures are silent — if Redis is down, every node falls back to its local view, which may not include the cert. Monitor
caddy_storage_io_errors_totalfrom Caddy metrics. - A wildcard cert via DNS-01 is operationally simpler than on-demand for a small number of subdomains under your own apex. Use on-demand only when customers bring their own apex domains.
Stack Harbor runs on-demand TLS for SaaS clients hosting thousands of customer domains — the ask-endpoint integrated with the customer database, the storage backed by a shared cluster with monitored failover, and the rate-limit thresholds tuned to the actual onboarding pattern. If you have a SaaS that allows customers to bring their own domain and you want the certificate plane operated for you, this is the kind of work covered by our Managed Operations practice.