Kubernetes Ingress returns 404 for a path other than /
404 Not FoundShort 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.
Fix it fast
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/2Match what you see, then jump to its fix
- 1
The backend itself returns 404 for /app but 200 for /The backend has no route for the prefixFix - 2
The same manifest worked on an older controller and breaks after an upgradeAn old rewrite-target without capture groupsFix - 3
kubectl apply refuses the Ingress, or no rule is createdThe manifest uses an API version or path type the cluster rejectsFix
- 1
What does the error look like?
404 Not Found404 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: 803Verify
Request the prefixed path through the ingress.
curl -i http://<ingress-address>/something/newConfirmed by kubernetes.github.io
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.yaml3Verify
Compare the path the backend logs with what you requested.
kubectl logs deploy/my-app --tail=20Confirmed by kubernetes.github.io
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 ingress2Fix
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: nginx3Verify
The Ingress is accepted and shows an address.
kubectl get ingress my-appConfirmed by kubernetes.io
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Servers
Uploads larger than 1 MB fail with 413 behind the Kubernetes NGINX Ingress controller
Uploads of a few MB fail, small files work
The ingress-nginx controller limits request bodies to 1m by default and answers 413 above that.
3 causes, each with a fixServers
Argo CD behind an nginx Ingress loops with too many redirects
307 Temporary Redirect on every request to the Argo CD host
The browser and Argo CD disagree about who handles TLS.
3 causes, each with a fixServers
ingress-nginx controller stops working after upgrading Kubernetes to 1.22 or later
ingress-nginx controller does not start after an AKS upgrade to 1.22
Kubernetes 1.22 stopped serving the extensions/v1beta1 and networking.k8s.io/v1beta1 Ingress APIs.
2 causes, each with a fixServers
Angular routes return 404 from nginx when you refresh or open a link
The home page loads, but refreshing /some/route shows an nginx 404
Angular routes like /users/42 are not files, so nginx looks for them on disk and returns 404.
3 causes, each with a fixServers
A cert-manager Certificate stays at READY False with Let's Encrypt
Issuing certificate as Secret does not exist
A Certificate that stays not ready is waiting on a chain: Issuer, CertificateRequest, Order, Challenge.
3 causes, each with a fixServers
A Let's Encrypt certificate did not renew with Certbot
Type: connection ... Timeout during connect (likely firewall problem)
Run sudo certbot renew --dry-run and read the Type and Detail lines.
6 causes, each with a fix