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:
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
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
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
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