Wildcard vhosts: one label, exact names first, and if_vhost to keep them in one group
Was this useful?
0 unique visitors
A vhost entry starting with *. is a pattern, so one group can serve
every subdomain of a parent without listing them:
vhosts:
- vhost: ["*.test.example.com"]
vhost_cert:
cert: "/etc/proxyauth/cert/test.example.com/fullchain.pem"
key: "/etc/proxyauth/cert/test.example.com/privkey.pem"
routes:
- prefix: "/"
target: "http://127.0.0.1:8080"
Wildcards arrived in 1.2.5. Their precedence rules were not right in that release, and the way they were fixed explains how matching works today.
What a pattern covers: exactly one label
This is RFC 6125’s rule, the same one browsers apply to certificate SANs. It was adopted on purpose, so that routing a hostname and holding a valid certificate for it can never disagree.
| Host | Against *.test.example.com |
|---|---|
a.test.example.com | matches |
test.example.com | no — the apex is a separate name |
a.b.test.example.com | no — a pattern never spans a dot |
*foo.example.com (as an entry) | read as a literal hostname |
Only a leading *. is a pattern. Hostnames are compared
case-insensitively with the port stripped.
The bug in 1.2.5
The intended rule is simple: a wildcard is a fallback. It must never take a request away from a host declared explicitly.
vhosts:
- vhost: ["git.example.com"]
routes:
- prefix: "/"
target: "http://127.0.0.1:3000"
- vhost: ["*.example.com"]
routes:
- prefix: "/"
target: "http://127.0.0.1:8080"
In 1.2.5, the matching order did not enforce that. Depending on the
configuration, a pattern could win for a host that was explicitly
declared elsewhere, and requests for git.example.com could land on the
wildcard’s backend. It was not a TLS or ACME issue; it was the vhost
selection logic. My oversight, and it was fixed in the following
releases.
How precedence works now
An exact vhost always wins over a wildcard that would also match — for request routing, for resolving vhost-level settings, and for TLS certificate selection. SNI follows the same order: exact name first, then patterns. File order plays no part.
The decision is made per request, not once per route at startup. That matters for a mixed list:
- vhost: ["example.com", "*.example.com"]
This group is an exact match when example.com is requested and a
wildcard match when a.example.com is. A single rank assigned to
the route could not express both, so matching runs two passes over the
requested host:
- routes naming the host literally, plus catch-all routes with no
vhost(which keep their historical rank); - then routes matching through a pattern.
Since 1.2.7, a mixed list like the one above correctly yields to a host declared explicitly in another group.
Apex and subdomains in one group: if_vhost
A group’s routes serve every host the group declares. if_vhost
(1.2.7) narrows one route to a subset, so a single group can hold two
roots:
vhosts:
- vhost: ["example.com", "*.example.com"]
vhost_cert:
cert: "/etc/proxyauth/cert/example.com/fullchain.pem"
key: "/etc/proxyauth/cert/example.com/privkey.pem"
certbot_renew: true
session_cookie: true
routes:
- prefix: "/"
if_vhost: ["example.com"]
target: "http://127.0.0.1:8000" # the apex
- prefix: "/"
if_vhost: ["*.example.com"]
target: "http://127.0.0.1:8080" # every undeclared subdomain
Certificate, renewal flag and session policy are written once. Without
if_vhost, this needs two full groups with every vhost-level setting
duplicated.
vhost and if_vhost answer different questions:
vhostdeclares what a route serves. Nothing checks it against anything.if_vhostfilters among what the group declared, and every entry is validated at startup against the group’s list, literally or through one of its patterns.
That validation earns its place. An entry the group does not cover would make the route serve a host the group’s certificate does not name. The browser would then refuse the connection before routing ever ran, with an error pointing nowhere near the typo. Instead, startup stops and names the route, the entry and the group’s list.
if_vhost is refused on a top-level route outside a group — there is
nothing to narrow — rather than silently ignored. A route without
if_vhost keeps its group’s vhost exactly as before.
Certificates are the real constraint
Routing a wildcard needs nothing special. Obtaining a certificate for it does. Let’s Encrypt issues wildcards only through DNS-01, which ProxyAuth performs manually:
proxyauth certbot new example.com
- It prints the TXT records to create and waits for confirmation.
- An apex and its wildcard produce two TXT values at the same name. Add two separate records; many DNS panels overwrite the first.
- Groups containing a wildcard are excluded from automatic renewal,
with a warning. Renew by hand before expiry;
certbot checkshows the days left. - Rehearse on the staging directory first.
The apex and wildcard sharing a vhost_cert end up on one certificate
with both as SANs.
Testing precedence
curl -sI --resolve git.example.com:443:127.0.0.1 https://git.example.com/
curl -sI --resolve foo.example.com:443:127.0.0.1 https://foo.example.com/
curl -sI --resolve example.com:443:127.0.0.1 https://example.com/
Log [vhost] and [route] in the access format while testing, so each
line tells you which group and route answered:
"format": "[time] [[vhost]] [route] [host] [status] [path]"
One label per asterisk, exact names first, decided per request — and
if_vhost to keep an apex and its subdomains under one certificate
without duplicating a whole group. The routing is easy; plan for the
certificate.