<p>I'm trying to deploy my frontend client application and backend API on the same domain and I want the frontend on the base path: /</p>
<p>However, I realized that the frontend and the backend need two different rewrite-target to accomplish this.</p>
<p>front-end works with:</p>
<pre><code>nginx.ingress.kubernetes.io/rewrite-target: /
</code></pre>
<p>while the backend works with:</p>
<pre><code>nginx.ingress.kubernetes.io/rewrite-target: /$2
</code></pre>
<p>I tried using two different ingress services in order to accommodate different rewrite-target, but that fails because the host was the same domain.</p>
<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: test
  annotations:
    kubernetes.io/ingress.class: nginx
    cert-manager.io/cluster-issuer: letsencrypt-staging
    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
    nginx.ingress.kubernetes.io/ssl-redirect: &quot;true&quot;
    nginx.ingress.kubernetes.io/add-base-url: &quot;true&quot;
    nginx.ingress.kubernetes.io/service-upstream: &quot;true&quot;
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/proxy-buffer-size: 128k
    nginx.ingress.kubernetes.io/proxy-buffering: &quot;on&quot;
    nginx.ingress.kubernetes.io/proxy-buffers-number: &quot;4&quot;
    nginx.ingress.kubernetes.io/cors-allow-credentials: &quot;true&quot;
    nginx.ingress.kubernetes.io/configuration-snippet: |
      more_set_headers &quot;server: hide&quot;;
      more_set_headers &quot;X-Content-Type-Options: nosniff&quot;;
      more_set_headers &quot;X-Xss-Protection: 1&quot;;
spec:
  tls:
  - hosts:
    - test.eastus.cloudapp.azure.com
    secretName: tls-secret
  rules:
  - host: test.eastus.cloudapp.azure.com
    http:
      paths:
      - backend:
          serviceName: webapp
          servicePort: 80
        path: /
      - backend:
          serviceName: api
          servicePort: 80
        path: /api(/|$)(.*)
</code></pre>
<p>I know I can make both work with the same rewrite-target <code>/$2</code> if I change the frontend path to path: /app(/|$)(.*) but I don't want to that except it is the only option.</p>
<p>Is there a way for me to better configure a single ingress to work for 2 different rewrite-target?</p>

capiono
<p>Ingress with API version networking.k8s.io/v1 with k8s version 1.19+ can solve your problem. I have given an example below.</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: simple-fanout-example
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: frontend-service
            port:
              number: 80
      - path: /api
        pathType: Prefix
        backend:
          service:
            name: backend-service
            port:
              number: 8080

</code></pre>
<p>According to kubernetes <a href="https://kubernetes.io/docs/concepts/services-networking/ingress/#multiple-matches" rel="nofollow noreferrer">documentation</a>:</p>
<blockquote>
<p>In some cases, multiple paths within an Ingress will match a request. In those cases precedence will be given first to the longest matching path. If two paths are still equally matched, precedence will be given to paths with an exact path type over prefix path type.</p>
</blockquote>
<p>So, when you are looking for a resource located at path &quot;/home.html&quot;, it only matches with your frontend service. But when you are looking for a resource located at path &quot;/api/something&quot; then it matches both service. But it will go to backend service always because of maximum path match stated above.</p>
