kubernetesnginxnginx-ingress

Kubernetes nginx Ingress rewrite-target results in double slash


I am working on creating an ingress configuration file for Kubernetes.

When I access /front/main/dashboard/3, I want to be redirected to /main/dashboard/3.
However, when I access /back/main/dashboard/3, I want to remain at /back/main/dashboard/3.

In essence, I need to remove the '/front' from the URL.
I have been using this file for testing purposes:

# demo-ingress.yaml
kind: Pod
apiVersion: v1
metadata:
  name: foo-app
  labels:
    app: foo
spec:
  containers:
    - name: foo-app
      image: "kicbase/echo-server:1.0"
---
kind: Service
apiVersion: v1
metadata:
  name: foo-service
spec:
  selector:
    app: foo
  ports:
    - port: 8080
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: demo-ingress
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$1$2 # Here we concatenate the two matched groups
spec:
  rules:
    - http:
        paths:
          - path: /front()(.*) # Here the first matching group is always empty
            pathType: ImplementationSpecific
            backend:
              service:
                name: foo-service
                port:
                  number: 8080
          - path: /(back)(.*) # Here the first matching group matches 'back'
            pathType: ImplementationSpecific
            backend:
              service:
                name: foo-service
                port:
                  number: 8080

When I run

kubectl apply -f demo-ingress.yaml && curl 192.168.49.2:80/back/main/dashboard/3

I get the expected response

HTTP/1.1 GET /back/main/dashboard/3

However, when I run

kubectl apply -f demo-ingress.yaml && curl 192.168.49.2:80/front/main/dashboard/3

I get

HTTP/1.1 GET //main/dashboard/3

I have tried various configurations to resolve this but haven't found a solution yet. Any help would be greatly appreciated.


Solution

  • I feel regex update would be required here. You are trying to match group 1 & 2.

    Please change -

    1. /front()(.*) -> /front(/|$)(.*)
    2. /(back)(.*) -> /(/back)(.*)
    3. /$1$2 -> $1$2

    This should map -

    1. /front -> /
    2. /front/whatever -> /whatever
    3. /back/whatever -> /back/whatever