gzip

描述

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

gzip - 图1IMPORTANT

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

属性

名称类型必选项默认值有效值描述
typesarray[string] or “[“text/html”]动态设置 gzip_types 指令,特殊值 ““ 匹配任何 MIME 类型。
min_lengthinteger20>= 1动态设置 gzip_min_length 指令。
comp_levelinteger1[1, 9]动态设置 gzip_comp_level 指令。
http_versionnumber1.11.1, 1.0动态设置 gzip_http_version 指令。
buffers.numberinteger32>= 1动态设置 gzip_buffers 指令。
buffers.sizeinteger4096>= 1动态设置 gzip_buffers 指令。
varybooleanfalse动态设置 gzip_vary 指令。

启用插件

以下示例展示了如何在指定路由中启用 gzip 插件:

  1. curl -i http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "uri": "/index.html",
  5. "plugins": {
  6. "gzip": {
  7. "buffers": {
  8. "number": 8
  9. }
  10. }
  11. },
  12. "upstream": {
  13. "type": "roundrobin",
  14. "nodes": {
  15. "127.0.0.1:1980": 1
  16. }
  17. }
  18. }'

测试插件

通过上述命令启用插件后,可以使用如下命令测试插件是否启用成功:

  1. curl http://127.0.0.1:9080/index.html -i -H "Accept-Encoding: gzip"
  1. HTTP/1.1 404 Not Found
  2. Content-Type: text/html; charset=utf-8
  3. Transfer-Encoding: chunked
  4. Connection: keep-alive
  5. Date: Wed, 21 Jul 2021 03:52:55 GMT
  6. Server: APISIX/2.7
  7. Content-Encoding: gzip
  8. Warning: Binary output can mess up your terminal. Use "--output -" to tell
  9. Warning: curl to output it to your terminal anyway, or consider "--output
  10. Warning: <FILE>" to save to a file.

禁用插件

当你需要禁用 gzip 插件时,可以通过以下命令删除相应的 JSON 配置,APISIX 将会自动重新加载相关配置,无需重启服务:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "uri": "/index.html",
  5. "upstream": {
  6. "type": "roundrobin",
  7. "nodes": {
  8. "127.0.0.1:1980": 1
  9. }
  10. }
  11. }'