【问题标题】:kubernetes nginx ingress error with configuration-snippet带有配置片段的 kubernetes nginx 入口错误
【发布时间】:2020-11-27 02:03:53
【问题描述】:

我有以下 ingress.yaml 文件

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
    name: nginx-configuration-snippet
    annotations:
        nginx.ingress.kubernetes.io/rewrite-target: /$2
        nginx.ingress.kubernetes.io/configuration-snippet: |
          location /base/path/v1/api/update {
              deny all;
              return 404;
            }
spec:
  rules:
    - http:
        paths:
          - path: /base/path(/|$)(.*)
            backend:
              serviceName: myApi
              servicePort: 8080

但是当我向 https:///base/path/v1/api/update 发送请求时,它会成功,并且在 nginx 入口控制器中出现以下错误

Error: exit status 1
2020/08/06 18:35:07 [emerg] 1734#1734: location "/base/path/v1/api/update" is outside location "^/base/path(/|$)(.*)" in /tmp/nginx-cfg008325631:2445
nginx: [emerg] location "/base/path/v1/api/update" is outside location "^/base/path(/|$)(.*)" in /tmp/nginx-cfg008325631:2445
nginx: configuration file /tmp/nginx-cfg008325631 test failed

有人可以帮忙吗?

【问题讨论】:

    标签: nginx kubernetes kubernetes-helm kubernetes-ingress nginx-ingress


    【解决方案1】:

    configuration-snippet 是将配置添加到位置

    如果您想将自定义位置添加到 server 上下文,您应该改用 server-snippet

    使用注解nginx.ingress.kubernetes.io/server-snippet是 可以在服务器配置中添加自定义配置 块。

    您还需要使用一些修饰符和正则表达式来使其工作(~*^)。

    以下配置应该可以工作:

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
        name: nginx-configuration-snippet
        annotations:
            nginx.ingress.kubernetes.io/rewrite-target: /$2
            nginx.ingress.kubernetes.io/server-snippet: |
              location ~* "^/base/path/v1/api/update" {
                  deny all;
                  return 403;
                }
    spec:
      rules:
        - http:
            paths:
              - path: /base/path(/|$)(.*)
                backend:
                  serviceName: myApi
                  servicePort: 8080
    

    最后的nginx.config 应该这样结束:

    $ kubectl exec -n kube-system nginx-ingress-controller-6fc5bcc8c9-chkxf -- cat /etc/nginx/nginx.conf
    
    [...]
    
    location ~* "^/base/path/v1/api/update" {
                deny all;
                return 403;
            }
            
    location ~* "^/base/path(/|$)(.*)" {
    [...]           
    }
    

    【讨论】:

    • 哇!这就像一个魅力。整个互联网搜索,你的答案是唯一有帮助的:)
    • @voidMainReturn 我可以像 ingress.kubernetes.io/server-snippet: 这样使用 configuration-sn-p 吗?我有这个问题link
    猜你喜欢
    • 2018-12-16
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 2019-09-30
    相关资源
    最近更新 更多