0

0

I am currently working on setting up an NGINX Ingress for my Kubernetes cluster, and I've encountered a bit of a complex scenario involving path rewriting.

To give a brief overview, I have multiple services in my cluster that need to be exposed through the Ingress, and different services require different path rewriting rules. Some services require the path to be rewritten as /$2 (meaning that a part of the original path should be stripped off) and some require /$1 (indicating that the original path should remain the same).

Here is a simplified dummy version of my Ingress YAML:

apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      annotations:
        nginx.ingress.kubernetes.io/use-regex: "true"
        kubernetes.io/ingress.class: nginx
        nginx.ingress.kubernetes.io/configuration-snippet: |
          rewrite ^/serviceA(/|$)(.*) /$2 break;
          rewrite ^/serviceB(/|$)(.*) /$2 break;
          rewrite ^/serviceC(/|$)(.*) /$2 break;
          rewrite ^/(.*) /$1 break;
        kubernetes.io/ingress.class: nginx
      name: sample-ingress
    spec:
      rules:
      - host: sample.com
        http:
          paths:
          - path: /serviceA(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: serviceA
                port:
                  number: 80
          - path: /serviceB(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: serviceB
                port:
                  number: 80
          - path: /serviceC(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: serviceC
                port:
                  number: 80
          - path: /serviceD(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: serviceD
                port:
                  number: 80
          - path: /(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: default-service
                port:
                  number: 80

In the above configuration, I've used a combination of the nginx.ingress.kubernetes.io/use-regex and nginx.ingress.kubernetes.io/configuration-snippet annotations for path rewriting.

This works well for serviceA, serviceB, and serviceC, which require /$2 rewriting. However, it does not work as expected for serviceD (and potentially others), which require /$1 rewriting. For these, I am expecting the original path to remain the same, but it appears that the last rewrite ^/(.*) /$1 break; rule isn't functioning as expected.

I am using the nginx:alpine image for my Ingress controller.

Has anyone encountered a similar situation? Any help or advice would be greatly appreciated!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.