Brotli#

Este é um conjunto de dois módulos:

  • ngx_brotli_filter — usado para compressão em tempo real de respostas.

  • ngx_brotli_static — usado para servir arquivos pré-comprimidos.

Instalação#

Para instalar o módulo, use um dos seguintes pacotes:

  • Angie: angie-module-brotli

  • Angie PRO: angie-pro-module-brotli

Carregando Módulos#

Carregando os módulos no contexto de main{}:

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

Exemplo de Configuração para Compressão Dinâmica#

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

Preparando para Demonstração#

Primeiro, vamos colocar o arquivo de teste 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

Solicitação de arquivo comprimido:

$ 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

Exemplo de Configuração para Servir Arquivos Pré-comprimidos#

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

Movendo o Arquivo Pré-comprimido#

$ 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

Solicitação de arquivo comprimido:

$ 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

Combinando Compressão Dinâmica e Estática#

Em uma configuração, é possível combinar o uso de compressão dinâmica (brotli on) e seleção estática (brotli_static on). Neste caso, o sistema primeiro procurará pelo arquivo comprimido estático correspondente. Se tal arquivo não for encontrado, ocorrerá a compressão dinâmica do arquivo solicitado.

Informações Adicionais#

Documentação completa das diretivas e código-fonte está disponível em: google/ngx_brotli.