gpt4 book ai didi

docker - 使用Traefik将正则表达式将URL重定向到另一个URL

转载 作者:行者123 更新时间:2023-12-02 20:43:18 26 4
gpt4 key购买 nike

我想通过traefik将一个URL重定向到另一个URL。

我正在使用docker进行此配置,以下是我的docker-compose.yml文件

version: '3'

services:
reverse-proxy:
# The official v2.0 Traefik docker image
image: containous/whoami
container_name: "whoami_cont"
# Enables the web UI and tells Traefik to listen to docker
ports:
# The HTTP port
- 80:80
# The Web UI (enabled by --api.insecure=true)
- 8080:8080
labels:
- traefik.http.middlewares.test-replacepathregex.replacepathregex.regex=^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]
- traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement=http://localhost:8081/4

然后我正在成功运行docker-compose命令。然后,我将命中任何与正则表达式模式匹配的URL,但该URL不会重定向到另一个URL-按照配置 http://localhost:8081/4
我正在使用traefik 2.0版

请让我知道是否缺少任何配置。

最佳答案

official example之后,似乎您的配置缺少实际的Traefik实例:

  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"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"

将Traefik添加到您的docker-compose文件并定义 middleware之后,您应该将中间件附加到路由器:
      - "traefik.http.routers.whoami.middlewares=test-replacepathregex"
- "traefik.http.middlewares.test-replacepathregex.replacepathregex.regex=^/foo/(.*)"
- "traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement=/bar/$$1"

/foo/123替换为 /bar/123的完整且有效(但简化)的示例:
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"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"

whoami:
image: "containous/whoami"
container_name: "simple-service"
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`localhost`)"
- "traefik.http.routers.whoami.entrypoints=web"
- "traefik.http.routers.whoami.middlewares=test-replacepathregex"
- "traefik.http.middlewares.test-replacepathregex.replacepathregex.regex=^/foo/(.*)"
- "traefik.http.middlewares.test-replacepathregex.replacepathregex.replacement=/bar/$$1"

关于docker - 使用Traefik将正则表达式将URL重定向到另一个URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59646283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com