<!-- review: finished -->

<a id="http-gzip"></a>

# GZip

该模块是一个使用 gzip 方法压缩响应的过滤器，可以将传输数据的大小减少 2 倍或更多。

#### WARNING
使用 SSL/TLS 协议时，压缩响应可能会受到 [BREACH](https://en.wikipedia.org/wiki/BREACH) 攻击。

<a id="configuration-example-20"></a>

## 配置示例

```nginx
gzip            on;
gzip_min_length 1000;
gzip_proxied    expired no-cache no-store private auth;
gzip_types      text/plain application/xml;
```

可以使用 [$gzip_ratio](#v-gzip-ratio) 变量来记录达到的压缩比率。

<a id="directives-21"></a>

## 指令

<a id="index-0"></a>

<a id="id3"></a>

### gzip

| [Syntax](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `gzip` `on` | `off`;                   |
|------------------------------------------------------------------------------------------|----------------------------------------|
| 默认                                                                                       | `gzip off;`                            |
| [Context](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server, location, if in location |

启用或禁用响应的 gzip 压缩。

<a id="index-1"></a>

<a id="gzip-buffers"></a>

### gzip_buffers

| [Syntax](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `gzip_buffers` number size;   |
|------------------------------------------------------------------------------------------|-------------------------------|
| 默认                                                                                       | `gzip_buffers 32 4k | 16 8k;` |
| [Context](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server, location        |

设置用于压缩响应的缓冲区数量和大小。默认情况下，缓冲区大小等于一个内存页大小，这取决于平台，通常是 4K 或 8K。

<a id="index-2"></a>

<a id="gzip-comp-level"></a>

### gzip_comp_level

| [Syntax](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `gzip_comp_level` level;   |
|------------------------------------------------------------------------------------------|----------------------------|
| 默认                                                                                       | `gzip_comp_level 1;`       |
| [Context](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server, location     |

设置响应的 gzip 压缩级别。可接受的值范围从 1 到 9。

<a id="index-3"></a>

<a id="gzip-disable"></a>

### gzip_disable

| [Syntax](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `gzip_disable` regex ...;   |
|------------------------------------------------------------------------------------------|-----------------------------|
| 默认                                                                                       | —                           |
| [Context](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server, location      |

禁止对 `User-Agent` 请求头字段与指定正则表达式匹配的请求进行 gzip 压缩。

特殊的掩码 `msie6` 对应于正则表达式 "MSIE [4-6]."，但工作速度更快。"MSIE 6.0; ... SV1" 被排除在该掩码之外。

<a id="index-4"></a>

<a id="gzip-http-version"></a>

### gzip_http_version

| [Syntax](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `gzip_http_version` `1.0` | `1.1`;   |
|------------------------------------------------------------------------------------------|--------------------------------------|
| 默认                                                                                       | `gzip_http_version 1.1;`             |
| [Context](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server, location               |

设置请求所需的最低 HTTP 版本以压缩响应。

<a id="index-5"></a>

<a id="gzip-min-length"></a>

### gzip_min_length

| [Syntax](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `gzip_min_length` length;   |
|------------------------------------------------------------------------------------------|-----------------------------|
| 默认                                                                                       | `gzip_min_length 20;`       |
| [Context](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server, location      |

设置将被 gzip 压缩的响应的最小长度。长度仅从 `Content-Length` 响应头字段中确定。

<a id="index-6"></a>

<a id="gzip-proxied"></a>

### gzip_proxied

| [Syntax](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `gzip_proxied` `off` | `expired` | `no-cache` | `no-store` | `private` | `no_last_modified` | `no_etag` | `auth` | `any` ...;   |
|------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------|
| 默认                                                                                       | `gzip_proxied off;`                                                                                                             |
| [Context](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server, location                                                                                                          |

根据请求和响应的情况启用或禁用对代理请求的响应进行 gzip 压缩。请求是代理请求的事实由请求头字段 `Via` 的存在来确定。该指令接受多个参数：

| `off`              | 禁用对所有代理请求的压缩，忽略其他参数；                               |
|--------------------|----------------------------------------------------|
| `expired`          | 如果响应头包含 `Expires` 字段且值禁止缓存，则启用压缩；                  |
| `no-cache`         | 如果响应头包含 `Cache-Control` 字段且具有 "no-cache" 参数，则启用压缩； |
| `no-store`         | 如果响应头包含 `Cache-Control` 字段且具有 "no-store" 参数，则启用压缩； |
| `private`          | 如果响应头包含 `Cache-Control` 字段且具有 "private" 参数，则启用压缩；  |
| `no_last_modified` | 如果响应头不包含 `Last-Modified` 字段，则启用压缩；                 |
| `no_etag`          | 如果响应头不包含 `ETag` 字段，则启用压缩；                          |
| `auth`             | 如果请求头包含 `Authorization` 字段，则启用压缩；                  |
| `any`              | 对所有代理请求启用压缩。                                       |

<a id="index-7"></a>

<a id="gzip-types"></a>

### gzip_types

| [Syntax](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `gzip_types` mime-type ...;   |
|------------------------------------------------------------------------------------------|-------------------------------|
| 默认                                                                                       | `gzip_types text/html;`       |
| [Context](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server, location        |

启用对指定 MIME 类型的响应进行 gzip 压缩，除了 `text/html` 之外。特殊值 "\*" 匹配任何 MIME 类型。`text/html` 类型的响应始终被压缩。

<a id="index-8"></a>

<a id="gzip-vary"></a>

### gzip_vary

| [Syntax](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `gzip_vary` `on` | `off`;   |
|------------------------------------------------------------------------------------------|-----------------------------|
| 默认                                                                                       | `gzip_vary off;`            |
| [Context](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server, location      |

启用或禁用在响应头中插入 "Vary: Accept-Encoding" 字段，如果指令 [gzip](#id3)、[gzip_static](https://cn.angie.software//angie/docs/configuration/modules/http/http_gzip_static.md#id3) 或 [gunzip](https://cn.angie.software//angie/docs/configuration/modules/http/http_gunzip.md#id3) 激活。

<a id="built-in-variables-1"></a>

## 内置变量

<a id="v-gzip-ratio"></a>

### `$gzip_ratio`

达到的压缩比率，计算为原始响应大小与压缩响应大小的比率。
