简体   繁体   中英

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

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 )

With help from Daniel Tomcej i came to the following working docker-compose.yml . You have to set the priority explicitly on both containers.

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'
      - 'traefik.frontend.redirect.regex=^https?://mysite.nl/(.*)'
      - 'traefik.frontend.redirect.replacement=https://www.mysite.nl/$${1}'
      - 'traefik.frontend.priority=5'
  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'
      - 'traefik.frontend.priority=20'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM