<!-- review: finished -->

<a id="external-unbrotli"></a>

# Unbrotli

`unbrotli` 模块用于为不支持 Brotli 压缩方法的客户端解压来自后端使用 Brotli 压缩(`Content-Encoding: br`)的响应。这在后端以压缩形式存储数据以节省空间的情况下特别有用。

<a id="installation-28"></a>

## 安装

要 [安装](https://cn.angie.software//angie/docs/installation/index.md#install-packages) 该模块,请使用以下软件包之一:

- Angie:`angie-module-unbrotli`
- Angie PRO:`angie-pro-module-unbrotli`

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

## 指令

该模块提供以下指令:

- `unbrotli` – 类似于 [gunzip](https://cn.angie.software//angie/docs/configuration/modules/http/http_gunzip.md#id3)
- `unbrotli_buffers` – 类似于 [gunzip_buffers](https://cn.angie.software//angie/docs/configuration/modules/http/http_gunzip.md#gunzip-buffers)

<a id="loading-the-module-28"></a>

## 加载模块

要使用该模块,必须在 `main{}` 上下文中加载它:

```nginx
load_module modules/ngx_http_unbrotli_filter_module.so;
```

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

## 配置示例

```nginx
server {
    listen 80 default_server;
    location / {
        root  /usr/share/angie/html;
        index index.html;
    }

    location /storage {
        unbrotli on;
        proxy_pass http://127.0.0.1:8080;
    }
}

# 后端
server {
    listen 8080;
    location /storage {
        root   /usr/share/angie;
        rewrite ^(.*)$ $1.br break;  # 返回带 .br 后缀的压缩文件
        add_header Content-Encoding br; # 在响应头中指示 Brotli 压缩
    }
}
```

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

## 演示

让我们放置压缩的测试文件 `war-and-peace.txt.br`:

```console
$ ls -l /usr/share/angie/storage/
total 2292
-rw-r--r-- 1 root root 1115616 Feb 27 16:10 war-and-peace.txt.br
```

如果客户端支持 Brotli,它将接收压缩文件而不进行解压:

```console
$ curl -s -H 'Accept-Encoding: br' -o tmp/war-and-peace.txt localhost/storage/war-and-peace.txt

$ ls -l tmp/
total 1092
-rw-r--r-- 1 asv asv 1115616 Feb 27 16:36 war-and-peace.txt
```

如果客户端不支持 Brotli,:samp:unbrotli 模块将在发送前在服务器上解压文件:

```console
$ curl -s -o tmp/war-and-peace.txt localhost/storage/war-and-peace.txt

$ ls -l tmp/
total 3284
-rw-r--r-- 1 asv asv 3359405 Feb 27 16:39 war-and-peace.txt
```

该文件在发送给客户端之前已由服务器解压。

<a id="additional-information-29"></a>

## 附加信息

详细文档和源代码可在以下位置获取:
[https://github.com/clyfish/ngx_unbrotli](https://github.com/clyfish/ngx_unbrotli)。
