Kubernetes
Traefik
Ingress
Path Prefix
Tutorial

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.

Practice system design

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

yaml
1apiVersion: traefik.containo.us/v1alpha1
2kind: Middleware
3metadata:
4  name: strip-api
5  namespace: app
6spec:
7  stripPrefix:
8    prefixes:
9      - /api
10    forceSlash: false

Step 2: Attach it to IngressRoute

yaml
1apiVersion: traefik.containo.us/v1alpha1
2kind: IngressRoute
3metadata:
4  name: app-route
5  namespace: app
6spec:
7  entryPoints:
8    - web
9  routes:
10    - match: Host(`example.com`) && PathPrefix(`/api`)
11      kind: Rule
12      middlewares:
13        - name: strip-api
14      services:
15        - name: app-service
16          port: 8080

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.

yaml
1apiVersion: networking.k8s.io/v1
2kind: Ingress
3metadata:
4  name: app-ingress
5  namespace: app
6  annotations:
7    traefik.ingress.kubernetes.io/router.middlewares: app-strip-api@kubernetescrd
8spec:
9  ingressClassName: traefik
10  rules:
11    - host: example.com
12      http:
13        paths:
14          - path: /api
15            pathType: Prefix
16            backend:
17              service:
18                name: app-service
19                port:
20                  number: 8080

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:

  1. middleware resource exists in expected namespace
  2. route actually references that middleware
  3. prefix list matches exact incoming URL segment
  4. service is healthy and reachable
  5. 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 404 rates 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 ReplacePath when 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 IngressRoute or annotated Ingress.
  • Validate rewrite behavior with route tests and backend logs.
  • Choose between StripPrefix and ReplacePath based on desired path semantics.
  • Clear documentation and testing are essential when URL structure and backend routes differ.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.