Kubernetes3 min read· 3 practice problems

Kubernetes Ingress 404 Not Found: causes and fixes (host mismatch, ingressClassName, path)

When an Ingress returns the nginx 404 page, the request matched no rule. Use kubectl get ingress and curl -H "Host" to tell host, ingressClassName, path/pathType and rewrite problems apart, and learn how to separate it from a 503.

On this page
  1. Short answer: the request matched no Ingress rule
  2. Who returned the 404?
  3. Diagnosis: compare the Ingress with the real request
  4. Causes 1 and 2: host and ingressClassName
  5. Cause 3: path and rewrite
  6. What not to do
  7. The order to work in
  8. Quick diagnostic checklist

Short answer: the request matched no Ingress rule

An Ingress controller picks a rule by the request's Host header and path. When nothing matches, ingress-nginx sends the request to its default backend, and what you see is the nginx 404 page. The cause is usually one of three:

  1. The host differs — www versus the apex domain, a typo, a newly added address
  2. ingressClassName is missing or wrong — the controller never reads this Ingress
  3. path, pathType or rewrite do not line up — this includes the app itself returning 404

If the rule matches but the Service name or port is wrong, or the Endpoints are empty, ingress-nginx answers 503 Service Temporarily Unavailable, not 404 (other controllers may differ). On a 503, start with kubectl get endpoints.

Who returned the 404?

<head><title>404 Not Found</title></head>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>

This page means the controller found no rule. If you get the app's own 404 page or JSON, the rule matched and the app does not know that path (cause 3).

Diagnosis: compare the Ingress with the real request

kubectl get ingress -n shop
kubectl describe ingress shop -n shop
NAME   CLASS   HOSTS              ADDRESS        PORTS     AGE
shop   nginx   shop.example.com   203.0.113.20   80, 443   12d

Rules:
  Host              Path  Backends
  ----              ----  --------
  shop.example.com
                    /   shop-web:80 (10.244.1.23:8080,10.244.2.11:8080)
  • Is the exact address you requested listed under HOSTS? If not, cause 1
  • Empty CLASS and ADDRESS mean no controller has taken this Ingress — cause 2

Take DNS out of the picture and send the Host header straight to the controller. For HTTPS, use --resolve so SNI matches too.

curl -i -H "Host: www.shop.example.com" http://203.0.113.20/
curl -i --resolve www.shop.example.com:443:203.0.113.20 https://www.shop.example.com/
kubectl -n ingress-nginx logs deploy/ingress-nginx-controller --tail=100

If the bracketed upstream in the controller's access log is [upstream-default-backend], no rule matched.

Causes 1 and 2: host and ingressClassName

host must match exactly, and *.example.com matches only one subdomain level, never example.com itself. Put every address you serve into the rules (and the TLS hosts). Look up the class name with kubectl get ingressclass and set it. Without a default IngressClass, ingress-nginx ignores an Ingress that has no class and logs Ignoring ingress because of error while validating ingress class.

spec:
  ingressClassName: nginx
  rules:
  - host: www.shop.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: shop-web
            port:
              number: 80

Cause 3: path and rewrite

Prefix matches by path element: /api matches /api and /api/v1 but not /apiv1. With Exact, /api does not match /api/. If the app expects /orders but receives /api/orders unchanged, the app returns 404. In ingress-nginx, strip the prefix with a regex and rewrite.

annotations:
  nginx.ingress.kubernetes.io/use-regex: "true"
  nginx.ingress.kubernetes.io/rewrite-target: /$2
...
      - path: /api(/|$)(.*)
        pathType: ImplementationSpecific

What not to do

  • Restarting the controller pods over and over — Ingress changes are picked up automatically. A wrong rule is still a 404 after a restart.
  • Papering over it with a catch-all rule without a host — the wrong service answers and the cause gets harder to see.

The order to work in

  1. Tell whether the 404 body is the nginx default page or the app's page (503 means Endpoints)
  2. Check HOSTS, CLASS and ADDRESS in kubectl get ingress
  3. Send a request straight to the controller with curl -H "Host: ..."
  4. Fix host, ingressClassName, path/pathType and rewrite in that order, then curl again

To practise the case where only the www address returns 404, try the one-minute quiz Ingress 404 Not Found: only the newly added www host returns 404.

Quick diagnostic checklist

  • First tell whether the 404 body is the nginx default page or the app's own 404.
  • A 503 Service Temporarily Unavailable is a Service or Endpoints problem, not a 404 one.
  • Check that the exact address you requested appears under HOSTS in kubectl get ingress.
  • If CLASS and ADDRESS are empty, find the class name with kubectl get ingressclass and set it.
  • Take DNS out with curl -i -H "Host: www.shop.example.com" http://<controller-ip>/.
  • Look for requests that ended at [upstream-default-backend] in the controller log.
  • Check the difference between pathType Prefix and Exact, and any trailing /.
  • If the app returns 404, check whether rewrite-target must strip a path prefix.

Practice with this guide

Reviewed problems about the same failure. Write the cause, recovery, and prevention yourself, then compare with the model answer.

Read next