Search code examples
dockerdocker-composetraefik

Combining Traefik frontend redirect replacement with PathPrefixStrip


I am encountering some conflicts between the traefik.frontend.redirect and PathPrefixStrip. the docker-compose.yml file below always routes www.mysite.nl/adminer to the wordpress container. If I omit the redirect rules it works correctly and i get routed to the adminer instance. How can I make these rules work together?

Drilled down docker-comose.yml:

version: '3'
services:
  wordpress:
    image: wordpress:latest 
    restart: $RESTART
    container_name: ${COMPOSE_PROJECT_NAME}_wp
    depends_on:
      - mysql
    networks:
      - web
    labels:
      - 'traefik.backend=$COMPOSE_PROJECT_NAME'
      - 'traefik.entrypoint=https'
      - 'traefik.enable=true'
      - 'traefik.frontend.rule=Host:mysite.nl, www.mysite.nl, cdn.mysite.net'
       # omitting these rules make the adminer instance reachable
      - 'traefik.frontend.redirect.regex=^https?://mysite.nl/(.*)'
      - 'traefik.frontend.redirect.replacement=https://www.mysite.nl/$${1}'
  mysql:
    image: mysql:latest
    restart: $RESTART
    container_name: ${COMPOSE_PROJECT_NAME}_db
    networks:
      - web
  adminer:
    image: adminer:4.6.2
    restart: $RESTART
    container_name: ${COMPOSE_PROJECT_NAME}
    depends_on:
      - mysql
    networks:
      - web
    labels:
      - 'traefik.backend=${COMPOSE_PROJECT_NAME}_adminer'
      - 'traefik.entrypoint=https'
      - 'traefik.enable=true'
      - 'traefik.frontend.rule=Host:www.mysite.nl;PathPrefixStrip:/adminer'

networks:
  web:
    external:
      name: traefik_${COMPOSE_PROJECT_NAME}_web

Solution

  • The issue you are running into is due to overlapping rules.

    The request www.mysite.nl/adminer

    Matches both: traefik.frontend.rule=Host:mysite.nl, www.mysite.nl, cdn.mysite.net and traefik.frontend.rule=Host:www.mysite.nl;PathPrefixStrip:/adminer

    Therefore Traefik does not know which to route requests to.

    Use the traefik.frontend.priority label to set an order for matching (from https://docs.traefik.io/configuration/backends/docker/#on-containers)