【问题标题】:Adding Traefik StripPrefix middleware to docker-compose labels results in 504将 Traefik StripPrefix 中间件添加到 docker-compose 标签会导致 504
【发布时间】:2020-02-17 21:21:40
【问题描述】:

我已经开发了几个 docker 化的全栈 Web 应用程序,我试图将请求路由到使用 Traefik。我想通过 docker-compose 标签利用动态配置。我想应用 stripPrefix 中间件选项,这样我就可以使用相同的应用程序路由,就好像每个应用程序都在根目录下提供服务一样。但是,一旦应用这些规则,就会导致 504 Gateway Timeout 响应。

这是我的设置:

  • Traefik 2.0.1
  • Docker 19.03.2,撰写 1.24.1
  • NGINX:最新图片
  • 运行 Traefik 容器的 global docker 网络
  • 多个多容器应用程序,每个应用程序都包含一个 NGINX Web 服务器
  • 所有应用程序都有自己的local 网络,NGINX 容器也在global 网络上。
  • 每个应用程序都配置为监听/

这是有问题的 NGINX 容器的 docker-compose.yml 定义:

nginx:
        image: nginx:latest
        container_name: "mps_nginx"
        volumes:
        - ./nginx/confs/nginx.conf:/etc/nginx/default.conf
        - ./static:/www/static
        restart: "always"
        labels:
            - traefik.http.routers.mps.rule=Host(`localhost`) && PathPrefix(`/mps`)
            - traefik.http.middlewares.strip-mps.stripprefix.prefixes=/mps
            - traefik.http.routers.mps.middlewares=strip-mps@docker
        networks:
        - default
        - mps

更糟糕的是,当我注释掉 middlewares 标签时,它运行得很好,但找不到匹配的 URL 模式。

在此之前,我使用 Traefik Quickstart Tutorial 中定义的 whoami 容器测试了我的方法:

# Test service to make sure our local docker-compose network functions
  whoami:
    image: containous/whoami
    labels:
      - traefik.http.routers.whoami.rule=Host(`localhost`) && PathPrefix(`/whoami`)
      - traefik.http.middlewares.strip-who.stripprefix.prefixes=/whoami
      - traefik.http.routers.whoami.middlewares=strip-who@docker

http://localhost/whoami 的请求返回(除其他外) GET / HTTP/1.1

这正是我希望我的路由方法适用于所有其他应用程序的方式。 Traefik 仪表板显示我注册的每个中间件都是绿色的,但我看到的只是 504 错误。

如果有人有任何线索,我将真诚地感谢它。

【问题讨论】:

    标签: docker nginx docker-compose middleware traefik


    【解决方案1】:

    有一个issue,前缀不以'/'结尾。 像这样测试你的配置:

          - "traefik.http.routers.whoami.rule=Host(`localhost`) && (PathPrefix(`//whoami/`) || PathPrefix(`/portainer`))"
          - "traefik.http.middlewares.strip-who.stripprefix.prefixes=/whoami"
    

    【讨论】:

    • || PathPrefix(/portainer) 需要吗?
    【解决方案2】:

    我有一个类似的问题,通过添加解决了

    - "traefik.http.middlewares.strip-who.stripprefix.forceslash=true"

    它确保带前缀不会同时删除正斜杠。

    您可以阅读有关forceslash 文档https://docs.traefik.io/middlewares/stripprefix/ 的更多信息

    【讨论】:

      猜你喜欢
      • 2020-05-10
      • 2019-10-05
      • 2020-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多