How to strip the path prefix in Kubernetes Traefik ingress?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
When running Traefik as your Kubernetes ingress controller, it is common to expose services under a shared prefix such as /api while backend services expect paths without that prefix. Path prefix stripping solves this by rewriting the request URL before forwarding traffic to the service. In Traefik, this is handled through middleware attached to your route.
Why Prefix Stripping Is Needed
Suppose your public URL is /api/users, but your backend route is /users. Without stripping /api, your service receives /api/users and may return 404 unless it is coded for that prefix.
Typical reasons to strip prefixes:
- one domain hosts multiple services under different path segments
- backend applications are already deployed with root-level routes
- you want cleaner ingress rules without modifying application code
Path rewrite at ingress keeps application routing simple and consistent.
Traefik Middleware Basics
In Traefik v2, prefix stripping is done with StripPrefix middleware. You define middleware as a Kubernetes resource and attach it to an IngressRoute or Ingress annotation.
Core components:
- route match such as host plus
PathPrefix - middleware that removes one or more leading segments
- service target and port
This separation allows reuse of one middleware across multiple routes.
Example with Traefik CRDs
Step 1: Create Middleware
Step 2: Attach it to IngressRoute
With this configuration, request /api/users is forwarded to backend as /users.
Using Standard Kubernetes Ingress
If you use standard Ingress instead of IngressRoute, you can still apply Traefik middleware through annotations.
Annotation value format is namespace-name@kubernetescrd.
StripPrefix Versus ReplacePath
StripPrefix removes a leading segment and keeps the remainder. ReplacePath replaces the full path with a fixed string. Choose based on routing behavior.
Use StripPrefix when:
- backend expects relative path after known prefix
- you want dynamic route continuation
Use ReplacePath when:
- backend only needs one fixed route regardless of incoming subpath
Mixing these middlewares incorrectly can cause path duplication or empty route issues.
Debugging Prefix Rewrite Problems
If rewriting does not work as expected, check these in order:
- middleware resource exists in expected namespace
- route actually references that middleware
- prefix list matches exact incoming URL segment
- service is healthy and reachable
- backend logs show received path
You can use temporary access logs in Traefik to confirm pre-forward path behavior and final routed target.
Multi-Prefix and Versioned API Cases
For versioned APIs such as /api/v1, you can strip multiple prefixes or just the top-level segment depending on desired backend path.
Example stripping /api only:
- incoming
/api/v1/users - backend receives
/v1/users
Example stripping both /api and /v1:
- backend receives
/users
Design this based on how backend routing is organized.
Security and Operational Considerations
Prefix rewriting changes request surface seen by backend. Keep documentation clear so app teams and API gateway teams share the same route expectations.
Operational recommendations:
- include rewrite behavior in runbooks
- add integration tests for key routes
- monitor
404rates after ingress changes - version ingress changes along with service releases
This reduces regressions during path migration or API consolidation work.
Common Pitfalls
- Applying middleware in one namespace and referencing it from another without correct identifier.
- Matching a path prefix that does not exactly align with live request paths.
- Forgetting to attach middleware to route and assuming definition alone applies it.
- Using
ReplacePathwhen prefix stripping was required. - Changing ingress path rules without validating backend route expectations.
Summary
- Traefik strips path prefixes using dedicated middleware, most commonly
StripPrefix. - Define middleware once and attach it explicitly to
IngressRouteor annotated Ingress. - Validate rewrite behavior with route tests and backend logs.
- Choose between
StripPrefixandReplacePathbased on desired path semantics. - Clear documentation and testing are essential when URL structure and backend routes differ.
Related reading
- How to switch kubectl clusters between gcloud and minikube
- How to switch namespace in kubernetes
- How to troubleshoot metrics-server on kubeadm?
- how to undo a kubectl port-forward
- how to uninstall minikube from ubuntu, i get an ''Unable to load cached images'' error
- How to update a set of pods running in kubernetes?
- How to update Kubernetes Dashboard in hosted Kubernetes on GKE?
- How to update nginx-ingress controller so that latest ingress paths are used?

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.