2020-01-24 12:47:52 -07:00
---
title: encode (Caddyfile directive)
---
2021-08-31 14:50:01 -04:00
< script >
$(function() {
// We'll add links to all the subdirectives if a matching anchor tag is found on the page.
addLinksToSubdirectives();
});
< / script >
2020-01-24 12:47:52 -07:00
# encode
Encodes responses using the configured encoding(s). A typical use for encoding is compression.
## Syntax
2020-05-17 16:32:12 -04:00
```caddy-d
2020-01-24 12:47:52 -07:00
encode [< matcher > ] < formats... > {
2021-05-10 13:43:20 -06:00
# encoding formats
2020-01-24 12:47:52 -07:00
gzip [< level > ]
zstd
2021-05-10 13:43:20 -06:00
2021-04-05 23:24:54 +02:00
minimum_length < length >
# response matcher single line syntax
match [header < field > [< value > ]] | [status < code... > ]
2021-05-10 13:43:20 -06:00
# or response matcher block for multiple conditions
2021-04-05 23:24:54 +02:00
match {
status < code... >
header < field > [< value > ]
}
2020-01-24 12:47:52 -07:00
}
```
2021-05-10 13:43:20 -06:00
- **< formats...> ** is the list of encoding formats to enable. If multiple encodings are enabled, the encoding is chosen based the request's Accept-Encoding header; if the client has no strong preference (q-factor), then the first supported encoding is used.
2023-08-20 04:16:11 -04:00
2021-08-31 14:50:01 -04:00
- **gzip** < span id = "gzip" /> enables Gzip compression, optionally at the specified level.
2023-08-20 04:16:11 -04:00
2021-08-31 14:50:01 -04:00
- **zstd** < span id = "zstd" /> enables Zstandard compression.
2023-08-20 04:16:11 -04:00
2021-08-31 14:50:01 -04:00
- **minimum_length** < span id = "minimum_length" /> the minimum number of bytes a response should have to be encoded (default: 512).
2023-08-20 04:16:11 -04:00
2021-08-31 14:50:01 -04:00
- **match** < span id = "match" /> is a [response matcher ](#response-matcher ). Only matching responses are encoded. The default looks like this:
2021-04-05 23:24:54 +02:00
```caddy-d
match {
header Content-Type text/*
header Content-Type application/json*
header Content-Type application/javascript*
header Content-Type application/xhtml+xml*
header Content-Type application/atom+xml*
header Content-Type application/rss+xml*
header Content-Type image/svg+xml*
}
```
## Response matcher
**Response matchers** can be used to filter (or classify) responses by specific criteria.
### status
```caddy-d
status < code... >
```
2020-01-24 12:47:52 -07:00
2021-04-05 23:24:54 +02:00
By HTTP status code.
- **< code...> ** is a list of HTTP status codes. Special cases are `2xx` , `3xx` , ... which match against all status codes in the range of 200-299, 300-399, ... respectively
### header
2021-05-12 15:21:32 -04:00
See the [header ](/docs/caddyfile/matchers#header ) request matcher for the supported syntax.
2020-01-24 12:47:52 -07:00
## Examples
Enable Gzip compression:
2020-05-17 16:32:12 -04:00
```caddy-d
2020-01-24 12:47:52 -07:00
encode gzip
```
2021-05-10 13:43:20 -06:00
Enable Zstandard and Gzip compression (with Zstandard implicitly preferred, since it is first):
2020-01-24 12:47:52 -07:00
2020-05-17 16:32:12 -04:00
```caddy-d
2020-01-24 12:47:52 -07:00
encode zstd gzip
```