文档
一个 项目

encode

使用配置的编码对响应进行编码。编码的典型用途是压缩。

Syntax

encode [<matcher>] [<formats...>] {
	# encoding formats
	gzip [<level>]
	zstd [<level>]
	
	minimum_length <length>

	match <inline_response_matcher>
}
  • <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
}