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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With