traefik/docs/content/middlewares/http/buffering.md

267 lines
6.3 KiB
Markdown
Raw Permalink Normal View History

---
title: "Traefik Buffering Documentation"
description: "The HTTP buffering middleware in Traefik Proxy limits the size of requests that can be forwarded to Services. Read the technical documentation."
---
# Buffering
How to Read the Request before Forwarding It
{: .subtitle }
2021-06-11 13:30:05 +00:00
![Buffering](../../assets/img/middleware/buffering.png)
2021-02-11 13:34:04 +00:00
The Buffering middleware limits the size of requests that can be forwarded to services.
2021-02-11 13:34:04 +00:00
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.
2021-02-11 13:34:04 +00:00
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.
## Configuration Examples
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2021-02-11 13:34:04 +00:00
# Sets the maximum request body to 2MB
2019-03-29 11:34:05 +00:00
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
2019-04-03 12:32:04 +00:00
```
```yaml tab="Kubernetes"
2021-02-11 13:34:04 +00:00
# Sets the maximum request body to 2MB
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-04-03 12:32:04 +00:00
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-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
2021-02-11 13:34:04 +00:00
# Sets the maximum request body to 2MB
2019-10-15 15:34:08 +00:00
- "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
```
2019-07-22 07:58:04 +00:00
```yaml tab="File (YAML)"
2021-02-11 13:34:04 +00:00
# Sets the maximum request body to 2MB
2019-07-22 07:58:04 +00:00
http:
middlewares:
limit:
buffering:
2019-09-23 12:32:04 +00:00
maxRequestBodyBytes: 2000000
2019-07-22 07:58:04 +00:00
```
```toml tab="File (TOML)"
# Sets the maximum request body to 2MB
[http.middlewares]
[http.middlewares.limit.buffering]
maxRequestBodyBytes = 2000000
```
## Configuration Options
2019-04-03 12:32:04 +00:00
### `maxRequestBodyBytes`
_Optional, Default=0_
2021-02-11 13:34:04 +00:00
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.
2019-09-23 12:32:04 +00:00
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2019-09-23 12:32:04 +00:00
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
2019-09-23 12:32:04 +00:00
```
```yaml tab="Kubernetes"
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-09-23 12:32:04 +00:00
kind: Middleware
metadata:
name: limit
spec:
buffering:
maxRequestBodyBytes: 2000000
```
2019-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
- "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
```
2019-09-23 12:32:04 +00:00
```yaml tab="File (YAML)"
http:
middlewares:
limit:
buffering:
maxRequestBodyBytes: 2000000
```
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.limit.buffering]
maxRequestBodyBytes = 2000000
```
2019-04-03 12:32:04 +00:00
### `memRequestBodyBytes`
_Optional, Default=1048576_
2021-02-11 13:34: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.
2019-09-23 12:32:04 +00:00
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2019-09-23 12:32:04 +00:00
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.limit.buffering.memRequestBodyBytes=2000000"
2019-09-23 12:32:04 +00:00
```
```yaml tab="Kubernetes"
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-09-23 12:32:04 +00:00
kind: Middleware
metadata:
name: limit
spec:
buffering:
memRequestBodyBytes: 2000000
```
2019-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
- "traefik.http.middlewares.limit.buffering.memRequestBodyBytes=2000000"
```
2019-09-23 12:32:04 +00:00
```yaml tab="File (YAML)"
http:
middlewares:
limit:
buffering:
memRequestBodyBytes: 2000000
```
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.limit.buffering]
memRequestBodyBytes = 2000000
```
2019-04-03 12:32:04 +00:00
### `maxResponseBodyBytes`
_Optional, Default=0_
2021-02-11 13:34:04 +00:00
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.
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2019-09-23 12:32:04 +00:00
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.limit.buffering.maxResponseBodyBytes=2000000"
2019-09-23 12:32:04 +00:00
```
```yaml tab="Kubernetes"
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-09-23 12:32:04 +00:00
kind: Middleware
metadata:
name: limit
spec:
buffering:
maxResponseBodyBytes: 2000000
```
2019-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
- "traefik.http.middlewares.limit.buffering.maxResponseBodyBytes=2000000"
```
2019-09-23 12:32:04 +00:00
```yaml tab="File (YAML)"
http:
middlewares:
limit:
buffering:
maxResponseBodyBytes: 2000000
```
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.limit.buffering]
maxResponseBodyBytes = 2000000
```
2019-04-03 12:32:04 +00:00
### `memResponseBodyBytes`
_Optional, Default=1048576_
2021-02-11 13:34: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.
2019-09-23 12:32:04 +00:00
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2019-09-23 12:32:04 +00:00
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.limit.buffering.memResponseBodyBytes=2000000"
2019-09-23 12:32:04 +00:00
```
```yaml tab="Kubernetes"
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-09-23 12:32:04 +00:00
kind: Middleware
metadata:
name: limit
spec:
buffering:
memResponseBodyBytes: 2000000
```
2019-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
- "traefik.http.middlewares.limit.buffering.memResponseBodyBytes=2000000"
```
2019-09-23 12:32:04 +00:00
```yaml tab="File (YAML)"
http:
middlewares:
limit:
buffering:
memResponseBodyBytes: 2000000
```
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.limit.buffering]
memResponseBodyBytes = 2000000
```
2019-04-03 12:32:04 +00:00
### `retryExpression`
_Optional, Default=""_
2021-02-11 13:34:04 +00:00
You can have the Buffering middleware replay the request using `retryExpression`.
2021-02-11 13:34:04 +00:00
??? example "Retries once in the case of a network error"
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2019-09-23 12:32:04 +00:00
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.limit.buffering.retryExpression=IsNetworkError() && Attempts() < 2"
2019-09-23 12:32:04 +00:00
```
2019-09-23 12:32:04 +00:00
```yaml tab="Kubernetes"
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-09-23 12:32:04 +00:00
kind: Middleware
metadata:
name: limit
spec:
buffering:
retryExpression: "IsNetworkError() && Attempts() < 2"
```
2019-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
- "traefik.http.middlewares.limit.buffering.retryExpression=IsNetworkError() && Attempts() < 2"
```
2019-09-23 12:32:04 +00:00
```yaml tab="File (YAML)"
http:
middlewares:
limit:
buffering:
retryExpression: "IsNetworkError() && Attempts() < 2"
```
```toml tab="File (TOML)"
[http.middlewares]
[http.middlewares.limit.buffering]
retryExpression = "IsNetworkError() && Attempts() < 2"
```
2019-09-23 12:32:04 +00:00
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:
- `Attempts()` number of attempts (the first one counts)
- `ResponseCode()` response code of the service
2021-02-11 13:34:04 +00:00
- `IsNetworkError()` whether the response code is related to networking error