Zstandard(zstd)壓縮

本文將詳細介紹如何在 OpenResty Edge 中配置和使用 Zstandard(zstd)壓縮功能。

配置層級概述

OpenResty Edge 提供了多個層級的 zstd 壓縮配置選項,按照優先順序從高到低排列如下:

  1. 頁面規則配置:應用 > HTTP 應用 > 具體應用 > 頁面規則 > 動作 > 啟用閘道器 Zstandard/設定 Zstandard 型別
  2. 全域性自定義動作配置(被 HTTP 應用引用):全域性配置 > 全域性自定義動作 > 動作 > 啟用閘道器 Zstandard/設定 Zstandard 型別
  3. 應用設定配置:應用 > HTTP 應用 > 具體應用 > 設定 > Zstandard
  4. 全域性改寫規則配置:全域性配置 > 全域性改寫規則 > 動作 > 啟用閘道器 Zstandard/設定 Zstandard 型別
  5. 全域性通用配置:全域性配置 > 通用配置 > Zstandard 配置(預設啟用)

注意:當多個層級同時配置時,高優先順序的配置會覆蓋低優先順序的配置。

全域性通用配置

全域性通用配置是 zstd 壓縮的基礎配置,為整個系統提供預設的壓縮引數。

配置引數說明

配置項預設值說明
Zstandard 開關開啟全域性啟用或禁用 Zstandard 壓縮功能
Zstandard 緩衝32 x 4KB壓縮過程中使用的緩衝區大小,影響記憶體使用
Zstandard 壓縮等級6壓縮級別(1-22),數值越高壓縮率越好但速度越慢
Zstandard 最小壓縮長度1000 位元組只有響應體大小超過此閾值才進行壓縮
分塊傳輸(Transfer-Encoding: chunked)時忽略此限制
Zstandard 壓縮資源型別見下方列表指定需要進行 zstd 壓縮的 MIME 型別

預設壓縮型別

系統預設對以下 MIME 型別啟用 zstd 壓縮:

text/plain text/css text/xml text/javascript text/cache-manifest
text/vtt text/x-component application/javascript application/json
application/ld+json application/xml application/xhtml+xml
application/rss+xml application/atom+xml application/manifest+json
application/x-web-app-manifest+json application/vnd.geo+json
application/vnd.ms-fontobject application/x-font-ttf application/wasm
font/opentype image/svg+xml image/bmp

提示text/html 型別預設包含在壓縮範圍內,無需在此處重複配置。

全域性改寫規則配置

透過全域性改寫規則可以為特定條件的請求配置 zstd 壓縮。

可用動作

  • 啟用閘道器 Zstandard:動態啟用或禁用 Zstandard 壓縮
  • 設定 Zstandard 壓縮型別:自定義需要壓縮的 MIME 型別

注意:此處設定的壓縮型別預設不包含 text/html,如需壓縮 HTML 內容,請手動新增。

全域性自定義動作配置

自定義動作提供了可複用的 zstd 壓縮配置,可在多個應用中引用。

配置方式與全域性改寫規則相同,但具有更高的優先順序。

應用級別配置

在應用設定中可以為特定應用配置 zstd 壓縮策略。

配置選項

  • 跟隨全域性:使用全域性通用配置中的設定
  • 啟用:為當前應用啟用 zstd 壓縮
  • 禁用:為當前應用禁用 zstd 壓縮

頁面規則配置

頁面規則提供最精細的 zstd 壓縮控制,可以針對特定的 URL 路徑或條件進行配置。

配置方式與全域性改寫規則相同,但具有最高的優先順序。

實際應用示例

以下示例展示了 zstd 壓縮的實際效果。

測試場景

示例中配置了一條簡單的頁面規則,直接響應 “Hello World” 文字。由於全域性通用配置預設啟用了 zstd 壓縮,因此無需額外配置。

壓縮效果測試

啟用 zstd 壓縮的請求:

$ curl test.com/enable --resolve test.com:80:127.0.0.1 -H 'Accept-Encoding: zstd' -v
*   Trying 127.0.0.1:80...
* Connected to test.com (127.0.0.1) port 80 (#0)
> GET /enable HTTP/1.1
> Host: test.com
> User-Agent: curl/7.76.1
> Accept: */*
> Accept-Encoding: zstd
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Req-ID: 0000008000046f7f9e280008
< Content-Encoding: zstd
<
Warning: Binary output can mess up your terminal. Use "--output -" to tell
Warning: curl to output it to your terminal anyway, or consider "--output
Warning: <FILE>" to save to a file.

響應頭中包含 Content-Encoding: zstd,確認壓縮已生效。

未啟用 zstd 壓縮的請求:

$ curl test.com/enable --resolve test.com:80:127.0.0.1 -v
*   Trying 127.0.0.1:80...
* Connected to test.com (127.0.0.1) port 80 (#0)
> GET /enable HTTP/1.1
> Host: test.com
> User-Agent: curl/7.76.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Fri, 30 May 2025 08:42:51 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Req-ID: 0000008000046f7fac58000b
<
Hello World

未包含 Accept-Encoding: zstd 請求頭時,伺服器返回未壓縮的原始內容。

更多說明

當同時啟用 Gzip、Brotli、zstd 壓縮時,優先順序是 zstd > Brotli > Gzip。