This episode covers the Traefik headers middleware: custom request and response headers, the security header suite such as Content-Security-Policy, X-Frame-Options, X-Content-Type-Options, and Referrer-Policy, complete CORS configuration with preflight, and HSTS settings for forcing HTTPS.

HTTP headers are the silent language between the browser and the server. Traefik's headers middleware gives you full control over that language: adding, removing, or replacing headers on both the request and response paths. Episode 10 covers this middleware thoroughly, plus two big themes: the security headers suite and CORS.
Why does this matter? Many classic web attacks — clickjacking, MIME sniffing, CSP-based XSS — are prevented not by the application, but by correct headers. Installing security headers in Traefik means all services behind it are protected at once, without changing a single line of application code. That is the value of a proxy as a centralized control point.
The headers middleware has four option groups: customRequestHeaders, customResponseHeaders, plus options to remove both. The headers middleware configuration always lives under http.middlewares:
http:
middlewares:
app-headers:
headers:
customRequestHeaders:
X-Source: "traefik-edge"
customResponseHeaders:
X-Powered-By: "Backend Cluster"
Cache-Control: "no-store"
customRequestHeadersRegex:
X-User-{name}: "{value}"customRequestHeaders: added to the request before being forwarded to the backend — useful for marking traffic origin or adding headers the backend needs.customResponseHeaders: added to the response before being sent to the client.Regex variants allow dynamic templates with capture groups from the request.Two older but still useful protections are enabled through dedicated options:
http:
middlewares:
old-school-protect:
headers:
customResponseHeaders:
X-XSS-Protection: "1; mode=block"
X-Content-Type-Options: "nosniff"X-Content-Type-Options: nosniff forbids the browser from guessing content types (MIME sniffing) — an important prevention for file upload based attacks.
The suite of modern security headers recommended for nearly every web service:
http:
middlewares:
secure:
headers:
contentSecurityPolicy: "default-src 'self'; frame-ancestors 'none'"
frameDeny: true
contentTypeNosniff: true
browserXssFilter: true
referrerPolicy: "no-referrer"
permissionsPolicy: "camera=(), microphone=(), geolocation=()"
forceSTSHeader: true
stsSeconds: 31536000
stsIncludeSubdomains: true
stsPreload: truedefault-src 'self' then loosen it according to application needs.For applications that do not use these headers at all, attaching the middleware above to every router already significantly improves the security posture. CSP settings that are too strict can break an application — test on staging first.
CORS (Cross-Origin Resource Sharing) governs whether the browser may call your API from another domain. Traefik's cors middleware configures this centrally:
http:
middlewares:
api-cors:
headers:
accessControlAllowOriginList:
- "https://app.example.com"
- "https://staging.example.com"
accessControlAllowMethods:
- GET
- POST
- OPTIONS
accessControlAllowHeaders:
- Content-Type
- Authorization
accessControlAllowCredentials: true
accessControlMaxAge: 3600accessControlAllowOriginList: the list of allowed origins — never use a wildcard together with credentials.accessControlAllowMethods: allowed methods.accessControlAllowHeaders: headers a cross-origin request may send.accessControlAllowCredentials: true: allows cross-origin cookies.accessControlMaxAge: how long the browser caches a preflight result.For complex requests, the browser first sends a preflight OPTIONS request. Traefik handles preflight automatically when the CORS middleware is active: if the method and headers match the configuration, Traefik replies with 204 without forwarding the request to the backend. This saves backend load and speeds up browser responses.
HSTS (HTTP Strict Transport Security) tells the browser: "from now on, only access this site over HTTPS". This prevents downgrade and SSL stripping attacks. Its configuration was already visible in the security headers section: stsSeconds is the policy duration in seconds, stsIncludeSubdomains extends it to all subdomains, and stsPreload registers the domain in the browser preload list.
The correct HSTS rollout rules:
stsSeconds, e.g. 300 seconds, for testing.stsPreload once confident — removal from the preload list is very difficult.Key takeaways:
headers middleware manages custom request and response headers.In episode 11 next we will cover path & request middlewares — AddPrefix, StripPrefix, ReplacePath, ReplacePathRegex, RedirectScheme to force HTTPS, and RedirectRegex for pattern-based redirects. These are the middlewares that align public URLs with an application's internal structure.