# How to define routers priority or define a service when no one router meets the request?

**URL:** <https://community.traefik.io/t/how-to-define-routers-priority-or-define-a-service-when-no-one-router-meets-the-request/1568>\
**Category:** Traefik v2\
**Tags:** middleware, docker\
**Created:** [September 12, 2019, 1:26pm UTC](https://community.traefik.io/t/how-to-define-routers-priority-or-define-a-service-when-no-one-router-meets-the-request/1568 "2019-09-12T13:26:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SrMouraSilva](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/srmourasilva/32/667_2.png) [@SrMouraSilva](https://community.traefik.io/u/SrMouraSilva)\
**Post date:** [September 12, 2019, 1:26pm UTC](https://community.traefik.io/t/how-to-define-routers-priority-or-define-a-service-when-no-one-router-meets-the-request/1568/1 "2019-09-12T13:26:15Z")

</div>

TLDR:

- How to set priority between different services?
- How do I set middleware errors to respond to 404 errors that do not respond to any defined routes?

Hello,

In my project, I use traefik to define access to different containers through the subdomain. I worked out a strategy to stop docker containers on weekends and restart they when were requested during the week.

In v1, I had developed with a [strategy similar to that](https://www.techjunktrunk.com/docker/2017/11/03/traefik-default-server-catch-all/):

There are one container with minimum priority that responds all subdomains requests that are not used and eventually restarts a stopped container if is one associated with the subdomain

```auto
labels:
  - traefik.nao-disponivel.frontend.rule=HostRegexp(`{subdomain:.+}.${DOMINIO}`)
  - traefik.nao-disponivel.frontend.priority=1

```

In v2, I try to create a router to responds all subdomains

```auto
labels:
  # Middleware Redirect https
  - traefik.http.middlewares.redirect_https.redirectregex.regex=^http://(.*)
  - traefik.http.middlewares.redirect_https.redirectregex.replacement=https://$${1}

  # Http to https
  - traefik.http.routers.ambientes_nao_disponivel_http.rule=HostRegexp(`{subdomain:.+}.localhost`)
  - traefik.http.routers.ambientes_nao_disponivel_http.middlewares=redirect_https

  # Https
  - traefik.http.routers.ambientes_nao_disponivel_https.rule=HostRegexp(`{subdomain:.+}.localhost`)
  - traefik.http.routers.ambientes_nao_disponivel_https.tls=true

```

I tested and verified that the rule has higher priority than any container that defined a subdomain.

**Question 1** : I didn't find in the documentation how to set a priority on the router.

**Question 2** : A workaround would be to set a default service for requests that do not fit into any router defined for traefik. Is there this option?

I tried to define an error middleware in the root domain (localhost), but got no results.

> **Configuration**
>
> ```auto
> version: '3.7'
> 
> services:
> traefik:
> image: traefik:v2.0.0-rc3-alpine
> command: >
> --api=true
> --api.dashboard=true
> --api.insecure=true
> --providers.docker=true
> --entryPoints.web.address=:80
> --entryPoints.web-secure.address=:443
> --log.level="DEBUG"
> restart: always
> 
> ports:
> - "80:80" # The HTTP port
> - "443:443" # The HTTPS port
> labels:
> # Traefik dashboard in subdomain: traefik.localhost 
> - traefik.http.routers.traefik_http.rule=Host(`traefik.localhost`)
> - traefik.http.routers.traefik_http.middlewares=redirect_https
> - traefik.http.routers.traefik_http.entryPoints=web
> 
> - traefik.http.routers.traefik_https.rule=Host(`traefik.localhost`)
> - traefik.http.routers.traefik_https.tls=true
> - traefik.http.routers.traefik_https.entryPoints=web-secure
> 
> - traefik.http.services.traefik_https.loadbalancer.server.port=8080
> 
> # Middlewares
> ## 404 -> Custom service
> - traefik.http.middlewares.404_not_found.errors.status=404
> - traefik.http.middlewares.404_not_found.errors.service=404_not_found_https
> - traefik.http.middlewares.404_not_found.errors.query=/
> ## Redirect https
> - traefik.http.middlewares.redirect_https.redirectregex.regex=^http://(.*)
> - traefik.http.middlewares.redirect_https.redirectregex.replacement=https://$${1}
> 
> volumes:
> - /var/run/docker.sock:/var/run/docker.sock 
> networks:
> - ambientes
> 
> ambientes-cliente:
> build:
> context: .
> dockerfile: ambientes-cliente.Dockerfile
> command: npm run ${AMBIENTE_EXECUCAO}
> restart: always
> 
> depends_on:
> - traefik
> 
> labels:
> - traefik.http.routers.ambientes_cliente_http.rule=Host(`localhost`)
> - traefik.http.routers.ambientes_cliente_http.middlewares=redirect_https
> - traefik.http.routers.ambientes_cliente_http.entryPoints=web
> 
> - traefik.http.routers.ambientes_cliente_https.rule=Host(`localhost`)
> - traefik.http.routers.ambientes_cliente_https.tls=true
> - traefik.http.routers.ambientes_cliente_https.middlewares=404_not_found
> - traefik.http.routers.ambientes_cliente_https.entryPoints=web-secure
> 
> - traefik.http.services.ambientes_cliente_https.loadbalancer.server.port=8100
> networks:
> - ambientes
> volumes:
> - ...
> 
> ambiente-nao-disponivel:
> depends_on:
> - traefik
> image: python:3-alpine
> command: sh setup.sh
> restart: always
> 
> labels:
> - traefik.http.routers.404_not_found_http.rule=HostRegexp(`404.localhost`)
> - traefik.http.routers.404_not_found_http.middlewares=redirect_https
> 
> - traefik.http.routers.404_not_found_https.rule=HostRegexp(`404.localhost`)
> - traefik.http.routers.404_not_found_https.tls=true
> 
> - traefik.http.services.404_not_found_https.loadbalancer.server.port=5000
> 
> networks:
> - ambientes
> volumes:
> - ...
> 
> networks:
> ambientes:
> name: ambientes
> driver: bridge
> 
> ```

---

<div class="post-metadata">

**Author:** ![SrMouraSilva](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/srmourasilva/32/667_2.png) [@SrMouraSilva](https://community.traefik.io/u/SrMouraSilva)\
**Post date:** [September 13, 2019, 1:52pm UTC](https://community.traefik.io/t/how-to-define-routers-priority-or-define-a-service-when-no-one-router-meets-the-request/1568/2 "2019-09-13T13:52:43Z")

</div>

Hello, the solution is the priority router param:

```auto
- traefik.http.routers.ambientes_nao_disponivel_http.priority=1

```

This has already implemented, but no documented:

> <https://github.com/containous/traefik/issues/5301#issuecomment-528429051>

[https://docs.traefik.io/v2.0/reference/dynamic-configuration/docker/](https://docs.traefik.io/v2.0/reference/dynamic-configuration/docker/)
