The default Caddy ACME flow uses HTTP-01 — Caddy puts a token at /.well-known/acme-challenge/<token> and Let’s Encrypt hits that URL to prove control. That works for single hostnames reachable on port 80, but it cannot produce a wildcard cert (*.example.com) and it cannot work for hosts that are not internet-reachable. DNS-01 is the alternative: Caddy writes a DNS TXT record via your DNS provider’s API, Let’s Encrypt resolves it, and the cert issues — wildcards welcome, no port 80 needed. This article is how we deploy DNS-01 for clients running wildcards or internal-only hosts.
How to verify
A working DNS-01 setup looks like this:
caddy list-modules | grep -i dns.providers
sudo journalctl -u caddy --since '10m ago' | grep -iE 'dns.01|txt record|propagation'
dig +short TXT _acme-challenge.example.com
sudo ls -la /var/lib/caddy/.local/share/caddy/certificates/*/example.com/
openssl x509 -in /var/lib/caddy/.local/share/caddy/certificates/*/example.com/example.com.crt -noout -text | grep -A1 'Subject Alternative'
list-modules should show your DNS provider (dns.providers.cloudflare, dns.providers.route53, etc.). The journal should show challenge creation and propagation success. The dig mid-issuance shows the live TXT; after issuance it disappears. The cert SAN should include the wildcard.
What’s happening
The ACME protocol supports three challenge types: HTTP-01 (token at a URL), TLS-ALPN-01 (token in a TLS handshake), and DNS-01 (token in a DNS TXT record). DNS-01 is the only one that supports wildcards, because the validator wants to confirm you control the entire domain rather than just a single hostname.
For DNS-01, Caddy must be able to programmatically create a _acme-challenge.<hostname> TXT record in your DNS zone, wait for it to propagate to Let’s Encrypt’s resolvers, and then remove it after issuance. This requires three pieces:
-
A DNS provider plugin compiled into Caddy. The
caddy-dnsGitHub organization hosts community-maintained plugins for Cloudflare, Route 53, DigitalOcean, Google Cloud DNS, Azure, Hetzner, Vercel, and many more. See caddy modules and plugins for the full list. -
A DNS API token with the right scope. The token must allow Caddy to add and remove TXT records in the target zone — but should not give Caddy permission to delete the zone, modify A/AAAA records, or touch other zones.
-
A propagation timeout wide enough for your DNS provider’s worst case. Caddy waits for the TXT to be visible to Let’s Encrypt before submitting the challenge, but if your DNS provider takes 30 seconds to propagate and Caddy’s default is 2 minutes, you are fine; if it takes 5 minutes, Caddy times out and retries.
The DNS-01 challenge bypasses port 80 entirely. Caddy can issue certs for hosts that exist only on the internal network, as long as Caddy itself can reach the DNS API.
The procedure
-
Build Caddy with your DNS provider. Pick the one matching your DNS. The naming convention is
github.com/caddy-dns/<provider>:xcaddy build \ --with github.com/caddy-dns/cloudflare \ --output /usr/local/bin/caddy sudo systemctl restart caddyFor Route 53, replace
cloudflarewithroute53; for DigitalOcean,digitalocean; and so on. See xcaddy custom builds. -
Create the DNS API token. Scope it tightly. For Cloudflare, the token needs
Zone:DNS:Editpermission on the specific zone, nothing more. Save the token in/etc/caddy/cloudflare.env:echo 'CLOUDFLARE_API_TOKEN=<your-token>' | sudo tee /etc/caddy/cloudflare.env sudo chown caddy:caddy /etc/caddy/cloudflare.env sudo chmod 600 /etc/caddy/cloudflare.env -
Reference the env file from the systemd unit. Edit
/etc/systemd/system/caddy.service.d/override.conf(usesudo systemctl edit caddy):[Service] EnvironmentFile=/etc/caddy/cloudflare.envsudo systemctl daemon-reload sudo systemctl restart caddy -
Configure DNS-01 globally for all sites. The simplest form:
{ email ops@example.com acme_dns cloudflare {env.CLOUDFLARE_API_TOKEN} } *.example.com, example.com { reverse_proxy localhost:3000 }The wildcard
*.example.comis now allowed because DNS-01 supports it. Caddy issues a single cert covering both the apex and any subdomain. -
Per-site DNS-01. When only one site uses DNS-01 (e.g., one wildcard, the rest HTTP-01):
{ email ops@example.com } *.internal.example.com { tls { dns cloudflare {env.CLOUDFLARE_API_TOKEN} } reverse_proxy localhost:3000 } public.example.com { reverse_proxy localhost:3001 }The first site uses DNS-01 (required for the wildcard); the second uses HTTP-01.
-
Tune propagation timeouts. If your DNS provider is slow to propagate (rare for cloud DNS, common for some registrars), bump the wait time:
*.example.com { tls { dns cloudflare {env.CLOUDFLARE_API_TOKEN} propagation_timeout 5m propagation_delay 30s } }propagation_delayis how long Caddy waits after writing the TXT before checking;propagation_timeoutis the cap. For Cloudflare, defaults are fine (propagation is near-instant); for slower providers, raise both. -
Use a different account for DNS challenges. Some teams want a dedicated Let’s Encrypt account for wildcard certs so the audit trail is cleaner. Use a separate
emailin a site block — Caddy creates one account per email per ACME endpoint.
Operational notes
- The DNS API token leaks differently from a TLS private key. If a token leaks, the holder can change DNS for the configured zone — that is more invasive than a key leak. Rotate tokens on the same schedule as other secrets, and audit DNS provider logs for unexpected API calls.
- Caddy creates and removes the
_acme-challengeTXT records around each renewal cycle. If your DNS provider has rate limits on API calls (Cloudflare: 1200/5min for the dashboard, much higher for the API), you will hit them only at fleet scales — but plan for it. - DNS-01 is the only ACME challenge that works for hosts without internet ingress. For internal-only deployments, point the DNS-01 zone at a public DNS provider you control even though the hostname resolves only on your internal network.
- A wildcard cert has a single private key for
*.example.com. If that key is compromised, every subdomain is compromised. For high-sensitivity deployments, issue per-subdomain certs via HTTP-01 instead. - The TXT record name is
_acme-challenge.<hostname>. For*.example.com, the record is_acme-challenge.example.com(the apex), not_acme-challenge.*.example.com. This sometimes surprises operators expecting wildcard-shaped records.
Stack Harbor configures DNS-01 for clients running wildcards, internal-only hosts, or multi-tenant SaaS — DNS API tokens under secret management, propagation timeouts tuned to the actual provider, and the renewal cycle monitored alongside the rest of the cert state. If you have a Caddy deployment that needs wildcards or operates behind a firewall and you want the certificate plane operated for you, this is the kind of work covered by our Managed Operations practice.