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.

Loading Modules#

Loading the modules in the context of main{}:

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

Example Configuration for Dynamic Compression#

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;
        try_files $uri $uri/ =404;
    }
}

Preparing for Demonstration#

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

$ 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
$ mkdir tmp

Request for Compressed File:#

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

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

Example Configuration for Serving Pre-compressed Files#

server {
    listen 80 default_server;

    brotli_static on;
    brotli_types text/plain text/css;

    location / {
        root   /usr/share/angie/html;
        index index.html;
        try_files $uri $uri/ =404;
    }
}

Moving the Pre-compressed File#

$ 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:#

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

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

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 requested file will occur.

Additional Information#

Full documentation of directives and source code is available at: google/ngx_brotli.