<p>I have an <code>ingress.yaml</code> with two paths; each to one of my microfrontends. However I'm really struggling to get the rewrite-target to work. mf1 loads correctly, but mf2 doesn't. I've done some research and know I need to use <a href="https://github.com/kubernetes/ingress-nginx/tree/main/docs/examples/rewrite#examples" rel="nofollow noreferrer">Captured Groups</a>, but can't seem to properly implement this. How do I do that?</p>
<p>This is what my ingress looks like:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: http-ingress
  annotations:
    kubernetes.io/ingress.class: public
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: mf1
                port:
                  number: 80
          - path: /mf2
            pathType: Prefix
            backend:
              service:
                name: mf2
                port:
                  number: 80
</code></pre>

Furkan Öztürk
<p>You need to use a regular expression capture group in your <code>path</code> expression, and then reference the capture group in your <code>.../rewrite-target</code> annotation.</p>
<p>That might look like this:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: http-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: &quot;/$2&quot;
spec:
  rules:
    - http:
        paths:
          - path: /()(.*)
            pathType: Prefix
            backend:
              service:
                name: backend1
                port:
                  name: http
          - path: /mf2(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: backend2
                port:
                  name: http
</code></pre>
<p>We need to ensure that for both rules, capture group <code>$2</code> contains the desired path. For the first rule (<code>path: /</code>), we have an empty group <code>$1</code> (because it's not necessary here), with the entire path captured in <code>$2</code>.</p>
<p>For the second rule, we match either <code>/mf2</code> followed by either a <code>/path...</code> or as the end of the url (this ensures we don't erroneously match <code>/mf2something</code>). Group <code>$1</code> will contain the <code>/</code> (or nothing), and the path goes into <code>$2</code>.</p>
<p>In both cases, the rewritten path (<code>/$2</code>) will have what we want.</p>
