簡體   English   中英

Traefik 從一台主機重定向到另一台主機

[英]Traefik redirect from one host to another

我們決定從子域結構轉移到一個帶有路徑前綴的根域,但是我們在互聯網上得到了許多舊的 URL。 那么有什么方法可以添加從舊 URL 到新 URL 的重定向? 例如,我們將子域 test.example.com 切換到 example.com/test,我可以使用traefik.frontend.rule: Host:example.com;PathPrefixStrip:/test -swarm YAML 文件traefik.frontend.rule: Host:example.com;PathPrefixStrip:/test的字符串正確訪問站點,但是當我嘗試添加到 Traefik 配置重定向時,如:

[http.middlewares]
  [http.middlewares.test-redirectregex.redirectRegex]
    regex = "^https://(*).example.com/)"
    replacement = "^https://example.com/${1}"

Traefik 說它不知道將這個請求轉發到哪里 如果我想添加: traefik.frontend.rule: Host:test.example.com,example.com;PathPrefixStrip:/test Traefik 向兩個主機添加前綴. 有沒有辦法在不添加第二個反向代理的情況下解決這個問題?

假設您使用的是 Traefik 2.1,您可以將以下中間件用於 Traefik

[http.middlewares]
  [http.middlewares.blog-redirect.redirectRegex]
    regex = "^(https?://)(.*).example.com/(.*)$"
    replacement = "${1}example.com/${2}/${3}"
    permanent = true

激活上述中間件的重要步驟是在相應的路由器和服務上添加以下標簽。 例如,如果您是一個blog服務,並為它定義了一個blog路由器,那么您需要將下表添加到服務中

traefik.http.routers.blog.middlewares=blog-redirect

此外,您的路由規則應類似於以下規則,以便能夠處理兩個域(或者您為每個服務定義多個路由)

- traefik.http.routers.blog.rule=Host(`example.com`) && Path(`/test`) || Host(`api.example.com``) 

在這篇文章中,您可以找到有關流量和重定向的更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM