кодирование
Кодирует ответы, используя настроенные кодировки. Типичное применение кодирования — сжатие.
Синтаксис
encode [<matcher>] [<formats...>] { # encoding formats
gzip [<level>]
zstd [<level>]
minimum_length <length>
match {
status <code...>
header <field> [<value>]
}
}
-
<formats...> — список кодировок для включения. Если включено несколько кодировок, кодировка выбирается на основе заголовка Accept-Encoding запроса; если у клиента нет явного предпочтения (q-фактор), используется первая поддерживаемая кодировка. Если опущено, по умолчанию включены
zstd(предпочтительно) иgzip. -
gzip — включает сжатие Gzip, необязательно на заданном уровне.
-
zstd — включает сжатие Zstandard, необязательно на заданном уровне (возможные значения = default, fastest, better, best). Уровень сжатия по умолчанию примерно эквивалентен значению по умолчанию режима Zstandard (уровень 3).
-
minimum_length — минимальное количество байт в ответе для кодирования (по умолчанию: 512).
-
match — матчер ответов. Кодируются только соответствующие ответы. Значение по умолчанию выглядит так:
match { header Content-Type application/atom+xml* header Content-Type application/eot* header Content-Type application/font* header Content-Type application/geo+json* header Content-Type application/graphql+json* header Content-Type application/javascript* header Content-Type application/json* header Content-Type application/ld+json* header Content-Type application/manifest+json* header Content-Type application/opentype* header Content-Type application/otf* header Content-Type application/rss+xml* header Content-Type application/truetype* header Content-Type application/ttf* header Content-Type application/vnd.api+json* header Content-Type application/vnd.ms-fontobject* header Content-Type application/wasm* header Content-Type application/x-httpd-cgi* header Content-Type application/x-javascript* header Content-Type application/x-opentype* header Content-Type application/x-otf* header Content-Type application/x-perl* header Content-Type application/x-protobuf* header Content-Type application/x-ttf* header Content-Type application/xhtml+xml* header Content-Type application/xml* header Content-Type font/* header Content-Type image/svg+xml* header Content-Type image/vnd.microsoft.icon* header Content-Type image/x-icon* header Content-Type multipart/bag* header Content-Type multipart/mixed* header Content-Type text/* }
Примеры
Включить сжатие Gzip:
encode gzip
Включить сжатие Zstandard и Gzip (с предпочтительным Zstandard, так как он указан первым):
encode zstd gzip
Так как это значение по умолчанию, предыдущая конфигурация строго эквивалентна:
encode
И в полном сайте, сжимая статические файлы, обслуживаемые file_server:
example.com {
root * /srv
encode
file_server
}
© 2015-2025 Matthew Holt and The Caddy Authors
Licensed under the Apache License 2.0.
Caddy is a registered trademark of Stack Holdings GmbH.
https://caddyserver.com/docs/caddyfile/directives/encode