我开发了几个 docker-ized 全栈 web 应用程序,我试图将请求路由到使用 Traefik。我想通过 docker-compose 标签利用动态配置。我想应用 stripPrefix 中间件选项,以便我可以使用相同的应用程序路由,就好像每个应用程序都在根处提供服务一样。但是,一旦应用这些规则,就会产生 504 网关超时响应。
这是我的设置:
global
Traefik 容器运行所在的 docker 网络 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
加重的部分是,当我注释掉
middlewares
时标签,它运行得很好,但找不到匹配的 URL 模式。在此之前,我使用
whoami
测试了我的方法。 Traefik Quickstart Tutorial 中定义的容器:# 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 错误。
如果有人有任何线索,我将衷心感谢。
最佳答案
有一个issue带前缀但不以“/”结尾。
像这样测试你的配置:
- "traefik.http.routers.whoami.rule=Host(`localhost`) && (PathPrefix(`//whoami/`) || PathPrefix(`/portainer`))"
- "traefik.http.middlewares.strip-who.stripprefix.prefixes=/whoami"
关于docker - 将 Traefik StripPrefix 中间件添加到 docker-compose 标签导致 504,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58492175/