Unbrotli#

The unbrotli module is designed for unpacking responses from the backend that use Brotli compression (Content-Encoding: br) for clients that do not support this compression method. This is particularly useful in cases where storing data in compressed form on the backend saves space.

Directives#

The module provides the following directives:

Loading the Module#

To use the module, it must be loaded in the context of main{}:

load_module modules/ngx_http_unbrotli_filter_module.so;

Configuration Example#

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;
    }
}

# Backend
server {
    listen 8080;
    location /storage {
        root   /usr/share/angie;
        rewrite ^(.*)$ $1.br break;  # Return the compressed file with .br suffix
        add_header Content-Encoding br; # Indicate Brotli compression in the response header
    }
}

Demonstration#

Let's place the compressed test file war-and-peace.txt.br:

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

If the client supports Brotli, it will receive the compressed file without decompression:

$ 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

If the client does not support Brotli, the unbrotli module will decompress the file on the server before sending:

$ 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

The file was decompressed by the server before being sent to the client.

Additional Information#

Detailed documentation and source code are available at: clyfish/ngx_unbrotli.