【问题标题】:traefik proxy - redirect to a site without proxyingtraefik 代理 - 无需代理即可重定向到站点
【发布时间】:2021-12-15 21:40:23
【问题描述】:

您好,我希望我的 traefik 代理将用户重定向到远程 url,而不做代理。这是可行的吗?我在我的 yaml 中使用中间件

traefik.yml

version: '3'
services:
  reverse-proxy:
    # The official v2 Traefik docker image
    image: traefik:v2.5
    # Enables the web UI and tells Traefik to listen to docker
    command:
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--providers.file=true"
      - "--providers.file.filename=/etc/traefik/rules.yml"
      - "--entrypoints.web.address=:80"
    ports:
      # The HTTP port
      - "8081:8081"
      - 80:80
      # The Web UI (enabled by --api.insecure=true)
      - "8080:8080"
    volumes:
      # So that Traefik can listen to the Docker events
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik_rules.yml:/etc/traefik/rules.yml
  whoami:
    # A container that exposes an API to show its IP address
    image: traefik/whoami
    labels:
      - "traefik.enable=true"
      - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=http://localhost/(.*)"
      - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=https://www.google.com/$${1}"
      - "traefik.http.middlewares.test-redirectregex.redirectregex.permanent=true"
      - "traefik.http.routers.my-route-2.middlewares=test-redirectregex@docker"

traefik-rules.yml

http:
  routers:
    my-route-2:
      rule: "Host(`localhost`)"
      service: my-keycloak-server-2
  services:
    my-keycloak-server-2:
      loadBalancer:
        servers:
          - url: "https://www.google.com"

我可以在我的 traefik 仪表板中看到中间件“test-redirectregex”已创建并附加到我的服务,但是当我访问“http://localhost”时它没有重定向我 - 我的浏览器 url 仍然是 localhost 而不是google.com。这个用例甚至可以由 Traefik Proxy 处理吗?提前致谢。

【问题讨论】:

    标签: traefik


    【解决方案1】:

    traefik-rules.yml

       http:
          routers:
            my-route:
              rule: "Host(`127.0.0.1`)"
              service: my-keycloak-server
            my-route-2:
              middlewares:
                - "test-redirectregex"
              rule: "Host(`localhost`)"
              service: my-keycloak-server-2
          services:
            my-keycloak-server:
              loadBalancer:
                servers:
                  - url: "https://www.demo2.com"
            my-keycloak-server-2:
              loadBalancer:
                servers:
                  - url: "https://www.demo.com"
          middlewares:
            test-redirectregex:
              redirectRegex:
                regex: "^http://localhost/(.*)"
                replacement: "https://www.demo2.com/${1}"
                permanent: true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 2019-05-23
      • 2021-04-14
      • 2021-11-14
      相关资源
      最近更新 更多