将Traefik前端重定向替换与PathPrefixStrip结合使用

时间:2018-07-13 17:05:48

标签: docker docker-compose traefik

我在traefik.frontend.redirectPathPrefixStrip之间遇到了一些冲突。下面的docker-compose.yml文件始终将www.mysite.nl/adminer路由到wordpress容器。如果我忽略了重定向规则,它将正常工作,并被路由到管理员实例。如何使这些规则协同工作?

深入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

2 个答案:

答案 0 :(得分:3)

您遇到的问题是由于规则重叠造成的。

请求www.mysite.nl/adminer

同时匹配: traefik.frontend.rule=Host:mysite.nl, www.mysite.nl, cdn.mysite.nettraefik.frontend.rule=Host:www.mysite.nl;PathPrefixStrip:/adminer

因此Traefik不知道将请求路由到哪个。

使用traefik.frontend.priority标签设置匹配顺序(来自https://docs.traefik.io/configuration/backends/docker/#on-containers

答案 1 :(得分:2)

在Daniel Tomcej的帮助下,我来到了以下工作docker-compose.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'
      - '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