正则表达式替换 Traefik 给出 404

Regex replacement Traefik gives 404

我在 Traefik 代理后面有一个 docker。我尝试将所有流量(cdn 除外)重定向到 https://www.mysite.nl。我配置了一个正则表达式,如下所示。但不幸的是它不起作用。我在所有主机上收到 404。我错过了什么?

新设置

在这里你可以测试正则表达式:https://regex101.com/r/mwt573/2

- traefik.frontend.priority=5
- traefik.frontend.rule=Host:mysite.nl, www.mysite.nl, mysite.com, www.mysite.com, cdn.mysite.net
- traefik.frontend.redirect.regex=^https?://(?:www.)?mysite\.(?:nl|com)(.*)
- traefik.frontend.redirect.replacement=https://www.mysite.nl$ 

旧设置

此设置对我有用,但没有将 .com 域重定向到 .nl。它还在每个 url.

之后放置一个 /
- traefik.frontend.priority=5
- traefik.frontend.rule=Host:mysite.nl, www.mysite.nl, cdn.mysite.net, mysite.com, www.mysite.com
- traefik.frontend.redirect.regex=^https?://mysite.nl/(.*)
- frontend.redirect.replacement=https://www.mysite.nl/$

我不知道为什么。但是删除非捕获组并取消转义点对我有用。

工作示例:

 - traefik.frontend.priority=5
 - traefik.frontend.rule=Host:mysite.nl, www.mysite.nl, cdn.mysite.net, mysite.com, www.mysite.com
 - traefik.frontend.redirect.regex=^https?://(www.)?mysite(.nl|.com)(.*)
 - traefik.frontend.redirect.replacement=https://www.mysite.nl$

我的 docker 运行 命令(可执行脚本文件)和 regex/replacement 标签让我苦恼了一段时间。这就是我将它用于 www 到非 www url 重写:

  1. 请注意 GO 的正则表达式库略有不同 - GO's regex lib here
  2. 可以在"Labels"下用docker inspect命令查看 属性,如果你的正则表达式被 运行 脚本正确捕获。
  3. 双引号和单引号是个问题,不知何故双引号对我不起作用(可能是 shell 去掉了双引号,没有双引号 traefik 就无法计算标签)...我怎么过在表达式周围使用单引号,没有任何其他问题。
  4. 在您的示例中,你们都使用句点(点),根据定义,这是一个通配符。我试图转义它们,但 GO 的正则表达式库似乎错过了转义句点字符的机会,因此我使用了字符集 [.](你不必转义句点)。
  5. 在 GO 中访问捕获的组是由 ${} 完成的。但是我在他们的正则表达式文档中找不到它。我从 try & error 那里得到了这些信息。

我的 www 到非 www 的结果是:

-l traefik.frontend.redirect.regex='^https?://(?:www[.])(.*)'

-l traefik.frontend.redirect.replacement='https://'

您声称的解决方案应该可以正常工作,我稍微重写了它:

-l traefik.frontend.redirect.regex='^https?://(?:www[.])?mysite[.](?:nl|com)(.*)'

-l traefik.frontend.redirect.replacement='https://www.mysite.nl'