Q How to rewrite single path among many with the ingress-nginx
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Rewriting one path in ingress-nginx is a common requirement when migrating APIs or preserving legacy routes. The subtle part is that rewrite annotations often apply to all paths in the same Ingress resource. This guide shows a safe pattern to rewrite only one path while leaving others unchanged.
Core Topic Sections
Why single-path rewrite is tricky
The annotation nginx.ingress.kubernetes.io/rewrite-target applies at the Ingress object level, not per-path in the same object. If you place many routes in one resource and add rewrite annotations, all matching paths may be affected.
The practical solution is:
- Put the rewritten path in its own Ingress resource.
- Keep non-rewritten routes in a separate Ingress resource.
- Use the same host in both resources.
ingress-nginx merges rules by host and path, so this layout works well.
Base routes without rewrite
Create an Ingress for normal routes that should pass through unchanged.
No rewrite annotation is present, so these paths keep original URI semantics.
Separate Ingress for the single rewrite
Now isolate only the legacy path that must be rewritten.
Request behavior:
/legacy/ordersbecomes/api/v1/ordersupstream./api/v1/ordersand/api/v2/ordersremain unchanged.
Validate generated NGINX behavior
After apply, verify status and test routes.
Quick functional tests:
Use backend logs to confirm request URI seen by each service.
Path matching and precedence notes
ingress-nginx path precedence can surprise teams when regex and prefix rules mix. Keep regex usage explicit and document intent in each rule. Avoid overlapping patterns when possible.
Recommended hygiene:
- Use specific patterns for rewritten routes.
- Keep rewritten route in dedicated resource.
- Add regression tests for representative URLs.
This prevents accidental routing changes during future edits.
Migration pattern for legacy APIs
Single-path rewrites are often temporary during API migration. Treat them as transitional infrastructure:
- Add metrics for rewritten route volume.
- Notify clients about deprecation timeline.
- Remove rewrite once traffic has moved to canonical path.
Tracking usage prevents permanent accumulation of compatibility rules.
Security and observability considerations
Rewrites can hide the original URI context if logging is limited. Ensure logs include both incoming path and upstream path when debugging auth and rate-limiting behavior.
Also verify that WAF or policy engines evaluate the intended path stage, especially in regulated workloads.
Common Pitfalls
- Putting rewrite annotations on a multi-path Ingress and unintentionally rewriting every path.
- Forgetting
use-regexwhile using capture groups in paths. - Using overlapping regex paths that cause unexpected route selection.
- Skipping end-to-end tests and discovering rewrite mistakes in production.
- Leaving temporary legacy rewrites in place without ownership or cleanup plan.
Summary
- Rewrite annotations are resource-level, so isolate rewritten path in its own Ingress.
- Keep normal routes in a separate Ingress to avoid collateral changes.
- Use regex capture groups carefully and validate with real requests.
- Monitor rewritten traffic as part of migration strategy.
- Document path precedence and test routing behavior continuously.
Related reading
- Rancher with cattle vs Rancher with Kubernetes vs Standalone Kubernetes
- Rate Limiting based on URL and Path in Kubernetes
- RBAC Role Based Access Control on K3s
- Read-only filesystem pod with Spring Boot application on Kubernetes
- Query EC2 tags from within instance
- RabbitMQ ** WARNING ** Mnesia is overloaded
- Readiness Probe for Redis with large dataset
- Readiness probe for statefulset, not individual pod/container

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.