Skip to content

Traefik headers middleware — security headers, CORS, and rewriting request/response

Use the Traefik headers middleware for HSTS, CSP, CORS, and request rewriting — and the cases where each setting actually does what you expect.

The Traefik headers middleware is the catch-all for HTTP header manipulation — security headers (HSTS, X-Frame-Options, X-Content-Type-Options), CORS (origin, methods, credentials), and arbitrary request/response rewriting. The defaults are sensible but every field has a quirk: HSTS only applies when the response is over HTTPS; some CORS settings interact in non-obvious ways; and the customResponseHeaders empty string is the documented way to remove a header. This article is a working reference for the fields that matter.

How to verify

curl -sI https://app.example.com/ | grep -iE 'strict-transport|x-frame|x-content|referrer-policy|content-security'
curl -sI -X OPTIONS https://api.example.com/ \
  -H "Origin: https://example.com" \
  -H "Access-Control-Request-Method: GET" \
  | grep -iE 'access-control'
# A request to a route with secure-headers should NOT include Server: traefik
curl -sI https://app.example.com/ | grep -i ^server

What’s happening

The headers middleware operates on both directions: request headers (forwarded to the backend) and response headers (returned to the client). The fields split into three groups.

Security headers — stsSeconds, stsIncludeSubdomains, stsPreload, contentTypeNosniff, frameDeny, customFrameOptionsValue, referrerPolicy, permissionsPolicy. These add response headers that browsers honour for security policy. HSTS is the highest-impact one: once set with a preload submission, you commit to HTTPS-only for that hostname for the duration of the header.

CORS — accessControlAllowOriginList, accessControlAllowMethods, accessControlAllowHeaders, accessControlExposeHeaders, accessControlAllowCredentials, accessControlMaxAge, addVaryHeader. Traefik handles the preflight OPTIONS automatically when CORS settings are present. The interaction that bites: accessControlAllowCredentials: true is incompatible with accessControlAllowOriginList: ["*"] per browser spec — you must enumerate origins.

Custom headers — customRequestHeaders (added to the request before it hits the backend), customResponseHeaders (added to the response before it leaves Traefik). An empty string value removes the header. This is the way to strip Server: traefik and any other fingerprint.

The reference

Security headers

http:
  middlewares:
    secure-headers:
      headers:
        # HSTS — one year, include subdomains, preload-ready
        stsSeconds: 31536000
        stsIncludeSubdomains: true
        stsPreload: true

        # MIME sniffing
        contentTypeNosniff: true

        # Clickjacking
        frameDeny: false                    # use customFrameOptionsValue for SAMEORIGIN
        customFrameOptionsValue: "SAMEORIGIN"

        # Referer
        referrerPolicy: "strict-origin-when-cross-origin"

        # Modern replacements / additions
        contentSecurityPolicy: "default-src 'self'; img-src 'self' data: https:; script-src 'self'; style-src 'self' 'unsafe-inline'"
        permissionsPolicy: "geolocation=(), camera=(), microphone=()"

        # Fingerprint stripping
        customResponseHeaders:
          Server: ""
          X-Powered-By: ""

CORS

http:
  middlewares:
    cors-api:
      headers:
        accessControlAllowOriginList:
          - "https://example.com"
          - "https://app.example.com"
        accessControlAllowMethods:
          - GET
          - POST
          - PUT
          - DELETE
          - OPTIONS
        accessControlAllowHeaders:
          - Authorization
          - Content-Type
          - X-Request-Id
        accessControlExposeHeaders:
          - X-Request-Id
          - X-Rate-Limit-Remaining
        accessControlAllowCredentials: true
        accessControlMaxAge: 600
        addVaryHeader: true

accessControlAllowOriginList accepts exact origins; regex matching is available via accessControlAllowOriginListRegex for patterns like https://[a-z0-9-]+\.example\.com.

Request rewriting

http:
  middlewares:
    inject-tenant:
      headers:
        customRequestHeaders:
          X-Tenant: "acme"
          X-Internal: "true"
          # Remove a header before forwarding
          Authorization: ""

Response rewriting

http:
  middlewares:
    add-cache-control:
      headers:
        customResponseHeaders:
          Cache-Control: "public, max-age=3600"
          X-Served-By: "edge-traefik"

Combined production preset

http:
  middlewares:
    web-standard:
      headers:
        stsSeconds: 31536000
        stsIncludeSubdomains: true
        contentTypeNosniff: true
        customFrameOptionsValue: "SAMEORIGIN"
        referrerPolicy: "strict-origin-when-cross-origin"
        contentSecurityPolicy: "default-src 'self'; img-src 'self' data: https:; script-src 'self'"
        customResponseHeaders:
          Server: ""
    api-standard:
      headers:
        stsSeconds: 31536000
        accessControlAllowOriginList:
          - "https://app.example.com"
        accessControlAllowMethods: [GET, POST, PUT, DELETE, OPTIONS]
        accessControlAllowHeaders: [Authorization, Content-Type]
        accessControlAllowCredentials: true
        accessControlMaxAge: 600
        addVaryHeader: true
        customResponseHeaders:
          Server: ""

Common pitfalls

  • HSTS stsPreload: true without actually submitting to the preload list — the header lies; submit at hstspreload.org or remove the flag.
  • Setting both frameDeny: true and customFrameOptionsValue — frameDeny wins and emits DENY; pick one.
  • CORS with accessControlAllowOriginList: ["*"] plus accessControlAllowCredentials: true — browsers refuse the response per spec; enumerate origins.
  • Forgetting addVaryHeader: true when CORS varies by origin — caches conflate responses for different origins.
  • Stripping Authorization in customRequestHeaders to remove a token before backend — works for the visible request but the original is gone for downstream auth; only do this deliberately.

The headers middleware is one of the most-used pieces of platform-level config — a single secure-headers definition referenced by every public router is how clients run a uniform security posture across many apps. The audit of “what does every route emit” is part of the managed operations cadence. For the wider middleware surface see traefik-routers-services-middlewares.