2019-02-26 13:50:07 +00:00
# Headers
Adding Headers to the Request / Response
{: .subtitle }
![Headers ](../assets/img/middleware/headers.png )
The Headers middleware can manage the requests/responses headers.
## Configuration Examples
### Adding Headers to the Request and the Response
Add the `X-Script-Name` header to the proxied request and the `X-Custom-Response-Header` to the response
2019-03-29 11:34:05 +00:00
```yaml tab="Docker"
labels:
2019-04-02 08:40:04 +00:00
- "traefik.http.middlewares.testHeader.Headers.CustomRequestHeaders.X-Script-Name=test"
- "traefik.http.middlewares.testHeader.Headers.CustomResponseHeaders.X-Custom-Response-Header=True"
```
```yaml tab="Kubernetes"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: testHeader
spec:
headers:
2019-06-17 16:30:05 +00:00
customRequestHeaders:
2019-04-02 08:40:04 +00:00
X-Script-Name: "test"
2019-06-17 16:30:05 +00:00
customResponseHeaders:
2019-04-02 08:40:04 +00:00
X-Custom-Response-Header: "True"
2019-03-29 11:34:05 +00:00
```
2019-04-15 16:22:07 +00:00
```json tab="Marathon"
"labels": {
"traefik.http.middlewares.testHeader.Headers.CustomRequestHeaders.X-Script-Name": "test",
"traefik.http.middlewares.testHeader.Headers.CustomResponseHeaders.X-Custom-Response-Header": "True"
}
```
2019-04-08 15:14:08 +00:00
```yaml tab="Rancher"
labels:
- "traefik.http.middlewares.testHeader.Headers.CustomRequestHeaders.X-Script-Name=test"
- "traefik.http.middlewares.testHeader.Headers.CustomResponseHeaders.X-Custom-Response-Header=True"
```
2019-03-29 11:34:05 +00:00
```toml tab="File"
[http.middlewares]
[http.middlewares.testHeader.headers]
[http.middlewares.testHeader.headers.CustomRequestHeaders]
X-Script-Name = "test"
[http.middlewares.testHeader.headers.CustomResponseHeaders]
X-Custom-Response-Header = "True"
```
2019-02-26 13:50:07 +00:00
### Adding and Removing Headers
2019-04-05 13:18:04 +00:00
`X-Script-Name` header added to the proxied request, the `X-Custom-Request-Header` header removed from the request,
and the `X-Custom-Response-Header` header removed from the response.
2019-02-26 13:50:07 +00:00
2019-04-24 15:44:04 +00:00
Please note that is not possible to remove headers through the use of labels (Docker, Rancher, Marathon, ...) for now.
2019-02-26 13:50:07 +00:00
2019-04-02 08:40:04 +00:00
```yaml tab="Kubernetes"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: testHeader
spec:
headers:
2019-06-17 16:30:05 +00:00
customRequestHeaders:
2019-04-05 13:18:04 +00:00
X-Script-Name: "test" # Adds
X-Custom-Request-Header: "" # Removes
2019-06-17 16:30:05 +00:00
customResponseHeaders:
2019-04-05 13:18:04 +00:00
X-Custom-Response-Header: "" # Removes
2019-04-02 08:40:04 +00:00
```
2019-02-26 13:50:07 +00:00
2019-04-08 15:14:08 +00:00
```yaml tab="Rancher"
labels:
- "traefik.http.middlewares.testHeader.Headers.CustomRequestHeaders.X-Script-Name=test"
2019-04-24 15:44:04 +00:00
```
```json tab="Marathon"
"labels": {
"traefik.http.middlewares.testHeader.Headers.CustomRequestHeaders.X-Script-Name": "test",
}
2019-04-08 15:14:08 +00:00
```
2019-04-02 08:40:04 +00:00
```toml tab="File"
[http.middlewares]
[http.middlewares.testHeader.headers]
[http.middlewares.testHeader.headers.CustomRequestHeaders]
2019-04-05 13:18:04 +00:00
X-Script-Name = "test" # Adds
X-Custom-Request-Header = "" # Removes
2019-04-02 08:40:04 +00:00
[http.middlewares.testHeader.headers.CustomResponseHeaders]
2019-04-05 13:18:04 +00:00
X-Custom-Response-Header = "" # Removes
2019-04-02 08:40:04 +00:00
```
2019-02-26 13:50:07 +00:00
### Using Security Headers
Security related headers (HSTS headers, SSL redirection, Browser XSS filter, etc) can be added and configured per frontend in a similar manner to the custom headers above.
This functionality allows for some easy security features to quickly be set.
2019-04-02 08:40:04 +00:00
```yaml tab="Docker"
labels:
- "traefik.http.middlewares.testHeader.Headers.FrameDeny=true"
- "traefik.http.middlewares.testHeader.Headers.SSLRedirect=true"
```
```yaml tab="Kubernetes"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: testHeader
spec:
headers:
2019-06-17 16:30:05 +00:00
frameDeny: "true"
sslRedirect: "true"
2019-04-02 08:40:04 +00:00
```
2019-04-08 15:14:08 +00:00
```yaml tab="Rancher"
labels:
- "traefik.http.middlewares.testHeader.Headers.FrameDeny=true"
- "traefik.http.middlewares.testHeader.Headers.SSLRedirect=true"
```
2019-04-24 15:44:04 +00:00
```json tab="Marathon"
"labels": {
"traefik.http.middlewares.testHeader.Headers.FrameDeny": "true",
"traefik.http.middlewares.testHeader.Headers.SSLRedirect": "true"
}
```
2019-04-02 08:40:04 +00:00
```toml tab="File"
[http.middlewares]
[http.middlewares.testHeader.headers]
FrameDeny = true
SSLRedirect = true
```
### CORS Headers
CORS (Cross-Origin Resource Sharing) headers can be added and configured per frontend in a similar manner to the custom headers above.
This functionality allows for more advanced security features to quickly be set.
```yaml tab="Docker"
labels:
- "traefik.http.middlewares.testHeader.Headers.AccessControlAllowMethods=GET,OPTIONS,PUT"
- "traefik.http.middlewares.testHeader.Headers.AccessControlAllowOrigin=origin-list-or-null"
- "traefik.http.middlewares.testHeader.Headers.AccessControlMaxAge=100"
- "traefik.http.middlewares.testHeader.Headers.AddVaryHeader=true"
```
```yaml tab="Kubernetes"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: testHeader
spec:
headers:
AccessControlAllowMethods:
- "GET"
- "OPTIONS"
- "PUT"
AccessControlAllowOrigin: "origin-list-or-null"
AccessControlMaxAge: 100
AddVaryHeader: "true"
```
2019-04-08 15:14:08 +00:00
```yaml tab="Rancher"
labels:
- "traefik.http.middlewares.testHeader.Headers.AccessControlAllowMethods=GET,OPTIONS,PUT"
- "traefik.http.middlewares.testHeader.Headers.AccessControlAllowOrigin=origin-list-or-null"
- "traefik.http.middlewares.testHeader.Headers.AccessControlMaxAge=100"
- "traefik.http.middlewares.testHeader.Headers.AddVaryHeader=true"
```
2019-04-24 15:44:04 +00:00
```json tab="Marathon"
"labels": {
"traefik.http.middlewares.testHeader.Headers.AccessControlAllowMethods": "GET,OPTIONS,PUT",
"traefik.http.middlewares.testHeader.Headers.AccessControlAllowOrigin": "origin-list-or-null",
"traefik.http.middlewares.testHeader.Headers.AccessControlMaxAge": "100",
"traefik.http.middlewares.testHeader.Headers.AddVaryHeader": "true"
}
```
2019-04-02 08:40:04 +00:00
```toml tab="File"
[http.middlewares]
[http.middlewares.testHeader.headers]
AccessControlAllowMethods= ["GET", "OPTIONS", "PUT"]
AccessControlAllowOrigin = "origin-list-or-null"
AccessControlMaxAge = 100
AddVaryHeader = true
```
2019-02-26 13:50:07 +00:00
## Configuration Options
### General
!!! warning
If the custom header name is the same as one header name of the request or response, it will be replaced.
!!! note
The detailed documentation for the security headers can be found in [unrolled/secure ](https://github.com/unrolled/secure#available-options ).
2019-04-03 12:32:04 +00:00
### `customRequestHeaders`
2019-02-26 13:50:07 +00:00
The `customRequestHeaders` option lists the Header names and values to apply to the request.
2019-04-03 12:32:04 +00:00
### `customResponseHeaders`
2019-04-02 08:40:04 +00:00
The `customResponseHeaders` option lists the Header names and values to apply to the response.
2019-04-03 12:32:04 +00:00
### `accessControlAllowCredentials`
2019-04-02 08:40:04 +00:00
The `accessControlAllowCredentials` indicates whether the request can include user credentials.
2019-04-03 12:32:04 +00:00
### `accessControlAllowHeaders`
2019-04-02 08:40:04 +00:00
The `accessControlAllowHeaders` indicates which header field names can be used as part of the request.
2019-04-03 12:32:04 +00:00
### `accessControlAllowMethods`
2019-04-02 08:40:04 +00:00
The `accessControlAllowMethods` indicates which methods can be used during requests.
2019-04-03 12:32:04 +00:00
### `accessControlAllowOrigin`
2019-04-02 08:40:04 +00:00
The `accessControlAllowOrigin` indicates whether a resource can be shared by returning different values. The three options for this value are:
- `origin-list-or-null`
- `*`
- `null`
2019-04-03 12:32:04 +00:00
### `accessControlExposeHeaders`
2019-04-02 08:40:04 +00:00
The `accessControlExposeHeaders` indicates which headers are safe to expose to the api of a CORS API specification.
2019-04-03 12:32:04 +00:00
### `accessControlMaxAge`
2019-04-02 08:40:04 +00:00
The `accessControlMaxAge` indicates how long a preflight request can be cached.
2019-04-03 12:32:04 +00:00
### `addVaryHeader`
2019-04-02 08:40:04 +00:00
The `addVaryHeader` is used in conjunction with `accessControlAllowOrigin` to determine whether the vary header should be added or modified to demonstrate that server responses can differ beased on the value of the origin header.
2019-04-03 12:32:04 +00:00
### `allowedHosts`
2019-02-26 13:50:07 +00:00
The `allowedHosts` option lists fully qualified domain names that are allowed.
2019-04-03 12:32:04 +00:00
### `hostsProxyHeaders`
2019-02-26 13:50:07 +00:00
The `hostsProxyHeaders` option is a set of header keys that may hold a proxied hostname value for the request.
2019-04-03 12:32:04 +00:00
### `sslRedirect`
2019-02-26 13:50:07 +00:00
The `sslRedirect` is set to true, then only allow https requests.
2019-04-03 12:32:04 +00:00
### `sslTemporaryRedirect`
2019-02-26 13:50:07 +00:00
Set the `sslTemporaryRedirect` to `true` to force an SSL redirection using a 302 (instead of a 301).
2019-04-03 12:32:04 +00:00
### `sslHost`
2019-02-26 13:50:07 +00:00
The `SSLHost` option is the host name that is used to redirect http requests to https.
2019-04-03 12:32:04 +00:00
### `sslProxyHeaders`
2019-02-26 13:50:07 +00:00
The `sslProxyHeaders` option is set of header keys with associated values that would indicate a valid https request. Useful when using other proxies with header like: `"X-Forwarded-Proto": "https"` .
2019-04-03 12:32:04 +00:00
### `sslForceHost`
2019-02-26 13:50:07 +00:00
Set `sslForceHost` to true and set SSLHost to forced requests to use `SSLHost` even the ones that are already using SSL.
2019-04-03 12:32:04 +00:00
### `stsSeconds`
2019-02-26 13:50:07 +00:00
The `stsSeconds` is the max-age of the Strict-Transport-Security header. If set to 0, would NOT include the header.
2019-04-03 12:32:04 +00:00
### `stsIncludeSubdomains`
2019-02-26 13:50:07 +00:00
The `stsIncludeSubdomains` is set to true, the `includeSubdomains` will be appended to the Strict-Transport-Security header.
2019-04-03 12:32:04 +00:00
### `stsPreload`
2019-02-26 13:50:07 +00:00
Set `STSPreload` to true to have the `preload` flag appended to the Strict-Transport-Security header.
2019-04-03 12:32:04 +00:00
### `forceSTSHeader`
2019-02-26 13:50:07 +00:00
Set `ForceSTSHeader` to true, to add the STS header even when the connection is HTTP.
2019-04-03 12:32:04 +00:00
### `frameDeny`
2019-02-26 13:50:07 +00:00
Set `frameDeny` to true to add the `X-Frame-Options` header with the value of `DENY` .
2019-04-03 12:32:04 +00:00
### `customFrameOptionsValue`
2019-02-26 13:50:07 +00:00
The `customFrameOptionsValue` allows the `X-Frame-Options` header value to be set with a custom value. This overrides the FrameDeny option.
2019-04-03 12:32:04 +00:00
### `contentTypeNosniff`
2019-02-26 13:50:07 +00:00
Set `contentTypeNosniff` to true to add the `X-Content-Type-Options` header with the value `nosniff` .
2019-04-03 12:32:04 +00:00
### `browserXssFilter`
2019-02-26 13:50:07 +00:00
Set `BrowserXssFilter` to true to add the `X-XSS-Protection` header with the value `1; mode=block` .
2019-04-03 12:32:04 +00:00
### `customBrowserXSSValue`
2019-02-26 13:50:07 +00:00
The `customBrowserXssValue` option allows the `X-XSS-Protection` header value to be set with a custom value. This overrides the BrowserXssFilter option.
2019-04-03 12:32:04 +00:00
### `contentSecurityPolicy`
2019-02-26 13:50:07 +00:00
The `contentSecurityPolicy` option allows the `Content-Security-Policy` header value to be set with a custom value.
2019-04-03 12:32:04 +00:00
### `publicKey`
2019-02-26 13:50:07 +00:00
The `publicKey` implements HPKP to prevent MITM attacks with forged certificates.
2019-04-03 12:32:04 +00:00
### `referrerPolicy`
2019-02-26 13:50:07 +00:00
The `referrerPolicy` allows sites to control when browsers will pass the Referer header to other sites.
2019-04-03 12:32:04 +00:00
### `isDevelopment`
2019-02-26 13:50:07 +00:00
Set `isDevelopment` to true when developing. The AllowedHosts, SSL, and STS options can cause some unwanted effects. Usually testing happens on 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.