带有配置片段的kubernetes nginx入口错误

时间:2020-08-06 18:40:59

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

我有以下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

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:5)

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(/|$)(.*)" {
[...]           
}