'How to configure path in nginx-ingress configuration? [closed]

I have a very simple single node cluster in minikube running locally and want to define an ingress configuration file for the kubernetes-dashboard.

$ kubectl get ns
NAME                   STATUS   AGE
default                Active   3d
ingress-nginx          Active   13h
kube-node-lease        Active   3d
kube-public            Active   3d
kube-system            Active   3d
kubernetes-dashboard   Active   12h
$ kubectl get all -n kubernetes-dashboard
NAME                                            READY   STATUS    RESTARTS      AGE
pod/dashboard-metrics-scraper-58549894f-9zg2k   1/1     Running   3 (53m ago)   13h
pod/kubernetes-dashboard-ccd587f44-vsb2m        1/1     Running   5 (52m ago)   13h

NAME                                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/dashboard-metrics-scraper   ClusterIP   10.96.55.136    <none>        8000/TCP   13h
service/kubernetes-dashboard        ClusterIP   10.104.138.73   <none>        80/TCP     13h

NAME                                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/dashboard-metrics-scraper   1/1     1            1           13h
deployment.apps/kubernetes-dashboard        1/1     1            1           13h

NAME                                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/dashboard-metrics-scraper-58549894f   1         1         1       13h
replicaset.apps/kubernetes-dashboard-ccd587f44        1         1         1       13h

As you can see there is a kubernetes-dashboard service running on port 80.

Also I installed ingress-nginx using-

$ minikube addons enable ingress

Here is my initial dashboard-ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: dashboard-ingress
  namespace: kubernetes-dashboard
spec:
  rules:
    - host: dashboard.com
      http:
        paths:
        - path: /
          pathType: Prefix
          backend:
              service:
                name: kubernetes-dashboard
                port:
                  number: 80

This runs perfectly when I open dashboard.com (This is a local IP address configured in /etc/hosts)

But when I modified it to-

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: dashboard-ingress
  namespace: kubernetes-dashboard
spec:
  rules:
    - host: dashboard.com
      http:
        paths:
        - path: /foo(/|$)(.*)
          pathType: Prefix
          backend:
              service:
                name: kubernetes-dashboard
                port:
                  number: 80

and try to access the dashboard through dashboard.com/foo, the dashboard doesn't open up but when I access it using dashboard.com/foo/ it opens perfectly fine. The rewrite documentation mentions that-

  • rewrite.bar.com/something rewrites to rewrite.bar.com/
  • rewrite.bar.com/something/ rewrites to rewrite.bar.com/

Then why I am I seeing a difference, also when I change to-

annotations:
  nginx.ingress.kubernetes.io/rewrite-target: /

Again I don't see the dashboard, this is confusing me, any help will be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source