Skip to content
欢迎扫码关注公众号

TKE Nginx Ingress Controller 路径跳转

通过腾讯云容器服务 TKE 创建的 Nginx Ingress Controller 类型的 Ingresspath 指定为默认的 / 时可以正常访问站点,指定了二级路径时则无法访问。

此时清单文件大概如下:

yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx-ingress-test
    kubernetes.io/ingress.rule-mix: "true"
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
  - http:
      paths:
      - backend:
          serviceName: svc-test
          servicePort: 80
        path: /test/
        pathType: ImplementationSpecific

未指定 host 时,相当于是 Nginx Ingress 的 IP 地址。

官方文档里没有看到提供非默认路径(/)的示例。

找了下 nginx ingress controller 的资料,发现是由于缺少 rewrite-target 配置导致的。

  1. 添加 nginx.ingress.kubernetes.io/rewrite-target: /$1 声明;
  2. path 中添加对应的通配符 /test/(.*)

修改后清单文件如下:

yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx-ingress-test
    kubernetes.io/ingress.rule-mix: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
  - http:
      paths:
      - backend:
          serviceName: svc-test
          servicePort: 80
        path: /test/(.*)
        pathType: ImplementationSpecific

附 1. 如果 path 中未指定通配符 /test/ 会导致如下错误:

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

附 2. 还可以使用 /test(/|$)(.*) 路径匹配以支持 /test 路径的跳转。

不过如果前端项目中 css/js 资源使用的 ./ 这样的相对路径,会导致这些资源文件路由到域名的根路径下,此时不可以采用这种配置方式。

yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - http:
      paths:
      - backend:
          serviceName: svc-test
          servicePort: 80
        path: /test(/|$)(.*)
        pathType: ImplementationSpecific

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.