Wildcard Subdomain Router in traefik

before I discover and give traefik a try, I used to have nginx to reverse proxy requests on my servers to my application containers. finding traefik took this setup to a completely different level, the automatic letsencrypt certificate generation and easy management of configuration through labels was enough to make traefik awesome.

Anyhow I think its enough talking about traefik, lets talk about how to have a router rule to route all the subdomains of a domain to a container.

normally we use Host rule to define a domain which traefik should reverse the proxy for, but in case of wanting to have a wildcard rule we should use HostRegexp.

Here is an example of traeifk label that proxies all the subdomains of postrocknation.com to a specific container:

HostRegexp(`^.+\.postrocknation\.com$`)

in many cases you want to proxy the requests to the same domain to the same container too, in this case we should combine the above configuration with Host rule.

Host(`postrocknation.com`) || HostRegexp(`^.+\.postrocknation\.com$`)

As you can see its pretty easy to have a rule in traefik to proxy wildcard subdomains to your containers.

Hope this helps some of you.
Happy Coding.

Leave a Reply