encode
使用指定的编码对响应进行编码。其典型用途就是压缩。
语法
encode [<matcher>] [<formats...>] { # 编码格式 gzip [<level>] zstd [<level>] minimum_length <length> match <inline_response_matcher> }
-
<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 }