<!-- review: finished -->

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

# Brotli

This is a set of two modules:

- `ngx_brotli_filter` — used for on-the-fly compression of responses.
- `ngx_brotli_static` — used for serving pre-compressed files.

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

## Installation

To [install](https://en.angie.software//angie/docs/installation/index.md#install-packages) the module, use one of the following packages:

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

<a id="loading-modules"></a>

## Loading Modules

Loading the modules in the `main{}` context:

```nginx
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
```

<a id="example-configuration-for-dynamic-compression"></a>

## Example Configuration for Dynamic Compression

```nginx
server {
    listen 80 default_server;
    brotli on;
    brotli_comp_level 1;
    brotli_types text/plain text/css;

    location / {
        root /usr/share/angie/html;
        index index.html;
    }
}
```

<a id="preparing-for-demonstration"></a>

## Preparing for Demonstration

First, let's place the test file `war-and-peace.txt`:

```console
$ ls -l /usr/share/angie/html/

  total 3292
  -rw-r--r-- 1 root root     497 Feb 13 07:40 50x.html
  -rw-r--r-- 1 root root     543 Feb 13 07:40 index.html
  -rw-r--r-- 1 root root 3359405 Feb 26 12:47 war-and-peace.txt
```

```console
$ mkdir tmp
```

Request for compressed file:

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

```console
$ ls -l tmp/

  total 1092
  -rw-r--r-- 1 asv asv 1115616 Feb 26 16:52 war-and-peace.br
```

<a id="example-configuration-for-serving-pre-compressed-files"></a>

## Example Configuration for Serving Pre-compressed Files

```nginx
server {
    listen 80 default_server;

    brotli_static on;
    brotli_types text/plain text/css;

    location / {
        root /usr/share/angie/html;
        index index.html;
    }
}
```

<a id="moving-the-pre-compressed-file"></a>

## Moving the Pre-compressed File

```console
$ sudo mv tmp/war-and-peace.br /usr/share/angie/html/war-and-peace.txt.br

$ ls -l /usr/share/angie/html/

  total 4384
  -rw-r--r-- 1 root root     497 Feb 13 07:40 50x.html
  -rw-r--r-- 1 root root     543 Feb 13 07:40 index.html
  -rw-r--r-- 1 root root 3359405 Feb 26 12:47 war-and-peace.txt
  -rw-r--r-- 1 root root 1115616 Feb 26 16:57 war-and-peace.txt.br
```

Request for compressed file:

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

```console
$ ls -l tmp/

  total 1092
  -rw-r--r-- 1 asv asv 1115616 Feb 26 17:13 war-and-peace.br
```

<a id="combining-dynamic-and-static-compression"></a>

## Combining Dynamic and Static Compression

In one configuration, it is possible to combine the use of dynamic compression
(`brotli on`) and static selection (`brotli_static on`). In this case, the system will first look for the corresponding static compressed file. If such a file is not found, dynamic compression of the file specified in the request will occur.

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

## Additional Information

Full documentation of directives and source code is available at:
[https://github.com/google/ngx_brotli](https://github.com/google/ngx_brotli).
