2019-02-26 13:50:07 +00:00
# Buffering
How to Read the Request before Forwarding It
{: .subtitle }
![Buffering ](../assets/img/middleware/buffering.png )
The Buffering middleware gives you control on how you want to read the requests before sending them 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 limit.
This can help services deal with large data (multipart/form-data for example), and can minimize time spent sending data to a service.
## Configuration Examples
2019-03-29 11:34:05 +00:00
```yaml tab="Docker"
# Sets the maximum request body to 2Mb
labels:
2019-09-23 12:32:04 +00:00
- "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
2019-04-03 12:32:04 +00:00
```
```yaml tab="Kubernetes"
# Sets the maximum request body to 2Mb
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: limit
spec:
buffering:
2019-09-23 12:32:04 +00:00
maxRequestBodyBytes: 2000000
2019-03-29 11:34:05 +00:00
```
2019-04-15 16:22:07 +00:00
```json tab="Marathon"
"labels": {
2019-09-23 12:32:04 +00:00
"traefik.http.middlewares.limit.buffering.maxRequestBodyBytes": "2000000"
2019-04-15 16:22:07 +00:00
}
```
2019-04-08 15:14:08 +00:00
```yaml tab="Rancher"
# Sets the maximum request body to 2Mb
labels:
2019-09-23 12:32:04 +00:00
- "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
2019-04-08 15:14:08 +00:00
```
2019-07-22 07:58:04 +00:00
```toml tab="File (TOML)"
2019-03-29 11:34:05 +00:00
# Sets the maximum request body to 2Mb
[http.middlewares]
2019-04-03 12:32:04 +00:00
[http.middlewares.limit.buffering]
2019-09-23 12:32:04 +00:00
maxRequestBodyBytes = 2000000
2019-03-29 11:34:05 +00:00
```
2019-02-26 13:50:07 +00:00
2019-07-22 07:58:04 +00:00
```yaml tab="File (YAML)"
# Sets the maximum request body to 2Mb
http:
middlewares:
limit:
buffering:
2019-09-23 12:32:04 +00:00
maxRequestBodyBytes: 2000000
2019-07-22 07:58:04 +00:00
```
2019-02-26 13:50:07 +00:00
## Configuration Options
2019-04-03 12:32:04 +00:00
### `maxRequestBodyBytes`
2019-02-26 13:50:07 +00:00
With the `maxRequestBodyBytes` option, you can configure the maximum allowed body size for the request (in Bytes).
2019-09-23 12:32:04 +00:00
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.
```yaml tab="Docker"
labels:
- "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
```
```yaml tab="Kubernetes"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: limit
spec:
buffering:
maxRequestBodyBytes: 2000000
```
```json tab="Marathon"
"labels": {
"traefik.http.middlewares.limit.buffering.maxRequestBodyBytes": "2000000"
}
```
```yaml tab="Rancher"
labels:
- "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
```
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.limit.buffering]
maxRequestBodyBytes = 2000000
```
```yaml tab="File (YAML)"
http:
middlewares:
limit:
buffering:
maxRequestBodyBytes: 2000000
```
2019-02-26 13:50:07 +00:00
2019-04-03 12:32:04 +00:00
### `memRequestBodyBytes`
2019-02-26 13:50:07 +00:00
2019-09-23 12:32:04 +00:00
You can configure a threshold (in Bytes) from which the request will be buffered on disk instead of in memory with the `memRequestBodyBytes` option.
```yaml tab="Docker"
labels:
- "traefik.http.middlewares.limit.buffering.memRequestBodyBytes=2000000"
```
```yaml tab="Kubernetes"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: limit
spec:
buffering:
memRequestBodyBytes: 2000000
```
```json tab="Marathon"
"labels": {
"traefik.http.middlewares.limit.buffering.memRequestBodyBytes": "2000000"
}
```
```yaml tab="Rancher"
labels:
- "traefik.http.middlewares.limit.buffering.memRequestBodyBytes=2000000"
```
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.limit.buffering]
memRequestBodyBytes = 2000000
```
```yaml tab="File (YAML)"
http:
middlewares:
limit:
buffering:
memRequestBodyBytes: 2000000
```
2019-02-26 13:50:07 +00:00
2019-04-03 12:32:04 +00:00
### `maxResponseBodyBytes`
2019-02-26 13:50:07 +00:00
With the `maxReesponseBodyBytes` option, you can configure 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 `413 (Request Entity Too Large) response` instead.
2019-09-23 12:32:04 +00:00
```yaml tab="Docker"
labels:
- "traefik.http.middlewares.limit.buffering.maxResponseBodyBytes=2000000"
```
```yaml tab="Kubernetes"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: limit
spec:
buffering:
maxResponseBodyBytes: 2000000
```
```json tab="Marathon"
"labels": {
"traefik.http.middlewares.limit.buffering.maxResponseBodyBytes": "2000000"
}
```
```yaml tab="Rancher"
labels:
- "traefik.http.middlewares.limit.buffering.maxResponseBodyBytes=2000000"
```
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.limit.buffering]
maxResponseBodyBytes = 2000000
```
```yaml tab="File (YAML)"
http:
middlewares:
limit:
buffering:
maxResponseBodyBytes: 2000000
```
2019-04-03 12:32:04 +00:00
### `memResponseBodyBytes`
2019-02-26 13:50:07 +00:00
2019-09-23 12:32:04 +00:00
You can configure a threshold (in Bytes) from which the response will be buffered on disk instead of in memory with the `memResponseBodyBytes` option.
```yaml tab="Docker"
labels:
- "traefik.http.middlewares.limit.buffering.memResponseBodyBytes=2000000"
```
```yaml tab="Kubernetes"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: limit
spec:
buffering:
memResponseBodyBytes: 2000000
```
```json tab="Marathon"
"labels": {
"traefik.http.middlewares.limit.buffering.memResponseBodyBytes": "2000000"
}
```
```yaml tab="Rancher"
labels:
- "traefik.http.middlewares.limit.buffering.memResponseBodyBytes=2000000"
```
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.limit.buffering]
memResponseBodyBytes = 2000000
```
```yaml tab="File (YAML)"
http:
middlewares:
limit:
buffering:
memResponseBodyBytes: 2000000
```
2019-02-26 13:50:07 +00:00
2019-04-03 12:32:04 +00:00
### `retryExpression`
2019-02-26 13:50:07 +00:00
You can have the Buffering middleware replay the request with the help of the `retryExpression` option.
2019-09-23 12:32:04 +00:00
??? example "Retries once in case of a network error"
```yaml tab="Docker"
labels:
- "traefik.http.middlewares.limit.buffering.retryExpression=IsNetworkError() && Attempts() < 2 "
```
2019-02-26 13:50:07 +00:00
2019-09-23 12:32:04 +00:00
```yaml tab="Kubernetes"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: limit
spec:
buffering:
retryExpression: "IsNetworkError() & & Attempts() < 2 "
```
```json tab="Marathon"
"labels": {
"traefik.http.middlewares.limit.buffering.retryExpression": "IsNetworkError() & & Attempts() < 2 "
}
```
```yaml tab="Rancher"
labels:
- "traefik.http.middlewares.limit.buffering.retryExpression=IsNetworkError() && Attempts() < 2 "
2019-02-26 13:50:07 +00:00
```
2019-09-23 12:32:04 +00:00
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.limit.buffering]
retryExpression = "IsNetworkError() & & Attempts() < 2 "
```
```yaml tab="File (YAML)"
http:
middlewares:
limit:
buffering:
retryExpression: "IsNetworkError() & & Attempts() < 2 "
```
The retry expression is defined as a logical combination of the functions below with the operators AND (`& & `) and OR (`||`). At least one function is required:
2019-02-26 13:50:07 +00:00
- `Attempts()` number of attempts (the first one counts)
- `ResponseCode()` response code of the service
- `IsNetworkError()` - if the response code is related to networking error