Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Kubernetes Ingress returns 404 for a path other than /

Error404 Not Found

Short answer

The ingress matches /app, but the backend still receives /app and has no route there, so it answers 404. Use the rewrite-target annotation to change the URI that goes upstream. Since ingress-nginx v0.22 the part you keep must sit in a regex capture group, and the path needs the ImplementationSpecific path type.

  • Applies to ingress-nginx 1.x, Kubernetes 1.30
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the backend directly, bypassing the ingress, for the same path. If it also returns 404, the ingress is working and the URI that reaches the app is the problem.

    kubectl port-forward svc/my-app 8080:80 &
    curl -i http://127.0.0.1:8080/app/
    curl -i http://127.0.0.1:8080/
  2. 2Match what you see, then jump to its fix

    1. 1The backend itself returns 404 for /app but 200 for /The backend has no route for the prefixFix
    2. 2The same manifest worked on an older controller and breaks after an upgradeAn old rewrite-target without capture groupsFix
    3. 3kubectl apply refuses the Ingress, or no rule is createdThe manifest uses an API version or path type the cluster rejectsFix

What does the error look like?

  • 404 Not Found
  • 404 Not Found nginx on /app but / works

Cause 1 of 3

The backend has no route for the prefix

1Check

rewrite-target sets the URI that incoming traffic is redirected to. Without it, the path the client used is what the app sees.

kubectl get ingress my-app -o yaml | grep -n "rewrite-target\|path:\|pathType"

2Fix

Rewrite the path to what the app serves, keeping the rest of the URI in a capture group. Here /something becomes /, and /something/new becomes /new.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-app
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: nginx
  rules:
    - http:
        paths:
          - path: /something(/|$)(.*)
            pathType: ImplementationSpecific
            backend:
              service:
                name: my-app
                port:
                  number: 80

3Verify

Request the prefixed path through the ingress.

curl -i http://<ingress-address>/something/new

Confirmed by kubernetes.github.io

Did this fix it?

Cause 2 of 3

An old rewrite-target without capture groups

1Check

From v0.22.0 any part of the request URI that must carry over has to be inside a capture group, and the numbered placeholder ($1, $2) goes into rewrite-target. A plain /something path with rewrite-target: / maps everything to the root.

kubectl get ingress -A -o yaml | grep -n "rewrite-target"

2Fix

Add the capture group to the path and use its number in the annotation, as in the manifest above.

kubectl apply -f ingress.yaml

3Verify

Compare the path the backend logs with what you requested.

kubectl logs deploy/my-app --tail=20

Confirmed by kubernetes.github.io

Did this fix it?

Cause 3 of 3

The manifest uses an API version or path type the cluster rejects

1Check

networking.k8s.io/v1 requires pathType on every path. The older v1beta1 Ingress API was removed in Kubernetes 1.22.

kubectl api-resources | grep -i ingress

2Fix

Use apiVersion networking.k8s.io/v1, set pathType on every path, and use ingressClassName instead of the old class annotation.

apiVersion: networking.k8s.io/v1
spec:
  ingressClassName: nginx

3Verify

The Ingress is accepted and shows an address.

kubectl get ingress my-app

Confirmed by kubernetes.io

Did this fix it?

How do I stop it coming back?

  • Prefer making the app serve under its prefix when you control it; rewrites are the workaround.
  • Test each path with curl after every ingress change.
  • Keep all Ingress manifests on networking.k8s.io/v1.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.