title: K8S Ingress controller redirect to the wrong path
body: <p>I have an Nginx ingress controller running on one k8s namespace,
  and on another k8s namespace, I defined a pod, a service, and an ingress resource.
  this is the ingress resource definition:</p>
  <pre><code>apiVersion: networking.k8s.io/v1beta1
  kind: Ingress
  metadata:
    name: my-app-ingress
    annotations:
      nginx.ingress.kubernetes.io/rewrite-target: /$2
  spec:
    ingressClassName: general-internal
    rules:
    - host: example.com
      http:
        paths:
        - path: &quot;/my-app(/|$)(.*)&quot;
          backend:
            serviceName: my-app-svc
            servicePort: 443
  </code></pre>
  <p>now, when I access this link:</p>
  <pre><code>http://example.com/my-app/some-path/
  </code></pre>
  <p>then everything is ok because the &quot;my-app-svc&quot; knows the path &quot;/some-path/&quot; and returns a 200 response (the forwarding is to <code>http://my-app-svc/some-path</code> and that's great because my-app-svc doesn't and shouldn't know or care for the <code>/my-app</code> prefix that exists only for the nginx ingress controller so it will know to forward that request to &quot;my-app-svc&quot; internally).</p>
  <p>but when I access this link (notice no &quot;/&quot; in the end):</p>
  <pre><code>http://example.com/my-app/some-path
  </code></pre>
  <p>I get a redirection response from the &quot;my-app-svc&quot; service, and the &quot;Location&quot; header contains &quot;/some-path/&quot;, so the redirection is to:</p>
  <pre><code>http://example.com/some-path/
  </code></pre>
  <p>which does not lead to the &quot;my-app-svc&quot; service because it doesn't have the &quot;/my-app&quot; prefix.</p>
  <p>If the Location header was &quot;/my-app/some-path/&quot; instead of &quot;/some-path/&quot; then everything was ok because the redirection would be to:</p>
  <pre><code>http://example.com/my-app/some-path/
  </code></pre>
  <p>which would give me the 200 response.</p>
  <p>the question is, how can I do that the ingress controller will add a &quot;my-app&quot; prefix to the Location header when it returns the redirection response to the client?</p>
  <p>Thanks</p>