0
votes

I have two deffinitions for Nginx Ingress Controller. Each of them routes to services for web app(React app hosted on nginx) and web api(.Net Core).

First is workig fine, but it is cumbresome since I need to add entry in etc file for each specified host to make it work:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-nginx-controller
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
    - host: testapp-web-dev
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: testapp-portal-web-service
                port:
                  number: 80
    - host: testapp-api-dev
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: testapp-portal-api-service
                port:
                  number: 80

I decided to modify it to have single host with multiple paths, so I will have only one entry in etc file. But it does not work. First request is routed correctly to http://testapp//testapp-web-dev but every other next request fails i.e. request for manifest goes to http://testapp/manifest.json but it should go to http://testapp/testapp-web-dev/manifest.json.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-nginx-controller
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
    - host: testapp
      http:
        paths:
          - path: /testapp-web-dev(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: testapp-portal-web-service
                port:
                  number: 80
          - path: /testapp-api-dev(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: testapp-portal-api-service
                port:
                  number: 80

Tried couple different url rewrite but without luck.

1

1 Answers

2
votes

If you want to preserve the requested path, you need to remove the nginx.ingress.kubernetes.io/rewrite-target: /$2 annotation.

As per Nginx Ingress Rewrite:

In this ingress definition, any characters captured by (.*) will be assigned to the placeholder $2, which is then used as a parameter in the rewrite-target annotation.

i.e., the annotation is redirecting http://testapp/testapp-web-dev/manifest.json to http://testapp/manifest.json.