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

Redirect URL to another URL by regex with Traefik

我想通过 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-编写命令成功。然后我将点击匹配正则表达式模式但 URL 未根据配置重定向到另一个 URL - http://localhost:8081/4 的任何 URL。 我正在使用 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/$"

/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/$"

如果您使用 Ingressroute,这将非常简单。假设我想使用 traefik

example.com 重定向到 app.example.com

首先要创建一个中间件:

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: example-redirectregex
spec:
  redirectRegex:
    regex: ^https://example.com/(.*)
    replacement: https://app.example.com/

如果您已经有一个 Ingressroute,那么您可以添加一个额外的规则,将 example.com 指向 app.example.com 使用的相同后端服务,并使用我们在上面创建的中间件

- kind: Rule
    match: Host(`example.com`)
    middlewares:
    - name: example-redirectregex
      namespace: traefik
    services:
    - kind: Service
      name: master-prod
      namespace: app-web
      port: 80

所以,最后您的 Ingressroute 应该如下所示:

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  generation: 3
  labels:
    app: app-web
    product: example.com
  name: example-com
  namespace: traefik
spec:
  entryPoints:
  - websecure
  routes:
  - kind: Rule
    match: Host(`example.com`)
    middlewares:
    - name: example-redirectregex
      namespace: traefik
    services:
    - kind: Service
      name: example
      namespace: app-web
      port: 80
  - kind: Rule
    match: Host(`app.example.com`)
    services:
    - kind: Service
      name: example
      namespace: app-web
      port: 80
  tls:
    secretName: cert-example-com

现在,如果您浏览 example.com,它应该会将您重定向到 app.example.com