<p>I have two deffinitions for Nginx Ingress Controller. Each of them routes to services for web app(React app hosted on nginx) and web api(.Net Core).</p>
<p>First is workig fine, but it is cumbresome since I need to add entry in etc file for each specified host to make it work:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-nginx-controller
  annotations:
    kubernetes.io/ingress.class: &quot;nginx&quot;
spec:
  rules:
    - host: testapp-web-dev
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: testapp-portal-web-service
                port:
                  number: 80
    - host: testapp-api-dev
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: testapp-portal-api-service
                port:
                  number: 80
</code></pre>
<p>I decided to modify it to have single host with multiple paths, so I will have only one entry in etc file. But it does not work. First request is routed correctly to http://testapp//testapp-web-dev but every other next
request fails i.e. request for manifest goes to http://testapp/manifest.json but it should go to http://testapp/testapp-web-dev/manifest.json.</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-nginx-controller
  annotations:
    kubernetes.io/ingress.class: &quot;nginx&quot;
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
    - host: testapp
      http:
        paths:
          - path: /testapp-web-dev(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: testapp-portal-web-service
                port:
                  number: 80
          - path: /testapp-api-dev(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: testapp-portal-api-service
                port:
                  number: 80
</code></pre>
<p>Tried couple different url rewrite but without luck.</p>

Daveo
<p>If you want to preserve the requested path, you need to <strong>remove the <code>nginx.ingress.kubernetes.io/rewrite-target: /$2</code> annotation.</strong></p>
<p>As per <a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="nofollow noreferrer">Nginx Ingress Rewrite</a>:</p>
<blockquote>
<p>In this ingress definition, any characters captured by <code>(.*)</code> will be
assigned to the placeholder <code>$2</code>, which is then used as a parameter in
the rewrite-target annotation.</p>
</blockquote>
<p>i.e., the annotation is redirecting <code>http://testapp/testapp-web-dev/manifest.json</code> to <code>http://testapp/manifest.json</code>.</p>
