Skip to main content

Compress Middleware

Compress Responses Before Sending to Clients The Compress middleware supports Gzip, Brotli, and Zstandard compression. The activation of compression and the compression method choice rely on the request’s Accept-Encoding header.

Configuration Examples

Enable Compression

labels:
  - "traefik.http.middlewares.test-compress.compress=true"

Compression with Excluded Content Types

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

Compression with Specific Encodings

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

When Compression Occurs

Responses are compressed when all these criteria are met:
  1. The Accept-Encoding request header contains gzip, br, zstd, and/or * (with or without quality values)
  2. The response is not already compressed (no Content-Encoding header)
  3. The response Content-Type is not excluded (or is included if using includedContentTypes)
  4. The response body is larger than the minimum configured bytes (default: 1024)
If the Accept-Encoding header is absent and no defaultEncoding is configured, the response won’t be compressed. If present but empty, compression is disabled.

Configuration Options

excludedContentTypes
array
List of content types to exclude from compression. Compared case-insensitively with whitespace ignored.
The excludedContentTypes and includedContentTypes options are mutually exclusive.
gRPC (application/grpc) is never compressed.
includedContentTypes
array
List of content types to include for compression. Only responses with these content types will be compressed.
The excludedContentTypes and includedContentTypes options are mutually exclusive.
minResponseBodyBytes
integer
default:"1024"
Minimum response body size in bytes required for compression.
When data is sent to the client on flush (e.g., Transfer-Encoding: chunked), this configuration is ignored and data is compressed immediately.
defaultEncoding
string
Default encoding if the Accept-Encoding header is not present or contains a wildcard (*).No fallback occurs when the header value is empty or unsupported.
encodings
array
default:"gzip, br, zstd"
List of supported compression encodings. Valid entries are gzip, br (Brotli), and zstd (Zstandard).The order sets priority - the top entry has the highest priority.

Usage Examples

Compress Specific Content Types

http:
  middlewares:
    json-compress:
      compress:
        includedContentTypes:
          - application/json
          - text/html
          - text/plain
        minResponseBodyBytes: 512

Prioritize Zstandard Compression

http:
  middlewares:
    modern-compress:
      compress:
        encodings:
          - zstd
          - br
          - gzip
        defaultEncoding: gzip

Combining Compression with Other Middlewares

http:
  routers:
    api-router:
      rule: "Host(`api.example.com`)"
      service: api-service
      middlewares:
        - api-compress
        - api-headers
        - api-ratelimit

  middlewares:
    api-compress:
      compress:
        includedContentTypes:
          - application/json
        minResponseBodyBytes: 256
    
    api-headers:
      headers:
        customResponseHeaders:
          X-Custom-Header: "value"
    
    api-ratelimit:
      rateLimit:
        average: 100
        burst: 50

Build docs developers (and LLMs) love