跳到主要内容

Gzip 压缩

gzip 插件在网关向客户端发送响应前,压缩符合条件的 HTTP 响应体。它可以减少选定路由或服务中文本、JSON 等可压缩内容占用的带宽,无需修改上游应用。

响应压缩条件​

满足以下条件时,插件会配置响应压缩:

条件行为
客户端支持客户端在 Accept-Encoding 中以非零质量值声明支持 gzip。
内容类型响应包含 Content-Type,且其 MIME 类型与 types 匹配。
响应长度如响应包含数值形式的 Content-Length,其值必须大于或等于 min_length。如未包含 Content-Length,插件不进行最小长度检查。
HTTP 版本客户端 HTTP 版本大于或等于 http_version。
响应状态响应状态码为 200、403 或 404。
已有编码响应尚未包含非空的 Content-Encoding 响应头。
响应体响应是带有响应体的主响应。

网关的静态 gzip_proxied 和 gzip_disable 策略仍可阻止压缩。压缩响应会移除 Content-Length 和 Accept-Ranges,并将强 ETag 转换为弱验证器。

示例​

第一个示例对比 identity 编码和 gzip 编码的响应,并验证解压后的响应体保持不变。第二个示例提高最小长度,使同一响应不再被压缩。

压缩文本响应​

选择本地纯文本上游的运行环境。

为示例创建工作目录并进入该目录。

mkdir gzip-example
cd gzip-example

创建以下 Python 脚本,作为测试上游。

gzip-upstream.py
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer


class Handler(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"

def do_GET(self):
if self.path != "/large-response":
self.send_error(404)
return

body = b"compressible response\n" * 256

self.send_response(200)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.send_header("Content-Length", str(len(body)))
self.send_header("Vary", "Origin")
self.end_headers()
self.wfile.write(body)

def log_message(self, *_):
pass


ThreadingHTTPServer(("0.0.0.0", 8000), Handler).serve_forever()

将 GATEWAY_CONTAINER 设为正在运行的 APISIX 或 API7 网关容器名。创建一个专用网络,并将网关接入该网络。

export GATEWAY_CONTAINER=replace-with-gateway-container-name

docker network create gateway-gzip-net
docker network connect gateway-gzip-net "$GATEWAY_CONTAINER"

启动测试上游:

docker run -d \
--name gzip-upstream \
--network gateway-gzip-net \
-v "$PWD/gzip-upstream.py:/gzip-upstream.py:ro" \
python:3.13.15-alpine3.24 \
python -u /gzip-upstream.py

配置插件,使其压缩足够大的纯文本响应,并添加响应差异标识头。

如果启用了 Admin API 身份验证,请在使用 Admin API 标签页前将具有管理员角色的密钥导出为 ADMIN_API_KEY。

创建路由。

curl "http://127.0.0.1:9180/apisix/admin/routes/gzip-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"uri": "/large-response",
"plugins": {
"gzip": {
"types": ["text/plain"],
"min_length": 100,
"comp_level": 6,
"vary": true
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"gzip-upstream:8000": 1
}
}
}'

分别保存 identity 编码和 gzip 编码的响应:

curl "http://127.0.0.1:9080/large-response" \
-H "Accept-Encoding: identity" \
--silent \
--show-error \
--fail \
--dump-header identity-headers.txt \
--output identity.txt

curl "http://127.0.0.1:9080/large-response" \
-H "Accept-Encoding: gzip" \
--silent \
--show-error \
--fail \
--dump-header gzip-headers.txt \
--output response.gz

确认第二个响应使用 gzip 编码,且响应会因请求编码而异:

grep -i "^Content-Encoding:" gzip-headers.txt
grep -i "^Vary:" gzip-headers.txt

gzip 编码的响应包含以下两个响应头:

Content-Encoding: gzip
Vary: Origin
Vary: Accept-Encoding

网关保留上游的响应差异标识,并为缓存添加按请求编码区分的标识。

解压响应,并与 identity 编码的响应体进行比较:

if ! gzip -dc response.gz > decompressed.txt; then
echo "Decompression failed"
exit 1
fi

if cmp identity.txt decompressed.txt; then
echo "Bodies match"
else
echo "Bodies differ"
exit 1
fi

if [ "$(wc -c < response.gz)" -lt "$(wc -c < identity.txt)" ]; then
echo "Compressed response is smaller"
else
echo "Compressed response is not smaller"
exit 1
fi

检查返回:

Bodies match
Compressed response is smaller

结果表明,对于本示例中易于压缩的测试内容,压缩保留了响应体内容,并减小了响应大小。

跳过小于最小长度的响应​

更新上一个示例中的插件,使测试响应小于配置的最小长度:

curl "http://127.0.0.1:9180/apisix/admin/routes/gzip-route" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"plugins": {
"gzip": {
"types": ["text/plain"],
"min_length": 10000,
"comp_level": 6,
"vary": true
}
}
}'

再次请求 gzip 编码,并检查响应是否包含 Content-Encoding:

if ! curl --silent --show-error --fail \
"http://127.0.0.1:9080/large-response" \
-H "Accept-Encoding: gzip" \
--dump-header min-length-headers.txt \
--output /dev/null; then
echo "Request failed" >&2
exit 1
fi

if grep -qi "^Content-Encoding: gzip" min-length-headers.txt; then
echo "Unexpected compressed response"
exit 1
else
echo "Response is not compressed"
fi

检查返回:

Response is not compressed

响应声明的长度小于更新后的阈值,因此未被压缩。