# 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:

```nushell
^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.

```yaml
# 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

1. Restart the app container so Traefik picks up the labels.
2. Wait a minute for the DNS-01 challenge to finish.
3. Check the certificate again:

```nushell
^curl -sI https://docs.example.com | lines | first 1
```

You should get `HTTP/2 200` and a valid certificate chain.

## Checklist

- [x] DNS record exists (A/AAAA, DNS-only)
- [x] Router labels live in one file
- [x] `certresolver` points at the DNS-01 resolver
- [ ] Add the next subdomain. It's covered by the wildcard already 🎉

## Further reading

- [Traefik: Docker routing labels](https://doc.traefik.io/traefik/routing/providers/docker/)
- [Traefik: ACME DNS challenge](https://doc.traefik.io/traefik/https/acme/#dnschallenge)
- [Cloudflare: API tokens](https://developers.cloudflare.com/fundamentals/api/get-started/create-token/)

---

*Questions or corrections? Open an issue on the [site repo](https://example.com/repo).*[^1]

[^1]: Wildcards only cover one level. `*.example.com` matches `docs.example.com` but not `v2.docs.example.com`.
