Ingress, rewrite-target, frontend and API same URL. Version 0.22.0+

Viewed 1113

I am tying to put together a setup like this :

  • example.com #frond end
  • example.com/api
  • example.com/authentication

And obviously each of them separated applications and should be able to continue their own path , ex. http://example.com/api/v1/test?v=ok

Right now I have a yaml like this:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
 name: test-ingress
 annotations:
   nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
 rules:
 - http:
     paths:
     - path: /
       backend:
         serviceName: frontend-service
         servicePort: 80
     - path: /api(/|$)(.*)
       backend:
         serviceName: backend-service
         servicePort: 80
     - path: /authentication(/|$)(.*)
       backend:
         serviceName: identityserver-service
         servicePort: 80

/api and /authentication behave the way I want, but the sub path for front end is not working. So for example http://example.com/css/bootstrap.css is not found.

So far I've tried to

1- add (/|$)(.*) at the and of front end path

2- add a copy of front end path with the same backed and port and path of /.*

None of them solved the problem.

Here is the describe result:

Name:             test-ingress
Namespace:        default
Address:          127.0.0.1
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host        Path  Backends
  ----        ----  --------
  *
              /                          frontend-service:80 (10.1.80.38:80,10.1.80.43:80,10.1.80.50:80)
              /api(/|$)(.*)              backend-service:80 (10.1.80.39:80,10.1.80.42:80,10.1.80.47:80)
              /authentication(/|$)(.*)   identityserver-service:80 (10.1.80.40:80,10.1.80.41:80,10.1.80.45:80)
Annotations:  nginx.ingress.kubernetes.io/rewrite-target: /$2
Events:
  Type    Reason  Age                 From                      Message
  ----    ------  ----                ----                      -------
  Normal  UPDATE  43s (x14 over 13h)  nginx-ingress-controller  Ingress default/test-ingress

PS : I'vd found a few answers that seems to be older than Version 0.22.0 and not working anymore ex . kubernetes ingress with multiple target-rewrite

1 Answers

The problem in your case lies with incorrect regular expression. The $ operator matches the end of the string or end of a line if the multiline flag is enabled. With first group you are capturing / or $ but your string does not end and there is not match.

I tested this and it works with this regex:

 paths:
 - path: /()(.*)
   backend:
     serviceName: frontend-service
     servicePort: 80
Related