kubernetes Ingress shows "404 Not Found" eventhough user redirected to correct url

10/15/2020

I have Nginx Ingress service running on 30001 Kubernetes version 1.18.6 Cluster configured with VmWare workstation

I have deployed wordpress and I have a ingress for wordpress

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-wordpress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
 rules:
   - http:
       paths:
          - path: /wordpress(/|$)(.*)
            backend:
              serviceName: wordpress
              servicePort: 80

This is my wordpress deployment

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: wordpress
      labels:
        name: wordpress
    
    spec:
      template:
        # Pod definition
        metadata:
          name: wordpress
          labels:
            name: wordpress
        spec:
          containers:
            - name: wordpress
              image: wordpress
              volumeMounts:
                - mountPath: /var/www/html
                  name: wordpress-persistent-storage
              env:
              - name: WORDPRESS_DB_PASSWORD
                valueFrom:
                    secretKeyRef:
                      name: mysql-pass
                      key: password
              - name: WORDPRESS_DB_HOST
                value: "wordpress-mysql"
              - name: WORDPRESS_DB_USER
                value: "root"
          volumes:
        - name: wordpress-persistent-storage
          persistentVolumeClaim:
            claimName: wordpress-persistent-storage
  replicas: 2
  selector:
    matchLabels:
      name: wordpress

This is my nginx ingress controller

apiVersion: apps/v1

kind: Deployment

metadata:

  name: nginx-ingress-controller

spec:

  replicas: 1

  selector:

    matchLabels:

      name: nginx-ingress

  template:

    metadata:

      labels:

        name: nginx-ingress

    spec:

      containers:

        - name: nginx-ingress-controller

          image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.31.1

          args:

            - /nginx-ingress-controller

            - --configmap=$(POD_NAMESPACE)/nginx-configuration

          env:

            - name: POD_NAME

              valueFrom:

                fieldRef:

                  fieldPath: metadata.name

            - name: POD_NAMESPACE

              valueFrom:

                fieldRef:

                  fieldPath: metadata.namespace

          ports:

            - name: http

              containerPort: 80

            - name: https

              containerPort: 443

my worker IPs are 192.168.111.136,192.168.111.137

when I load http://worker-ip:30001/wordress

I am getting redirected for http://worker-ip:30001/wp-admin/install.php which is the correct url of course.

But my browser shows 404 Not Found

enter image description here

This is my logs for --> kubectl logs nginx-ingress-controller-776d4c78f9-fkrgx -f

I1014 16:34:37.481750       6 event.go:278] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"ingress-wordpress", UID:"6051b54a-c359-469b-a34e-8ba1b0202ebd", APIVersion:"networking.k8s.io/v1beta1", ResourceVersion:"171471", FieldPath:""}): type: 'Normal' reason: 'DELETE' Ingress default/ingress-wordpress
I1014 16:34:37.535710       6 controller.go:155] Backend successfully reloaded.
I1014 16:34:40.458184       6 event.go:278] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"ingress-wordpress", UID:"0c6d67d5-a118-4391-b3ca-c8179fd12a44", APIVersion:"networking.k8s.io/v1beta1", ResourceVersion:"171482", FieldPath:""}): type: 'Normal' reason: 'CREATE' Ingress default/ingress-wordpress
I1014 16:34:40.815250       6 controller.go:139] Configuration changes detected, backend reload required.
I1014 16:34:40.865628       6 controller.go:155] Backend successfully reloaded.
10.32.0.1 - - [14/Oct/2020:16:34:52 +0000] "GET /wordpress HTTP/1.1" 302 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36" 458 0.012 [default-wordpress-80] [] 10.32.0.4:80 0 0.012 302 3fa7b1be573bb3db1e573953d7c29aba
I1014 16:35:05.344074       6 status.go:275] updating Ingress default/ingress-wordpress status from [] to [{192.168.111.136 }]
I1014 16:35:05.349635       6 event.go:278] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"ingress-wordpress", UID:"0c6d67d5-a118-4391-b3ca-c8179fd12a44", APIVersion:"networking.k8s.io/v1beta1", ResourceVersion:"171546", FieldPath:""}): type: 'Normal' reason: 'UPDATE' Ingress default/ingress-wordpress
10.44.0.0 - - [14/Oct/2020:16:44:14 +0000] "GET /wordpress HTTP/1.1" 302 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36" 439 0.009 [default-wordpress-80] [] 10.32.0.4:80 0 0.012 302 54c29a839541ce6e724ce004a0890942
10.32.0.1 - - [14/Oct/2020:16:45:35 +0000] "GET /wordpress HTTP/1.1" 302 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36" 458 0.012 [default-wordpress-80] [] 10.32.0.4:80 0 0.012 302 83335c2e98f6784efd051d81b84ad2ef
10.32.0.1 - - [14/Oct/2020:16:45:37 +0000] "GET /wordpress/ HTTP/1.1" 302 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36" 440 0.021 [default-wordpress-80] [] 10.44.0.3:80 0 0.020 302 db916b199d7f08379ac79122415855b7

How to correct this ?

-- AMendis
kubernetes
kubernetes-ingress
nginx-ingress
wordpress

1 Answer

10/15/2020

Your application is redirecting you to /wp-admin/. This path does not match any Ingress rules, so it returns 404.

You need to add a wp-admin HTTP path rule as well, so the request will be properly proxy-forwarded to the WordPress container.

-- Eduardo Baitello
Source: StackOverflow