Compress
The Compress middleware supports Gzip and Brotli compression.
The activation of compression, and the compression method choice rely (among other things) on the request's Accept-Encoding
header.
Responses are compressed when the following criteria are all met:
- The
Accept-Encoding
request header containsgzip
,*
, and/orbr
with or without quality values. If theAccept-Encoding
request header is absent, 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, that is the
Content-Encoding
response header is not already set. - The response
Content-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
Field | Description | Default | Required |
---|---|---|---|
excludedContentTypes | 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. | " " | No |
info
The excludedContentTypes
and includedContentTypes
options are mutually exclusive.
- Gzip
- gRPC
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.
Note that application/grpc
is never compressed.
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: test-compress
spec:
compress:
excludedContentTypes:
- text/event-stream
includedContentTypes
Field | Description | Default | Required |
---|---|---|---|
includedContentTypes | 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. | " " | No |
info
The excludedContentTypes
and includedContentTypes
options are mutually exclusive.
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: test-compress
spec:
compress:
includedContentTypes:
- application/json
- text/html
- text/plain
minResponseBodyBytes
Field | Description | Default | Required |
---|---|---|---|
minResponseBodyBytes | 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. | 1024 | No |
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: test-compress
spec:
compress:
minResponseBodyBytes: 1200