<p>I have a deployment that has a single ingress definition that uses paths to direct requests for two services/pods on the backend</p>
<ul>
<li>one pod is for api</li>
<li>one pod is for ui</li>
</ul>
<p>The web address should only go to api if the URL contains <code>/api/&lt;anything&gt;</code>, otherwise everything else should go to ui.</p>
<p>My ingress setup looks like this</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myapp
  namespace: &quot;myapp&quot;
  labels:
    app.kubernetes.io/name: myapp
spec:
  ingressClassName: nginx
  rules:
    - host: myapp.dev
      http:
        paths:
          - path: /api/
            pathType: Prefix
            backend:
              service:
                name: myapp-api
                port:
                  name: http
          - path: /
            pathType: Prefix
            backend:
              service:
                name: mayapp-ui
                port:
                  name: http
</code></pre>
<p>If I test with <a href="https://myapp.dev/api/endpoint" rel="nofollow noreferrer">https://myapp.dev/api/endpoint</a>, the requests are sent round robin to both backend pods (I can see the requests in the logs in the ui pod).</p>
<p>I was under the impression that as <code>/api/endpoint</code> was in the path, it should always match on the longest rule and only ever send to the api pod?</p>
<p>Have I misunderstood how path matching works?</p>
<p>Many thanks</p>

Scott
<p>Your paths are not unique.</p>
<p>You should add the <code>rewrite-target</code> under <code>annotations</code>:</p>
<pre><code>metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: &quot;/$1&quot;
  namespace: default
</code></pre>
<p>and edit the paths names to map your cases:</p>
<pre><code> paths:
  - path: /(.*)
    pathType: Prefix
    backend:
      service:
        name: mayapp-ui
        port:
          name: http
  - path: /(api(?:/|$).*)
    pathType: Prefix
    backend:
      service:
        name: myapp-api
        port:
          name: http
</code></pre>
