Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/traefik/traefik/llms.txt

Use this file to discover all available pages before exploring further.

StripPrefix Middleware

Removing Prefixes From the Path Before Forwarding The StripPrefix middleware removes specified prefixes from the URL path before forwarding the request to the service.

Configuration Examples

# Strip prefix /foobar and /fiibar
labels:
  - "traefik.http.middlewares.test-stripprefix.stripprefix.prefixes=/foobar,/fiibar"

Configuration Options

prefixes
array
required
List of prefixes to strip from the request URL.For instance, /products also matches /products/shoes and /products/shirts.The middleware stores the stripped prefix in the X-Forwarded-Prefix header.
forceSlash
boolean
default:"true"
Ensures the resulting stripped path is not an empty string by replacing it with / when necessary.
This option is deprecated and should not be used.

How It Works

The StripPrefix middleware:
  1. Strips the matching path prefix from the request URL
  2. Stores the stripped prefix in the X-Forwarded-Prefix header
  3. Forwards the modified request to the backend service
Backend Asset URLsIf your backend is serving assets (e.g., images or JavaScript files), it can use the X-Forwarded-Prefix header to properly construct relative URLs.For example, if the original request is /products/shoes/image.png and you strip /products, the backend should return URLs like /products/shoes/image.png (not /image.png).

Usage Examples

Exposing Backend on a Subpath

Use StripPrefix when your backend listens on the root path (/) but should be exposed on a specific prefix:
http:
  routers:
    my-router:
      rule: "Host(`example.com`) && PathPrefix(`/api`)"
      service: my-api
      middlewares:
        - api-strip-prefix

  middlewares:
    api-strip-prefix:
      stripPrefix:
        prefixes:
          - "/api"

  services:
    my-api:
      loadBalancer:
        servers:
          - url: "http://backend:8080"
In this example:
  • Request: https://example.com/api/users
  • Forwarded to backend: http://backend:8080/users
  • X-Forwarded-Prefix: /api

Multiple Prefix Stripping

http:
  middlewares:
    multi-strip:
      stripPrefix:
        prefixes:
          - "/v1"
          - "/v2"
          - "/api"

Combining with Other Middlewares

http:
  routers:
    app-router:
      rule: "Host(`example.com`) && PathPrefix(`/app`)"
      service: app-service
      middlewares:
        - app-auth
        - app-strip
        - app-headers

  middlewares:
    app-auth:
      basicAuth:
        users:
          - "user:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"
    
    app-strip:
      stripPrefix:
        prefixes:
          - "/app"
    
    app-headers:
      headers:
        customRequestHeaders:
          X-Custom-Header: "value"