Compress

Compress Allows Compressing Responses before Sending them to the Client

Compress

The Compress middleware supports Gzip, Brotli and Zstandard compression. The activation of compression, and the compression method choice rely (among other things) on the request’s Accept-Encoding header.

Configuration Examples

Docker & Swarm

  1. # Enable compression
  2. labels:
  3. - "traefik.http.middlewares.test-compress.compress=true"

Kubernetes

  1. # Enable compression
  2. apiVersion: traefik.io/v1alpha1
  3. kind: Middleware
  4. metadata:
  5. name: test-compress
  6. spec:
  7. compress: {}

Consul Catalog

  1. # Enable compression
  2. - "traefik.http.middlewares.test-compress.compress=true"

File (YAML)

  1. # Enable compression
  2. http:
  3. middlewares:
  4. test-compress:
  5. compress: {}

File (TOML)

  1. # Enable compression
  2. [http.middlewares]
  3. [http.middlewares.test-compress.compress]

Info

Responses are compressed when the following criteria are all met:

  • The Accept-Encoding request header contains gzip, and/or *, and/or br, and/or zstd with or without quality values. If the Accept-Encoding request header is absent and no defaultEncoding is configured, the response won’t be encoded. If it is present, but its value is the empty string, then compression is disabled.
  • The response is not already compressed, i.e. the Content-Encoding response header is not already set.
  • The responseContent-Type header is not one among the excludedContentTypes options, or is one among the includedContentTypes options.
  • The response body is larger than the configured minimum amount of bytes (default is 1024).

Configuration Options

excludedContentTypes

Optional, Default=””

excludedContentTypes specifies a list of content types to compare the Content-Type header of the incoming requests and responses before compressing.

The responses with content types defined in excludedContentTypes are not compressed.

Content types are compared in a case-insensitive, whitespace-ignored manner.

Info

The excludedContentTypes and includedContentTypes options are mutually exclusive.

In the case of gzip

If the Content-Type header is not defined, or empty, the compress middleware will automatically detect a content type. It will also set the Content-Type header according to the detected MIME type.

gRPC

Note that application/grpc is never compressed.

Docker & Swarm

  1. labels:
  2. - "traefik.http.middlewares.test-compress.compress.excludedcontenttypes=text/event-stream"

Kubernetes

  1. apiVersion: traefik.io/v1alpha1
  2. kind: Middleware
  3. metadata:
  4. name: test-compress
  5. spec:
  6. compress:
  7. excludedContentTypes:
  8. - text/event-stream

Consul Catalog

  1. - "traefik.http.middlewares.test-compress.compress.excludedcontenttypes=text/event-stream"

File (YAML)

  1. http:
  2. middlewares:
  3. test-compress:
  4. compress:
  5. excludedContentTypes:
  6. - text/event-stream

File (TOML)

  1. [http.middlewares]
  2. [http.middlewares.test-compress.compress]
  3. excludedContentTypes = ["text/event-stream"]

includedContentTypes

Optional, Default=””

includedContentTypes specifies a list of content types to compare the Content-Type header of the responses before compressing.

The responses with content types defined in includedContentTypes are compressed.

Content types are compared in a case-insensitive, whitespace-ignored manner.

Info

The excludedContentTypes and includedContentTypes options are mutually exclusive.

Docker & Swarm

  1. labels:
  2. - "traefik.http.middlewares.test-compress.compress.includedcontenttypes=application/json,text/html,text/plain"

Kubernetes

  1. apiVersion: traefik.io/v1alpha1
  2. kind: Middleware
  3. metadata:
  4. name: test-compress
  5. spec:
  6. compress:
  7. includedContentTypes:
  8. - application/json
  9. - text/html
  10. - text/plain

Consul Catalog

  1. - "traefik.http.middlewares.test-compress.compress.includedcontenttypes=application/json,text/html,text/plain"

File (YAML)

  1. http:
  2. middlewares:
  3. test-compress:
  4. compress:
  5. includedContentTypes:
  6. - application/json
  7. - text/html
  8. - text/plain

File (TOML)

  1. [http.middlewares]
  2. [http.middlewares.test-compress.compress]
  3. includedContentTypes = ["application/json","text/html","text/plain"]

minResponseBodyBytes

Optional, Default=1024

minResponseBodyBytes specifies the minimum amount of bytes a response body must have to be compressed.

Responses smaller than the specified values will not be compressed.

Docker & Swarm

  1. labels:
  2. - "traefik.http.middlewares.test-compress.compress.minresponsebodybytes=1200"

Kubernetes

  1. apiVersion: traefik.io/v1alpha1
  2. kind: Middleware
  3. metadata:
  4. name: test-compress
  5. spec:
  6. compress:
  7. minResponseBodyBytes: 1200

Consul Catalog

  1. - "traefik.http.middlewares.test-compress.compress.minresponsebodybytes=1200"

File (YAML)

  1. http:
  2. middlewares:
  3. test-compress:
  4. compress:
  5. minResponseBodyBytes: 1200

File (TOML)

  1. [http.middlewares]
  2. [http.middlewares.test-compress.compress]
  3. minResponseBodyBytes = 1200

defaultEncoding

Optional, Default=””

defaultEncoding specifies the default encoding if the Accept-Encoding header is not in the request or contains a wildcard (*).

There is no fallback on the defaultEncoding when the header value is empty or unsupported.

Docker & Swarm

  1. labels:
  2. - "traefik.http.middlewares.test-compress.compress.defaultEncoding=gzip"

Kubernetes

  1. apiVersion: traefik.io/v1alpha1
  2. kind: Middleware
  3. metadata:
  4. name: test-compress
  5. spec:
  6. compress:
  7. defaultEncoding: gzip

Consul Catalog

  1. - "traefik.http.middlewares.test-compress.compress.defaultEncoding=gzip"

File (YAML)

  1. http:
  2. middlewares:
  3. test-compress:
  4. compress:
  5. defaultEncoding: gzip

File (TOML)

  1. [http.middlewares]
  2. [http.middlewares.test-compress.compress]
  3. defaultEncoding = "gzip"

encodings

Optional, Default=”zstd, br, gzip”

encodings specifies the list of supported compression encodings. At least one encoding value must be specified, and valid entries are zstd (Zstandard), br (Brotli), and gzip (Gzip). The order of the list also sets the priority, the top entry has the highest priority.

Docker & Swarm

  1. labels:
  2. - "traefik.http.middlewares.test-compress.compress.encodings=zstd,br"

Kubernetes

  1. apiVersion: traefik.io/v1alpha1
  2. kind: Middleware
  3. metadata:
  4. name: test-compress
  5. spec:
  6. compress:
  7. encodings:
  8. - zstd
  9. - br

Consul Catalog

  1. - "traefik.http.middlewares.test-compress.compress.encodings=zstd,br"

File (YAML)

  1. http:
  2. middlewares:
  3. test-compress:
  4. compress:
  5. encodings:
  6. - zstd
  7. - br

File (TOML)

  1. [http.middlewares]
  2. [http.middlewares.test-compress.compress]
  3. encodings = ["zstd","br"]