【问题标题】:Regex paths in kubernetes Ingress not getting created as regex location in Nginx Ingress Controllerkubernetes 入口中的正则表达式路径未在 Nginx 入口控制器中创建为正则表达式位置
【发布时间】:2020-09-07 21:57:00
【问题描述】:

我正在使用 Nginx Ingress Controller helm chart 0.6.0 和 nginx ingress controller tag 1.8.0,kubernetes 1.15。

当我在我的 Ingress 中定义一个使用正则表达式的路径时,它不起作用,因为它没有在 Nginx 控制器的配置文件位置部分中被指定为正则表达式。

我需要使用另外支持 VirtualServer 和 VirtualServerRoutes 的 nginx 入口控制器版本。

kind: Ingress
metadata:
  name: test-ingress
  namespace: ingress-test
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
  - host: test.any-xyz.com
    http:
      paths:
      - path: /quack5/(.*)
        backend:
          serviceName: test
          servicePort: 80

当我应用这个时,nginx 入口控制器已将位置设置为非正则表达式

$ kubectl -n nginx-ingress-controller exec nginx-ingress-controller-nginx-ingress-79984b9747-8pl4w -- grep -R quack /etc/nginx/conf.d
$ /etc/nginx/conf.d/ingress-test-test-ingress.conf: location /quack5/(.*) {

我希望看到位置 ~* /quack5/(.*) {

【问题讨论】:

    标签: kubernetes-ingress nginx-ingress


    【解决方案1】:

    尝试在你的 Nginx Ingress Controller 定义文件中添加nginx.ingress.kubernetes.io/rewrite-target 注解。

    删除nginx.ingress.kubernetes.io/use-regex: "true"注解。

    编辑:

    我认为问题可能出在 buildLocation 函数中(&& location.Rewrite.Target != path)。如果路径和nginx.ingress.kubernetes.io/rewrite-target 相等,则 nginx 位置不以 ~* 为前缀,并作为正则表达式处理。

        if len(location.Rewrite.Target) > 0 && location.Rewrite.Target != path {
            if path == slash {
                return fmt.Sprintf("~* %s", path)
            }
            // baseuri regex will parse basename from the given location
            baseuri := `(?<baseuri>.*)`
            if !strings.HasSuffix(path, slash) {
                // Not treat the slash after "location path" as a part of baseuri
                baseuri = fmt.Sprintf(`\/?%s`, baseuri)
            }
            return fmt.Sprintf(`~* ^%s%s`, path, baseuri)
        }
    
    return path
    

    我会建议一个指令来指定 spec.rules.http.paths.path 应该最终作为一个正则表达式位置,不管nginx.ingress.kubernetes.io/rewrite-target 可能是什么(即使它没有指定)。目前,让位置受到正则表达式威胁(添加 ~*)的唯一方法是定义一个不同于 spec.rules.http.paths.path 的 nginx.ingress.kubernetes.io/rewrite-target

    请看:ingress-regexingress-path-matchingpath-not-threated-as-regex

    【讨论】:

    • 谢谢。这个建议我没有成功,我仍然看到同样的行为。我也试过图像 1.8.1 没有成功。
    猜你喜欢
    • 2018-01-08
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2020-09-11
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多