0

I have an ingress.yaml 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 Captured Groups, but can't seem to properly implement this. How do I do that?

This is what my ingress looks like:

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
Jonas
  • 121,568
  • 97
  • 310
  • 388
Furkan Öztürk
  • 441
  • 4
  • 21
  • Does this answer your question? [kubernetes ingress with multiple target-rewrite](https://stackoverflow.com/questions/49514702/kubernetes-ingress-with-multiple-target-rewrite) – Yassine CHABLI Jan 06 '23 at 19:07

1 Answers1

2

You need to use a regular expression capture group in your path expression, and then reference the capture group in your .../rewrite-target annotation.

That might look like this:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: http-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: "/$2"
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

We need to ensure that for both rules, capture group $2 contains the desired path. For the first rule (path: /), we have an empty group $1 (because it's not necessary here), with the entire path captured in $2.

For the second rule, we match either /mf2 followed by either a /path... or as the end of the url (this ensures we don't erroneously match /mf2something). Group $1 will contain the / (or nothing), and the path goes into $2.

In both cases, the rewritten path (/$2) will have what we want.

larsks
  • 277,717
  • 41
  • 399
  • 399