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 插件:

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

测试插件

使用 curl 访问:

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

禁用插件

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

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