Skip to main content

Headers

The Headers middleware allows adding and removing headers in the requests and responses.


Advanced Configuration

---
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: test-header
namespace: apps
spec:
headers:
customRequestHeaders:
X-Script-Name: "test"
X-Custom-Request-Header: "" # Removes
customResponseHeaders:
X-Custom-Response-Header: "value"

Configuration Options

The Headers middleware allows managing 3 kinds of headers:

Custom Headers

FieldDescriptionDefaultRequired
customRequestHeadersThe customRequestHeaders option lists the header names and values to apply to the request.
Custom headers will overwrite existing headers if they have identical names.
Empty values remove the listed headers.
""false
customResponseHeadersThe customResponseHeaders option lists the header names and values to apply to the response.
Custom headers will overwrite existing headers if they have identical names.
Empty values remove the listed headers.
""false

Security Headers

Security-related headers (HSTS headers, Browser XSS filter, and such) make it possible to use security features by adding headers.

FieldDescriptionDefaultRequired
allowedHostsThe allowedHosts option lists fully qualified domain names that are allowed.""false
hostsProxyHeadersThe hostsProxyHeaders option is a set of header keys that may hold a proxied hostname value for the request.""false
sslTemporaryRedirectSet sslTemporaryRedirect to true to force an SSL redirection using a 302 (instead of a 301).
Deprecated in favor of EntryPoint redirection or the RedirectScheme middleware.
""false
sslRedirectThe sslRedirect only allow HTTPS requests when set to true.
Deprecated in favor of EntryPoint redirection or the RedirectScheme middleware.
""false
sslHostThe sslHost option is the host name that is used to redirect HTTP requests to HTTPS.
Deprecated in favor of the RedirectRegex middleware.
""false
sslForceHostSet sslForceHost to true and set sslHost to force requests to use SSLHost regardless of whether they already use SSL.
Deprecated in favor of the RedirectRegex middleware.
""false
stsSecondsThe stsSeconds is the max-age of the Strict-Transport-Security header.
If set to 0, the header is not set.
""false
stsIncludeSubdomainsIf the stsIncludeSubdomains is set to true, the includeSubDomains directive is appended to the Strict-Transport-Security header.""false
sslProxyHeadersThe sslProxyHeaders option is set of header keys with associated values that would indicate a valid HTTPS request.
It can be useful when using other proxies (example: "X-Forwarded-Proto": "https").
""false
stsPreloadSet stsPreload to true to have the preload flag appended to the Strict-Transport-Security header.""false
forceSTSHeaderSet forceSTSHeader to true to add the STS header even when the connection is HTTP.""false
frameDenySet frameDeny to true to add the X-Frame-Options header with the value of DENY.""false
customFrameOptionsValueThe customFrameOptionsValue allows the X-Frame-Options header value to be set with a custom value.
This overrides the FrameDeny option.
""false
contentTypeNosniffSet contentTypeNosniff to true to add the X-Content-Type-Options header with the value nosniff.""false
browserXssFilterSet browserXssFilter to true to add the X-XSS-Protection header with the value 1; mode=block.""false
customBrowserXSSValueThe customBrowserXssValue option allows the X-XSS-Protection header value to be set with a custom value.
This overrides the BrowserXssFilter option.
""false
contentSecurityPolicyThe contentSecurityPolicy option allows the Content-Security-Policy header value to be set with a custom value.""false
publicKeyThe publicKey implements HPKP to prevent MITM attacks with forged certificates.""false
referrerPolicyThe referrerPolicy allows sites to control whether browsers forward the Referer header to other sites.""false
featurePolicyThe featurePolicy allows sites to control browser features.
Deprecated in favor of permissionsPolicy.
""false
permissionsPolicyThe permissionsPolicy allows sites to control browser features.""false
isDevelopmentSet isDevelopment to true when developing to mitigate the unwanted effects of the AllowedHosts, SSL, and STS options.
Usually testing takes place using HTTP, not HTTPS, and on localhost, not your production domain.
If you would like your development environment to mimic production with complete Host blocking, SSL redirects, and STS headers, leave this as false.
""false
More information about Security Headers

The detailed documentation for security headers can be found in unrolled/secure.

CORS Headers

If CORS headers are set, the middleware does not pass preflight requests to any service. Instead, the response is generated and sent back to the client directly.

FieldDescriptionDefaultRequired
accessControlAllowCredentialsThe accessControlAllowCredentials indicates whether the request can include user credentials.""false
accessControlAllowHeadersThe accessControlAllowHeaders indicates which header field names can be used as part of the request.""false
accessControlAllowMethodsThe accessControlAllowMethods indicates which methods can be used during requests.""false
accessControlAllowOriginListThe accessControlAllowOriginList indicates whether a resource can be shared by returning different values.
More information here
""false
accessControlAllowOriginListRegexThe accessControlAllowOriginListRegex option is the counterpart of the accessControlAllowOriginList option with regular expressions instead of origin values.
It allows all origins that contain any match of a regular expression in the accessControlAllowOriginList.
Regular expressions and replacements can be tested using online tools such as Go Playground or the Regex101.
When defining a regular expression within YAML, any escaped character needs to be escaped twice: example\.com needs to be written as example\\.com.
""false
accessControlExposeHeadersThe accessControlExposeHeaders indicates which headers are safe to expose to the API of a CORS API specification.""false
accessControlMaxAgeThe accessControlMaxAge indicates how many seconds a preflight request can be cached for.""false
addVaryHeaderThe addVaryHeader is used in conjunction with accessControlAllowOriginList to determine whether the Vary header should be added or modified to demonstrate that server responses can differ based on the value of the origin header.""false

accessControlAllowOriginList

A wildcard origin * can also be configured, and matches all requests. If this value is set by a backend service, it will be overwritten by Traefik.

This value can contain a list of allowed origins.

More information including how to use the settings can be found at:

Traefik no longer supports the null value, as it is no longer recommended as a return value.