将 Traefik StripPrefix 中间件添加到 docker-compose 标签导致 504

Rya*_*anM 5 middleware nginx docker docker-compose traefik

我开发了几个 docker-ized 全栈 web 应用程序,我试图将请求路由到使用 Traefik。我想通过 docker-compose 标签利用动态配置。我想应用 stripPrefix 中间件选项,以便我可以使用相同的应用程序路由,就好像每个应用程序都在根处提供服务一样。但是,一旦应用这些规则,就会产生 504 网关超时响应。

这是我的设置:

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

以下是docker-compose.yml违规 NGINX 容器的定义:

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
Run Code Online (Sandbox Code Playgroud)

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

在此之前,我使用Traefik 快速入门教程中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
Run Code Online (Sandbox Code Playgroud)

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

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

如果有人有任何线索,我将衷心感谢。

igo*_*shi 8

总结我对类似问题的解决方案

  my-service:
    image: some/image
    ports: # no need to expose port, just to show that service listens on 8090
      - target: 8090
        published: 8091
        mode: host
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.myservice-router.rule=PathPrefix(`/myservice/`)"
      - "traefik.http.routers.myservice-router.service=my-service"
      - "traefik.http.services.myservice-service.loadbalancer.server.port=8090"
      - "traefik.http.middlewares.myservice-strip.stripprefix.prefixes=/myservice"
      - "traefik.http.middlewares.myservice-strip.stripprefix.forceslash=false"
      - "traefik.http.routers.myservice-router.middlewares=myservice-strip"
Run Code Online (Sandbox Code Playgroud)

现在详情

路由规则

...重定向所有以/myservice/开头的路径的调用

- "traefik.http.routers.myservice-router.rule=PathPrefix(`/myservice/`)"
Run Code Online (Sandbox Code Playgroud)

...为我的服务提供服务

- "traefik.http.routers.myservice-router.service=my-service"``
Run Code Online (Sandbox Code Playgroud)

...到端口 8090

- "traefik.http.services.myservice-service.loadbalancer.server.port=8090"
Run Code Online (Sandbox Code Playgroud)

...并使用myservice-strip中间件

- "traefik.http.routers.myservice-router.middlewares=myservice-strip"
Run Code Online (Sandbox Code Playgroud)

...这个中间件将从路径中剥离/myservice

- "traefik.http.middlewares.myservice-strip.stripprefix.prefixes=/myservice"
Run Code Online (Sandbox Code Playgroud)

...并且如果路径变为空字符串,则不会添加尾部斜杠 (/)

- "traefik.http.middlewares.myservice-strip.stripprefix.forceslash=false"
Run Code Online (Sandbox Code Playgroud)


小智 1

不以“/”结尾的前缀存在问题。像这样测试你的配置:

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

  • 是**|| PathPrefix(`/portainer`)** 必要吗? (3认同)