Traefik Priority

Traefik Priority
Photo by Tsvetoslav Hristov / Unsplash

I just needed a "new" feature of Traefik. It is not actually a new feature of Traefik just new to me.

The issue

I needed to add a second domain name to one of my sites so that Let's Encrypt would generate a certificate for that domain, so I modified the router rule.

traefik.http.routers.myservice1.rule=Host('site1.domain.com') || Host('site2.domain.com')

The issue was that as soon as Traefik picked up this rule another site/service stopped working. The rule for this service was:

traefik.http.routers.myservice2.rule=Host(site1.domain.com) && PathPrefix(`/myfolder)

By default traefik prioritizes router rules by the length of the rule. Longer rules take precedence over shorter rules. When I updated the first router rule, this made the rule longer than the second router rule and the first rule took precedence.

Calls to site1.domain.com/myfolder would match the first rule and the service hosted at site1.domain.com/myfolder would not work.

The Fix

Traefik has a feature which lets you set the priority of a rule. So I added:

traefik.http.routers.myservice1.priority: 100

to my docker-compose.yml file. This lowered the priority of the longer rule so that calls to myservice2 would match first.