<p>I have following ingress.yaml file</p>
<pre class="lang-sh prettyprint-override"><code>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
</code></pre>
<p>But when I send a request to https:///base/path/v1/api/update it succeeds and I got following error in nginx ingress controller</p>
<pre><code>Error: exit status 1
2020/08/06 18:35:07 [emerg] 1734#1734: location &quot;/base/path/v1/api/update&quot; is outside location &quot;^/base/path(/|$)(.*)&quot; in /tmp/nginx-cfg008325631:2445
nginx: [emerg] location &quot;/base/path/v1/api/update&quot; is outside location &quot;^/base/path(/|$)(.*)&quot; in /tmp/nginx-cfg008325631:2445
nginx: configuration file /tmp/nginx-cfg008325631 test failed
</code></pre>
<p>Can somebody help?</p>

<p>The <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#configuration-snippet" rel="noreferrer">configuration-snippet</a> is to add configs <strong>to locations</strong>.</p>
<p>If you want to add a custom location to the <a href="http://nginx.org/en/docs/http/ngx_http_core_module.html#server" rel="noreferrer">server</a> context, you should use the <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#server-snippet" rel="noreferrer">server-snippet</a> instead:</p>
<blockquote>
<p>Using the annotation <code>nginx.ingress.kubernetes.io/server-snippet</code> it is
possible to add custom configuration in the server configuration
block.</p>
</blockquote>
<p>You also need to use some modifiers and regex to make it work (<code>~*</code> and <code>^</code>).</p>
<p>The following config should work:</p>
<pre><code>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 ~* &quot;^/base/path/v1/api/update&quot; {
              deny all;
              return 403;
            }
spec:
  rules:
    - http:
        paths:
          - path: /base/path(/|$)(.*)
            backend:
              serviceName: myApi
              servicePort: 8080
</code></pre>
<p>The final <code>nginx.config</code> should end like this:</p>
<pre><code>$ kubectl exec -n kube-system nginx-ingress-controller-6fc5bcc8c9-chkxf -- cat /etc/nginx/nginx.conf

[...]

location ~* &quot;^/base/path/v1/api/update&quot; {
            deny all;
            return 403;
        }
        
location ~* &quot;^/base/path(/|$)(.*)&quot; {
[...]           
}
</code></pre>
