encode
使用配置的编码对响应进行编码。编码的典型用途是压缩。
语法
encode [<matcher>] [<formats...>] {
# encoding formats
gzip [<level>]
zstd [<level>]
minimum_length <length>
match {
status <code...>
header <field> [<value>]
}
}
-
<formats...> 是要启用的编码格式列表。如果启用了多种编码,则根据请求的 Accept-Encoding 标头选择编码;如果客户端没有强烈的偏好(q-factor),则使用第一个支持的编码。如果省略,则默认启用
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
}