文档
一个 项目

encode

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

语法

encode [<matcher>] [<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 默认模式(level 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
}