docs: Add wildcard certificates common pattern

We did recently add a similar example to the `abort` directive docs, but I feel this is a good fit in this page as well since it's a commonly repeated pattern we respond with when answering forum questions.
This commit is contained in:
Francis Lavoie 2021-03-14 14:44:47 -04:00
parent caede64af5
commit 9054a2f1d2
No known key found for this signature in database
GPG key ID: 7D1A27F0725BE5D8

View file

@ -15,6 +15,7 @@ These are not drop-in solutions; you will have to customize your domain name, po
- [PHP](#php)
- [Redirect `www.` subdomain](#redirect-www-subdomain)
- [Trailing slashes](#trailing-slashes)
- [Wildcard certificates](#wildcard-certificates)
## Static file server
@ -125,3 +126,33 @@ redir /remove/ /remove
```
Using a redirect, the client will have to re-issue the request, enforcing a single acceptable URI for a resource.
### Wildcard certificates
If you need to serve multiple subdomains with the same wildcard certificate, the best way to handle them is with a Caddyfile like this, making use of the [`handle`](/docs/caddyfile/directives/handle) directive and [`host`](/docs/caddyfile/matchers#host) matchers:
```caddy
*.example.com {
tls {
dns <provider_name> [<params...>]
}
@foo host foo.example.com
handle @foo {
respond "Foo!"
}
@bar host bar.example.com
handle @bar {
respond "Bar!"
}
# Fallback for otherwise unhandled domains
handle {
abort
}
}
```
Note that you must enable the [ACME DNS challenge](/docs/automatic-https#dns-challenge) to have Caddy automatically manage wildcard certificates.