Search code examples
kuberneteskubernetes-helmkubernetes-ingressnginx-ingress

kubernetes nginx ingress server-snippet annotation not taking effect


I have following ingress.yaml

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 404;
            }
spec:
  rules:
    - http:
        paths:
          - path: /base/path(/|$)(.*)
            backend:
              serviceName: myApi
              servicePort: 8080

But when I send PUT request to /base/path/v1/api/update instead of getting 404 I am getting 500 which means that the path is reachable from ingress controller. Can anybody help me identify why ?

I changed to configuration-snippet and the error I got is :

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

Solution

  • Adding my own answer : The final config that worked for me was :

    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