文件
一個 項目

編碼

使用已設定的編碼編碼回應。編碼的典型用途是壓縮。

語法

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

	# response matcher single line syntax
	match [header <field> [<value>]] | [status <code...>]
	# or response matcher block for multiple conditions
	match {
		status <code...>
		header <field> [<value>]
	}
}
  • <formats...> 是要啟用的編碼格式清單。如果啟用多種編碼,編碼會根據要求的 Accept-Encoding 標頭選擇;如果客戶端沒有強烈偏好 (q 因子),則會使用第一個支援的編碼。

  • gzip 啟用 Gzip 壓縮,也可以選擇在指定層級啟用。

  • zstd 啟用 Zstandard 壓縮。

  • minimum_length 回應應具有的最小位元組數,才能進行編碼 (預設值:512)。

  • match 是一個 回應比對器。只有符合的回應會進行編碼。預設值如下所示:

    match {
        header Content-Type text/*
        header Content-Type application/json*
        header Content-Type application/javascript*
        header Content-Type application/xhtml+xml*
        header Content-Type application/atom+xml*
        header Content-Type application/rss+xml*
        header Content-Type image/svg+xml*
    }
    

回應比對器

回應比對器 可用於根據特定條件篩選 (或分類) 回應。

狀態

status <code...>

依據 HTTP 狀態碼。

  • <code...> 是 HTTP 狀態碼清單。特殊情況為 2xx3xx 等,分別比對 200-299、300-399 等範圍內的全部狀態碼

請參閱 標頭 要求比對器以取得支援的語法。

範例

啟用 Gzip 壓縮

encode gzip

啟用 Zstandard 和 Gzip 壓縮(由於 Zstandard 優先,因此隱含優先使用 Zstandard)

encode zstd gzip

在完整網站中,壓縮由 file_server 提供的靜態檔案

example.com {
	root * /srv
	encode zstd gzip
	file_server
}