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

    1. # Sets the maximum request body to 2MB
    2. apiVersion: traefik.containo.us/v1alpha1
    3. kind: Middleware
    4. metadata:
    5. name: limit
    6. spec:
    7. buffering:
    8. maxRequestBodyBytes: 2000000

    Consul Catalog

    1. # Sets the maximum request body to 2MB
    2. - "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"

    Marathon

    1. "labels": {
    2. "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes": "2000000"
    3. }

    Rancher

    1. # Sets the maximum request body to 2MB
    2. labels:
    3. - "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"

    File (YAML)

    1. # Sets the maximum request body to 2MB
    2. http:
    3. middlewares:
    4. limit:
    5. buffering:
    6. maxRequestBodyBytes: 2000000

    File (TOML)

    1. # Sets the maximum request body to 2MB
    2. [http.middlewares]
    3. [http.middlewares.limit.buffering]
    4. maxRequestBodyBytes = 2000000

    Configuration Options

    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

    1. labels:
    2. - "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"

    Kubernetes

    1. apiVersion: traefik.containo.us/v1alpha1
    2. kind: Middleware
    3. metadata:
    4. name: limit
    5. spec:
    6. buffering:
    7. maxRequestBodyBytes: 2000000

    Consul Catalog

    1. - "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"

    Marathon

    1. "labels": {
    2. "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes": "2000000"
    3. }
    1. labels:
    2. - "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"

    File (YAML)

    1. middlewares:
    2. limit:
    3. maxRequestBodyBytes: 2000000

    File (TOML)

    1. [http.middlewares]
    2. [http.middlewares.limit.buffering]
    3. maxRequestBodyBytes = 2000000

    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

    1. apiVersion: traefik.containo.us/v1alpha1
    2. kind: Middleware
    3. metadata:
    4. name: limit
    5. spec:
    6. buffering:
    7. memRequestBodyBytes: 2000000

    Consul Catalog

    1. - "traefik.http.middlewares.limit.buffering.memRequestBodyBytes=2000000"

    Marathon

    1. "labels": {
    2. "traefik.http.middlewares.limit.buffering.memRequestBodyBytes": "2000000"
    3. }

    Rancher

    1. labels:
    2. - "traefik.http.middlewares.limit.buffering.memRequestBodyBytes=2000000"

    File (YAML)

    1. http:
    2. middlewares:
    3. limit:
    4. buffering:
    5. memRequestBodyBytes: 2000000

    File (TOML)

    1. [http.middlewares]
    2. [http.middlewares.limit.buffering]
    3. memRequestBodyBytes = 2000000

    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 413 (Request Entity Too Large) response instead.

    Docker

    1. labels:
    2. - "traefik.http.middlewares.limit.buffering.maxResponseBodyBytes=2000000"

    Kubernetes

    1. apiVersion: traefik.containo.us/v1alpha1
    2. kind: Middleware
    3. metadata:
    4. name: limit
    5. spec:
    6. buffering:
    7. maxResponseBodyBytes: 2000000

    Consul Catalog

    1. - "traefik.http.middlewares.limit.buffering.maxResponseBodyBytes=2000000"

    Marathon

    1. "labels": {
    2. "traefik.http.middlewares.limit.buffering.maxResponseBodyBytes": "2000000"
    3. }

    Rancher

    1. labels:
    2. - "traefik.http.middlewares.limit.buffering.maxResponseBodyBytes=2000000"
    1. http:
    2. middlewares:
    3. limit:
    4. buffering:
    5. maxResponseBodyBytes: 2000000

    File (TOML)

    1. [http.middlewares.limit.buffering]

    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

    1. apiVersion: traefik.containo.us/v1alpha1
    2. kind: Middleware
    3. metadata:
    4. name: limit
    5. spec:
    6. buffering:
    7. memResponseBodyBytes: 2000000

    Consul Catalog

    1. - "traefik.http.middlewares.limit.buffering.memResponseBodyBytes=2000000"

    Marathon

    1. "labels": {
    2. "traefik.http.middlewares.limit.buffering.memResponseBodyBytes": "2000000"
    3. }

    Rancher

    1. labels:
    2. - "traefik.http.middlewares.limit.buffering.memResponseBodyBytes=2000000"

    File (YAML)

    1. http:
    2. middlewares:
    3. limit:
    4. buffering:
    5. memResponseBodyBytes: 2000000

    File (TOML)

    1. [http.middlewares]
    2. [http.middlewares.limit.buffering]
    3. memResponseBodyBytes = 2000000

    You can have the Buffering middleware replay the request using retryExpression.

    Retries once in the case of a network error

    Docker

    1. labels:
    2. - "traefik.http.middlewares.limit.buffering.retryExpression=IsNetworkError() && Attempts() < 2"

    Kubernetes

    1. apiVersion: traefik.containo.us/v1alpha1
    2. kind: Middleware
    3. metadata:
    4. name: limit
    5. spec:
    6. buffering:
    7. retryExpression: "IsNetworkError() && Attempts() < 2"

    Consul Catalog

    1. - "traefik.http.middlewares.limit.buffering.retryExpression=IsNetworkError() && Attempts() < 2"

    Marathon

    1. "labels": {
    2. "traefik.http.middlewares.limit.buffering.retryExpression": "IsNetworkError() && Attempts() < 2"
    3. }

    Rancher

    1. labels:
    2. - "traefik.http.middlewares.limit.buffering.retryExpression=IsNetworkError() && Attempts() < 2"

    File (YAML)

    1. http:
    2. middlewares:
    3. limit:
    4. buffering:
    5. retryExpression: "IsNetworkError() && Attempts() < 2"
    1. [http.middlewares]
    2. [http.middlewares.limit.buffering]
    3. 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:

    • Attempts() number of attempts (the first one counts)
    • ResponseCode() response code of the service