How to exclude a domain from HostRegexp in Traefik

If you’re brave like me and have hosted an app with wildcard subdomains on your traefik server, you might find yourself in the situation where you want to exclude a subdomain from being routed by traefik router to the app container.

Let’s say you have a router like this:

- "traefik.http.routers.prn.rule=HostRegexp(`^.+\\.postrocknation\\.com$`)"

Thats goanna have all the subdomains of Postrocknation.com routed to the prn service.

But what happens if you’re going to host another service which shouldn’t be routed to that container?

obviously if you’re hosting this subdomain externally, you can just point that subdomain via your DNS settings to the destination server, but if you’re using the same server as your app server for this purpose, you need to let traefik know that subdomain shouldn’t be routed to prn service.

In order to do this we should use priorities, for example, lets say we have a mail server which should be hosted on mail.postrocknation.com, this is how we’d define the traefik labels:

- traefik.http.routers.mailserver.rule=Host(`mail.postrocknation.com`)
- ...
- traefik.http.routers.mailerver.priority=10

If a service has traefik configured with higher priority, traefik will route an incoming host request to that service, even if in a previous configuration it is set to capture a wildcard set of hosts.

I hope this helps you!

Leave a Reply