<p>I have a pod that responds to requests to /api/</p>

<p>I want to do a rewrite where requests to /auth/api/ go to /api/.</p>

<p>Using an Ingress (nginx), I thought that with the ingress.kubernetes.io/rewrite-target: annotation I could do it something like this:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myapi-ing
  annotations:
    ingress.kubernetes.io/rewrite-target: /api
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: api.myapp.com
    http:
      paths:
      - path: /auth/api
        backend:
          serviceName: myapi
          servicePort: myapi-port
</code></pre>

<p>What's happening however is that /auth/ is being passed to the service/pod and a 404 is rightfully being thrown.  I must be misunderstanding the rewrite annotation.</p>

<p>Is there a way to do this via k8s &amp; ingresses?</p>

<p>I don't know if this is still an issue, but since version 0.22 it seems you need to use capture groups to pass values to the rewrite-target value
From the nginx example available <a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="noreferrer">here</a></p>

<blockquote>
  <p>Starting in Version 0.22.0, ingress definitions using the annotation nginx.ingress.kubernetes.io/rewrite-target are not backwards compatible with previous versions. In Version 0.22.0 and beyond, any substrings within the request URI that need to be passed to the rewritten path must explicitly be defined in a capture group.</p>
</blockquote>

<p>For your specific needs, something like this should do the trick</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myapi-ing
annotations:
  ingress.kubernetes.io/rewrite-target: /api/$2
  kubernetes.io/ingress.class: "nginx"
spec:
 rules:
 - host: api.myapp.com
   http:
    paths:
     - path: /auth/api(/|$)(.*)
       backend:
         serviceName: myapi
         servicePort: myapi-port
</code></pre>
