docker - 使用 docker 使用 traefik v2 转发 url
问题描述
我想用 traefik(version 2) 转发 url,比如如果我点击 URL http://localhost/1然后它被转发到http://localhost:8081/1。
我尝试了几种配置,但没有一个可以工作。请在下面找到我已经完成但没有成功的配置。
version: "3.3"
services:
traefik:
image: "traefik:v2.0.0-rc3"
container_name: "traefik"
command:
- "--log.level=DEBUG"
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
ports:
- "80:80"
- "8080:8080"
- "8081:8081"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
whoami:
#image: "containous/whoami"
#container_name: "simple-service"
#command:
# - "--port=8081"
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`localhost`)"
- "traefik.http.routers.whoami.entrypoints=web"
- "traefik.http.services.whoami.loadbalancer.server.port=8081"
解决方案
我认为你不需要转发,因为你已经定义了负载均衡器端口,添加PathPrefix
到你的 whoami 路由器规则应该可以工作。像这样
- "traefik.http.routers.whoami.rule=Host(`localhost`) && PathPrefix(`/1`)"
如果你确实需要转发请求,你可以使用 middlewares.redirectregex。
- "traefik.http.routers.whoami.middlewares=whoami-redirectregex"
- "traefik.http.middlewares.whoami-redirectregex.redirectregex.regex=^http://localhost/(.*)"
- "traefik.http.middlewares.whoami-redirectregex.redirectregex.replacement=http://localhost:8081/$${1}"
供参考。中间件文档
推荐阅读
- spring-boot - 如何在 Spring Boot Thymeleaf 列表中使用 Bootstrap 模式?
- powershell - 如何在 Azure CLI 任务中设置 put 变量
- gitlab - 使用 Semantic Versioning 或 Lerna Publish 从 CI/CD 部署时如何确保 Master 和 Dev 分支保持同步
- delphi - SVG 颜色问题:RED 颜色显示为 GREY
- javascript - 将一个数组排序为尽可能接近另一个数组
- bash - 如何将 StartExplorer Eclipse 插件指向 Bash?
- python - Plotly 表达 imshow 滑块崩溃
- php - json 数组的格式不正确。拉拉维尔
- vba - 选择范围对象中每个单元格的行 (VBA)
- html - 我的 CSS 列不相邻