Traefik 重定向

发布于 2025-01-11 10:40:09 字数 1410 浏览 0 评论 0原文

我正在尝试在 Traefik 的文件提供程序中实现基本重定向。我会 如下所示:

http://example.com/foobar      ->  https://foobar.example.com
http://www.example.com/foobar  ->  https://foobar.example.com
https://example.com/foobar     ->  https://foobar.example.com
https://www.example.com/foobar ->  https://foobar.example.com

这是我的尝试:

http:
  middlewares:
    redirect-foobar:
      redirectRegex:
        permanent: true
        regex: "^https?://(www\\.)?example\\.com/foobar(/?.*)"
        replacement: "https://foobar.example.com${2}"
  routers:
    catch-foobar:
      middlewares:
        - redirect-foobar
      rule: Host(`example.com`) || Host(`www.example.com`)
      service: noop@internal

但是当我尝试时,我得到的是:

$ curl -I http://www.example.com/foobar 
HTTP/1.1 308 Permanent Redirect
Location: https://foobar.example.com
Date: Wed, 02 Mar 2022 02:12:48 GMT
Content-Length: 18
Content-Type: text/plain; charset=utf-8

# That's good =)
$ curl -I https://www.example.com/foobar
HTTP/2 404 
content-type: text/plain; charset=utf-8
x-content-type-options: nosniff
content-length: 19
date: Wed, 02 Mar 2022 02:05:22 GMT

# Does that mean the request didn't even make it to the catch-foobar router?

我做错了什么?任何帮助将不胜感激。

谢谢,

C

编辑:修复了正则表达式,但 https 情况仍然有问题

I'm trying to implement a basic redirection in Traefik's file provider. I would
like the following:

http://example.com/foobar      ->  https://foobar.example.com
http://www.example.com/foobar  ->  https://foobar.example.com
https://example.com/foobar     ->  https://foobar.example.com
https://www.example.com/foobar ->  https://foobar.example.com

Here's my attempt:

http:
  middlewares:
    redirect-foobar:
      redirectRegex:
        permanent: true
        regex: "^https?://(www\\.)?example\\.com/foobar(/?.*)"
        replacement: "https://foobar.example.com${2}"
  routers:
    catch-foobar:
      middlewares:
        - redirect-foobar
      rule: Host(`example.com`) || Host(`www.example.com`)
      service: noop@internal

But when I try it out, here's what I get:

$ curl -I http://www.example.com/foobar 
HTTP/1.1 308 Permanent Redirect
Location: https://foobar.example.com
Date: Wed, 02 Mar 2022 02:12:48 GMT
Content-Length: 18
Content-Type: text/plain; charset=utf-8

# That's good =)
$ curl -I https://www.example.com/foobar
HTTP/2 404 
content-type: text/plain; charset=utf-8
x-content-type-options: nosniff
content-length: 19
date: Wed, 02 Mar 2022 02:05:22 GMT

# Does that mean the request didn't even make it to the catch-foobar router?

What am I doing wrong? Any help would be greatly appreciated.

Thanks,

C

edit: fixed the regex, but the https case is still problematic

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱果果 2025-01-18 10:40:09

Traefik 社区论坛的人们帮助了我。原来是路由器没有tls配置。正确的配置如下所示:

http:
  middlewares:
    redirect-foobar:
      redirectRegex:
        permanent: true
        regex: "^https?://(www\\.)?example\\.com/foobar(/?.*)"
        replacement: "https://foobar.example.com${2}"
  routers:
    catch-foobar:
      middlewares:
        - redirect-foobar
      rule: Host(`example.com`) || Host(`www.example.com`)
      service: noop@internal
      tls:  # here
        certResolver: default

其中 default 是我的 traefik.yml 中定义的证书解析器。

The folks from the Traefik community forum helped me. It turned out the router did not have a tls configuration. The correct configuration looks like this:

http:
  middlewares:
    redirect-foobar:
      redirectRegex:
        permanent: true
        regex: "^https?://(www\\.)?example\\.com/foobar(/?.*)"
        replacement: "https://foobar.example.com${2}"
  routers:
    catch-foobar:
      middlewares:
        - redirect-foobar
      rule: Host(`example.com`) || Host(`www.example.com`)
      service: noop@internal
      tls:  # here
        certResolver: default

where default is a certificate resolver defined in my traefik.yml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文