Sub topic 17
Wildcard certificates with DNS-01
A wildcard certificate covers every name at one level under a domain with a single certificate. This chapter explains why Let's Encrypt only issues wildcards through the DNS-01 challenge. It also shows how to request one by hand and how to automate renewal with a DNS provider plugin.
It assumes you have already obtained a certificate with the HTTP-01 challenge and that nginx serves it. You should be comfortable editing DNS records and know the layout of /etc/letsencrypt.
What a wildcard covers
A wildcard name has an asterisk in the leftmost label. The asterisk matches exactly one label.
| Certificate name | `www.example.com` | `example.com` | `a.b.example.com` |
|---|---|---|---|
| `*.example.com` | Covered | Not covered | Not covered |
| `example.com` | Not covered | Covered | Not covered |
For this reason you almost always request both example.com and *.example.com in the same certificate. Both names go on one certificate as separate Subject Alternative Names. Deeper levels such as a.b.example.com need their own wildcard, *.b.example.com.
Why wildcards require DNS-01
With HTTP-01, the CA fetches a file from http://name/.well-known/acme-challenge/. That proves control of one web server answering for one host name. A wildcard claims every possible host name under the domain, including ones that do not exist yet. A web server cannot prove that. Control of the DNS zone can. So Let's Encrypt only validates wildcard names with DNS-01.
In DNS-01, the CA gives your client a token. The client publishes a derived value as a TXT record at _acme-challenge.<domain>. The CA then queries DNS for that record.
The apex name and the wildcard share the same challenge name, _acme-challenge.example.com. A request for both therefore creates two TXT records at that one name. DNS allows multiple values on a TXT record set, so both coexist until validation finishes.
Requesting a wildcard manually
Manual mode works anywhere and teaches you what happens. Run it once to understand the process.
sudo certbot certonly --manual --preferred-challenges dns \
-d example.com -d '*.example.com'Quote the wildcard so the shell does not expand it. Certbot prints a TXT record name and value for each name. Create them at your DNS provider. Do not press Enter yet.
Before you continue, check that the records are visible from outside:
dig +short TXT _acme-challenge.example.com
dig +short TXT _acme-challenge.example.com @1.1.1.1You should see both values in the output. Press Enter in Certbot only after they appear. If you continue too early, the CA may cache a negative answer and validation fails.
Automating with a DNS provider plugin
Certbot has DNS plugins for several providers. Examples are Cloudflare, Route 53, DigitalOcean and Google Cloud DNS. The plugin calls the provider API to create and remove the TXT record, which makes renewal fully unattended. This example uses Cloudflare.
Install the plugin:
sudo apt install python3-certbot-dns-cloudflareCreate an API token at Cloudflare limited to Zone:DNS:Edit on the one zone you need. Store it in a credentials file readable only by root:
sudo install -m 600 /dev/null /etc/letsencrypt/cloudflare.ini
sudo tee /etc/letsencrypt/cloudflare.ini >/dev/null <<'EOF'
dns_cloudflare_api_token = REPLACE_WITH_YOUR_TOKEN
EOFTest against staging first, as you did for HTTP-01:
sudo certbot certonly --dns-cloudflare \
--dns-cloudflare-credentials /etc/letsencrypt/cloudflare.ini \
--dns-cloudflare-propagation-seconds 60 \
--staging \
-d example.com -d '*.example.com'The propagation option sets how long Certbot waits after creating the record before asking the CA to validate. The default is short. Raise it if your provider is slow to publish.
When the staging run succeeds, repeat the command without --staging. Certbot may report that a certificate with that name already exists and ask whether to replace it. Using --cert-name example.com makes the lineage explicit. Add --force-renewal only if you intend to replace the existing certificate.
Certbot records the plugin and credentials path in /etc/letsencrypt/renewal/example.com.conf. The renewal job reads that file, so certbot renew uses the same method with no extra options.
Using the certificate in nginx
The files live under /etc/letsencrypt/live/example.com/, named after the first -d value. A wildcard does not change the file names. The nginx configuration points at the same fullchain.pem and privkey.pem as before. Here a single wildcard server block serves any subdomain:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com *.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
root /var/www/html;
}Check and reload as usual:
sudo nginx -t && sudo systemctl reload nginxDelegating the challenge with CNAME
Giving a server a token that can edit your whole zone is uncomfortable. A common mitigation is delegation. Create a CNAME at _acme-challenge.example.com that points to a name in a zone you use only for validation, for example _acme-challenge.example.com CNAME example.com.acme.example.net.. Let's Encrypt follows the CNAME when it looks up the TXT record. The Certbot host then needs API rights only for the validation zone. A compromise there cannot alter your main records.
This works with ACME clients that support following the CNAME. With Certbot, the plugin must create the TXT record in the target zone. Confirm that your plugin and provider handle this before relying on it, and test with --staging.
Trade-offs and failure modes
A wildcard has costs as well as convenience.
- One key for many hosts. Every server using the certificate holds the same private key. If one host is compromised, all names under the wildcard are exposed. Prefer separate certificates when hosts differ in trust level.
- Wildcards hide inventory. Individual certificates appear in Certificate Transparency logs by name. A wildcard reveals only the pattern, which can be an advantage. It is not a reason to skip other security controls.
- CAA records. If your zone has CAA records,
issuecontrols normal names andissuewildcontrols wildcards. A CAA record that allows onlyissue "letsencrypt.org"also covers wildcards when noissuewildexists. Anissuewild ";"entry forbids them. - Propagation delay. The most common failure is validation running before the TXT record is visible. Query the authoritative name servers directly with
dig TXT _acme-challenge.example.com @ns1.provider.example. - Stale records. A leftover TXT record from a failed run is harmless to validation but clutters the zone. Remove it by hand if the plugin did not.
Verifying the result
Confirm the wildcard appears in the names the server presents:
echo | openssl s_client -connect www.example.com:443 -servername www.example.com 2>/dev/null \
| openssl x509 -noout -ext subjectAltNameYou should see DNS:example.com and DNS:*.example.com. Then confirm renewal works end to end:
sudo certbot renew --dry-runA dry run that passes proves the plugin, credentials and API access still work. That check matters here because a DNS token that expires or is revoked is the usual reason a working wildcard setup fails months later.
Exercise
You requested -d '*.example.com' only, and visitors to https://example.com now see a name mismatch error. Why does this happen, and how do you fix it?
Show answer
The wildcard matches exactly one label to the left of example.com, so it covers www.example.com but not the apex example.com. Request the certificate again with both names, -d example.com -d '*.example.com'. The two names share one _acme-challenge.example.com TXT record set, so DNS-01 validates both in the same run. Reload nginx afterwards.
Exercise
A wildcard certificate obtained with --manual worked, but sixty days later the renewal timer fails. What caused it, and what is the production fix?
Show answer
Manual mode needs a person to publish a new TXT value at each renewal, and an unattended timer cannot do that. Switch to a DNS provider plugin, such as --dns-cloudflare with a zone-scoped API token in a mode 600 credentials file, or add authentication hooks that call your DNS API. Then confirm with sudo certbot renew --dry-run.