'ingress-nginx: How to insert access_by_lua_block{} only for specific location?

I have multiple location blocks under a single host:, something like this:

apiVersion: networking.k8s.io/v1
kind: ingress
metadata:
  name: ingress-nginx
  annotations:
    kubernetes.io/ingress.calass: nginx
    ngnx.ingress.kubernetes.io/use-regex: "true"
    ngnx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: nginx
  rules:
    - host: ingress.mydomain.org.local
      http:
        paths:
          - path: /app1(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: app1-service
                port:
                  number: 5678
          - path: /api(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: api-service
                port:
                  number: 5678
    

I need to insert access_by_lua_block{...} only for one location, say: /api - how do I do that? I tried with ngnx.ingress.kubernetes.io\configuration-snippet, like this:

annotations:
  ngnx.ingress.kubernetes.io\configuration-snippet: |
    location ~* "^/api(/|$)(.*)" {
      access_by_lua_block {
      ....
      }
    }

but that's just adding a nested location ~* "^/api(/|$)(.*)" block under every other location entries. Is there any way that can be achieved?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source