简体   繁体   English

Traefik - 中间件 redirectRegex 不起作用

[英]Traefik - Middleware redirectRegex doesn't work

I have a simple website running in my Kube.netes cluster and exposed to the Inte.net using Traefik.我有一个简单的网站在我的 Kube.netes 集群中运行,并使用 Traefik 暴露给 Inte.net。 My Ingress object looks like this (the only things I've changed here are the name and domain names):我的 Ingress object 看起来像这样(我在这里唯一改变的是名称和域名):

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    cert-manager.io/cluster-issuer: letsencrypt-staging
    kubernetes.io/ingress.class: traefik
  name: my-cool-website
  namespace: default
spec:
  rules:
  - host: my-cool-website.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: my-cool-website
            port:
              number: 80
  - host: www.my-cool-website.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: my-cool-website
            port:
              number: 80
  tls:
  - hosts:
    - my-cool-website.com
    - www.my-cool-website.com
    secretName: my-cool-website-tls

This works .行得通 It allows me to access the site either from my-cool-website.com or from www.my-cool-website.com .它允许我从my-cool-website.comwww.my-cool-website.com访问该站点。 But what I'd like to have happen is that if someone visits the former, that Traefik automatically redirects them to the latter.但我希望发生的是,如果有人访问前者,Traefik 会自动将他们重定向到后者。 I found a couple of guides online that recommended creating a Traefik middleware, so I did just that, but unfortunately it doesn't work as intended.我在网上找到了一些建议创建 Traefik 中间件的指南,所以我就这样做了,但不幸的是它没有按预期工作。 Here is my middleware definition:这是我的中间件定义:

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: my-cool-website-force-www
spec:
  redirectRegex:
    regex: ^https?://my-cool-website.com/(.*)
    replacement: https://www.my-cool-website.com/${1}

And then I add the following annotation back to the ingress object:然后我将以下注释添加回入口 object:

traefik.ingress.kubernetes.io/router.middlewares: my-cool-website-force-www

But as soon as I do that, it breaks my web app.但是一旦我这样做,它就会破坏我的 web 应用程序。 By that I mean, when that annotation is applied, instead of serving my website, I start seeing a generic nginx page that looks like this when I try to access the domain (and also it does not do the redirect):我的意思是,当应用该注释时,我并没有为我的网站提供服务,而是开始看到一个通用的 nginx 页面,当我尝试访问该域时(它也没有进行重定向):

Hello World 通用 Traefik 响应

I have to assume this Hello World page is being served by Traefik as some sort of generic default page, as it definitely does not originate from my pod.我必须假设这个 Hello World 页面由 Traefik 作为某种通用默认页面提供,因为它绝对不是来自我的 pod。 So that tells me something about this middleware (or perhaps how I'm calling it with the annotation) isn't working.所以这告诉我一些关于这个中间件的事情(或者也许我是如何用注释调用它的)不工作。 What am I missing?我错过了什么?

I figured it out, by port-forwarding to the Traefik Dashboard and looking at the service there.我想通了,通过端口转发到 Traefik 仪表板并查看那里的服务。 It was showing an error for the middleware not found.它显示找不到中间件的错误。 I then clicked over to the middlewares and realized that they end up with a longer canonical name.然后我点击中间件并意识到它们最终有一个更长的规范名称。 So in my annotation I had to change the reference from my-cool-website-force-www to default-my-cool-website-force-www@kube.netescrd and then everything worked.因此,在我的注释中,我不得不将引用从my-cool-website-force-www更改为default-my-cool-website-force-www@kube.netescrd ,然后一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM