Wildcard TLS for New Subdomains with Traefik and Cloudflare
You add a DNS record, open the new subdomain in a browser, and get a certificate warning. Running curl -I shows the cert is issued to TRAEFIK DEFAULT CERT. The DNS is fine and Traefik isn’t broken. Nothing has told Traefik that the hostname exists yet.
This post covers the fix we use: one router with a wildcard certificate, issued through Cloudflare’s DNS-01 challenge.
<!-- more -->Why the default cert shows up
Traefik only requests a certificate for a hostname when a router matches it. If a request arrives for a hostname that no router matches, Traefik falls back to its self-signed default certificate.
You can confirm that’s what you’re getting:
^openssl s_client -connect docs.example.com:443 -servername docs.example.com </dev/null
| lines
| where $it =~ "subject="
If the subject is CN=TRAEFIK DEFAULT CERT, no router is claiming that host.
The pattern: one router, one wildcard
A DNS-01 challenge doesn’t require port 80 to be reachable, and it can issue wildcard certificates. That means you request the cert once, and every new subdomain is covered automatically.
| Approach | Port 80 needed | Wildcards | Cert per subdomain |
|---|---|---|---|
| HTTP-01 challenge | Yes | No | Yes |
| TLS-ALPN-01 | No (443) | No | Yes |
| DNS-01 (Cloudflare) | No | Yes | No |
The labels
Keep every label for a router in one file. Splitting rule from tls across files is how you end up debugging for an hour.
# compose.yml
services:
docs:
image: registry.example.com/site/docs:latest
networks:
- docs-private
labels:
traefik.enable: "true"
traefik.docker.network: docs-private
traefik.http.routers.docs.entrypoints: web-secure
traefik.http.routers.docs.rule: "Host(`docs.example.com`)"
traefik.http.routers.docs.tls: "true"
traefik.http.routers.docs.tls.certresolver: cert-cloudflare
traefik.http.routers.docs.tls.domains[0].main: example.com
traefik.http.routers.docs.tls.domains[0].sans: "*.example.com"
traefik.http.services.docs.loadbalancer.server.port: 8080
networks:
docs-private:
external: true
Note: The app joins its own private network, and Traefik joins that network too. Don’t attach apps to a shared public network. Isolation between apps is the whole point.
Verify it
- Restart the app container so Traefik picks up the labels.
- Wait a minute for the DNS-01 challenge to finish.
- Check the certificate again:
^curl -sI https://docs.example.com | lines | first 1
You should get HTTP/2 200 and a valid certificate chain.
Checklist
- DNS record exists (A/AAAA, DNS-only)
- Router labels live in one file
-
certresolverpoints at the DNS-01 resolver - Add the next subdomain. It’s covered by the wildcard already 🎉
Further reading
Questions or corrections? Open an issue on the site repo.1
Wildcards only cover one level. *.example.com matches docs.example.com but not v2.docs.example.com.