Skip to main content
Version: 2.13

gzip

描述#

gzip 插件能动态设置 Nginx 的压缩行为。

该插件要求 APISIX 运行在 APISIX-OpenResty 上。

属性#

名称类型必选项默认值有效值描述
typesarray[string] or "*"可选["text/html"]动态设置 gzip_types 指令,特殊值 "*" 匹配任何 MIME 类型
min_lengthinteger可选20>= 1动态设置 gzip_min_length 指令
comp_levelinteger可选1[1, 9]动态设置 gzip_comp_level 指令
http_versionnumber可选1.11.1, 1.0动态设置 gzip_http_version 指令
buffers.numberinteger可选32>= 1动态设置 gzip_buffers 指令
buffers.sizeinteger可选4096>= 1动态设置 gzip_buffers 指令
varyboolean可选false动态设置 gzip_vary 指令

如何启用#

下面是一个示例,在指定的 route 上开启了 gzip 插件:

curl -i http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/index.html",
"plugins": {
"gzip": {
"buffers": {
"number": 8
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
}
}'

测试插件#

使用 curl 访问:

curl http://127.0.0.1:9080/index.html -i -H "Accept-Encoding: gzip"
HTTP/1.1 404 Not Found
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Date: Wed, 21 Jul 2021 03:52:55 GMT
Server: APISIX/2.7
Content-Encoding: gzip

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.

禁用插件#

想要禁用该插件时很简单,在路由 plugins 配置块中删除对应 JSON 配置,不需要重启服务,即可立即生效禁用该插件。

curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/index.html",
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
}
}'