Buffering
The Buffering middleware limits the size of requests that can be forwarded to services.
With Buffering, Traefik reads the entire request into memory (possibly buffering large requests into disk), and rejects requests that are over a specified size limit.
This can help services avoid large amounts of data (multipart/form-data
for example), and can minimize the time spent sending data to a service.
Docker
Kubernetes
# Sets the maximum request body to 2MB
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: limit
spec:
buffering:
maxRequestBodyBytes: 2000000
Consul Catalog
# Sets the maximum request body to 2MB
- "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
Marathon
"labels": {
"traefik.http.middlewares.limit.buffering.maxRequestBodyBytes": "2000000"
}
Rancher
# Sets the maximum request body to 2MB
labels:
- "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
File (YAML)
# Sets the maximum request body to 2MB
http:
middlewares:
limit:
buffering:
maxRequestBodyBytes: 2000000
File (TOML)
# Sets the maximum request body to 2MB
[http.middlewares]
[http.middlewares.limit.buffering]
maxRequestBodyBytes = 2000000
Configuration Options
Optional, Default=0
The maxRequestBodyBytes
option configures the maximum allowed body size for the request (in bytes).
If the request exceeds the allowed size, it is not forwarded to the service, and the client gets a 413
(Request Entity Too Large) response.
Docker
labels:
- "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
Kubernetes
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: limit
spec:
buffering:
maxRequestBodyBytes: 2000000
Consul Catalog
- "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
Marathon
"labels": {
"traefik.http.middlewares.limit.buffering.maxRequestBodyBytes": "2000000"
}
Rancher
labels:
- "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
middlewares:
limit:
maxRequestBodyBytes: 2000000
File (TOML)
[http.middlewares]
[http.middlewares.limit.buffering]
maxRequestBodyBytes = 2000000
Optional, Default=1048576
You can configure a threshold (in bytes) from which the request will be buffered on disk instead of in memory with the memRequestBodyBytes
option.
Docker
Kubernetes
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: limit
spec:
buffering:
memRequestBodyBytes: 2000000
Consul Catalog
- "traefik.http.middlewares.limit.buffering.memRequestBodyBytes=2000000"
Marathon
"labels": {
"traefik.http.middlewares.limit.buffering.memRequestBodyBytes": "2000000"
}
Rancher
labels:
- "traefik.http.middlewares.limit.buffering.memRequestBodyBytes=2000000"
File (YAML)
http:
middlewares:
limit:
buffering:
memRequestBodyBytes: 2000000
File (TOML)
[http.middlewares]
[http.middlewares.limit.buffering]
memRequestBodyBytes = 2000000
Optional, Default=0
The maxResponseBodyBytes
option configures the maximum allowed response size from the service (in bytes).
If the response exceeds the allowed size, it is not forwarded to the client. The client gets a 500
(Internal Server Error) response instead.
Docker
labels:
- "traefik.http.middlewares.limit.buffering.maxResponseBodyBytes=2000000"
Kubernetes
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: limit
spec:
buffering:
maxResponseBodyBytes: 2000000
Consul Catalog
- "traefik.http.middlewares.limit.buffering.maxResponseBodyBytes=2000000"
Marathon
"labels": {
"traefik.http.middlewares.limit.buffering.maxResponseBodyBytes": "2000000"
}
Rancher
labels:
- "traefik.http.middlewares.limit.buffering.maxResponseBodyBytes=2000000"
File (YAML)
http:
middlewares:
limit:
buffering:
maxResponseBodyBytes: 2000000
[http.middlewares.limit.buffering]
Optional, Default=1048576
You can configure a threshold (in bytes) from which the response will be buffered on disk instead of in memory with the memResponseBodyBytes
option.
Docker
Kubernetes
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: limit
spec:
buffering:
memResponseBodyBytes: 2000000
Consul Catalog
- "traefik.http.middlewares.limit.buffering.memResponseBodyBytes=2000000"
Marathon
"labels": {
"traefik.http.middlewares.limit.buffering.memResponseBodyBytes": "2000000"
}
Rancher
labels:
- "traefik.http.middlewares.limit.buffering.memResponseBodyBytes=2000000"
File (YAML)
http:
middlewares:
limit:
buffering:
memResponseBodyBytes: 2000000
File (TOML)
[http.middlewares]
[http.middlewares.limit.buffering]
memResponseBodyBytes = 2000000
Optional, Default=””
You can have the Buffering middleware replay the request using retryExpression
.
Retries once in the case of a network error
Docker
labels:
- "traefik.http.middlewares.limit.buffering.retryExpression=IsNetworkError() && Attempts() < 2"
Kubernetes
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: limit
spec:
buffering:
retryExpression: "IsNetworkError() && Attempts() < 2"
Consul Catalog
- "traefik.http.middlewares.limit.buffering.retryExpression=IsNetworkError() && Attempts() < 2"
Marathon
"labels": {
"traefik.http.middlewares.limit.buffering.retryExpression": "IsNetworkError() && Attempts() < 2"
}
Rancher
labels:
- "traefik.http.middlewares.limit.buffering.retryExpression=IsNetworkError() && Attempts() < 2"
File (YAML)
http:
middlewares:
limit:
buffering:
retryExpression: "IsNetworkError() && Attempts() < 2"
File (TOML)
[http.middlewares]
[http.middlewares.limit.buffering]
retryExpression = "IsNetworkError() && Attempts() < 2"
Attempts()
number of attempts (the first one counts)ResponseCode()
response code of the service