I would like to have a very simple traefik config that would have a default route (which works fine) but also a fallback route incase any of the services are down or the subdomain is used for something else.
The issue is that with the config below Hostregexp takes prority over the defaultroute but I would like the exact opposite to happen. Is there some way to make the regexp shorter if that matter? Help appreciated.
version: "3.7"
services:
tra:
image: traefik:latest
container_name: traefik
restart: unless-stopped
command:
- "--api.dashboard=true"
- "--ping=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=true" # no labels needed in our docker compose files
- "--providers.docker.defaultrule=Host(`{{ index .Labels \"com.docker.compose.service\"}}.$domain`)" # service name = cname
- "--entrypoints.web.address=:22280" # behind nginx until everything works
- "--entrypoints.web.http.redirections.entryPoint.to=websecure"
- "--entrypoints.web.http.redirections.entryPoint.scheme=https"
- "--entrypoints.web.http.redirections.entrypoint.permanent=true"
- "--entrypoints.websecure.address=:22443"
- "--entrypoints.websecure.http.tls.domains.main=$domain"
- "--entrypoints.websecure.http.middlewares=test-auth" # test only
- "--entrypoints.traefik.address=:22288" # no conflict for dashboard port
- "--accesslog=true"
- "--accesslog.filepath=/etc/traefik/access.log"
- "--log.filePath=/etc/traefik/traefik.log"
- "--log.level=INFO"
- "--providers.file.filename=/etc/traefik/dynamic.yaml" # tls config and manual routes
- "--providers.file.watch=true"
- "--pilot.token=$pilot"
labels:
- "traefik.http.routers.api.service=api@internal" # dashboard service
- "traefik.http.services.api-svc.loadbalancer.server.port=22288" # !!! without this, the dashboard will return 404
- "traefik.http.middlewares.test-auth.basicauth.users=test:$$apr1$$oNcYrRwI$$0/NZI6oT2pwDzPMR8pkXW." # test only
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- "./:/etc/traefik"
network_mode: host # traefik will get access to all host routing
public:
image: containous/whoami
container_name: whoami
restart: unless-stopped
labels:
- "traefik.http.routers.public.rule=HostRegexp(`{s:[a-z]+}.$domain`)"
Hi @kevinpollet! Thanks for the reply, traefik is great but the journey to replace a 10+ year and 1k+ line nginx setup is not easy
The issue with adding a priority is as soon as one is added witha a label, the "fallback route" will give 404 instead of serving the websecure endpoint in the above config. Should more labels be added to the fallback service? (public)
I think your fallback router should have the smallest priority. By doing that, your fallback router will always be evaluated lastly. The following config should work:
Hey, I tried different values, as soon as a priority number is added it seems to invalidate the rule or something else because all requests that are not matched by the defaultrule will get 404. It's easily replicatable with running the above compose and trying to go to public.$domain
I tried your example and everything is working fine on my side (I used the following docker-compose command: domain=local docker-compose up). The following example is simpler and works also fine on my side: