主题
gzip 
本文介绍了关于 APIX gzip 插件的基本信息及使用方法。
描述 
gzip 插件能动态设置 NGINX 的压缩行为。
IMPORTANT
该插件要求 APIX 运行在 APISIX-Base 上。
属性 
| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 | 
|---|---|---|---|---|---|
| types | array[string] or "*" | 否 | ["text/html"] | 动态设置 gzip_types指令,特殊值"*"匹配任何 MIME 类型。 | |
| min_length | integer | 否 | 20 | >= 1 | 动态设置 gzip_min_length指令。 | 
| comp_level | integer | 否 | 1 | [1, 9] | 动态设置 gzip_comp_level指令。 | 
| http_version | number | 否 | 1.1 | 1.1, 1.0 | 动态设置 gzip_http_version指令。 | 
| buffers.number | integer | 否 | 32 | >= 1 | 动态设置 gzip_buffers指令。 | 
| buffers.size | integer | 否 | 4096 | >= 1 | 动态设置 gzip_buffers指令。 | 
| vary | boolean | 否 | false | 动态设置 gzip_vary指令。 | 
启用插件 
以下示例展示了如何在指定路由中启用 gzip 插件:
shell
curl -i http://127.0.0.1:9180/apix/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
        }
    }
}'测试插件 
通过上述命令启用插件后,可以使用如下命令测试插件是否启用成功:
shell
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.禁用插件 
当你需要禁用 gzip 插件时,可以通过以下命令删除相应的 JSON 配置,APISIX 将会自动重新加载相关配置,无需重启服务:
shell
curl http://127.0.0.1:9180/apix/admin/routes/1  \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "uri": "/index.html",
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "127.0.0.1:1980": 1
        }
    }
}'