Skip to content

Brotli

默认在 HTTPS 下,请求头的 Accept-Encoding 中才会携带 br。

下载

https://codeload.github.com/google/brotli/tar.gz/refs/tags/v1.0.9https://codeload.github.com/google/ngx_brotli/tar.gz/refs/tags/v1.0.0rc

解压

tar -xzvf brotli-1.0.9.tar.gz tar -xzvf ngx_brotli-1.0.0rc.tar.gz

移动依赖的算法包

cd /root/brotli-1.0.9 mv ./* /root/ngx_brotli-1.0.0rc/deps/brotli/

编译

cd /root/nginx-1.18.0 ./configure --with-compat --add-dynamic-module=/root/ngx_brotli-1.0.0rc --prefix=/usr/local/nginx/ make

mkdir /usr/local/nginx/modules cd /root/nginx-1.18.0/objs cp ./ngx_http_brotli_filter_module.so /usr/local/nginx/modules cp ./ngx_http_brotli_static_module.so /usr/local/nginx/modules mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old cp ./nginx /usr/local/nginx/sbin

配置项

brotli_static on | off | always; brotli on | off; brotli_types text/html; brotli_buffers 32 4k | 16 8k; brotli_comp_level 6; brotli_window 512k; brotli_min_length 20;

nginx
# 动态加载模块
load_module "/usr/local/nginx/modules/ngx_http_brotli_filter_module.so";
load_module "/usr/local/nginx/modules/ngx_http_brotli_static_module.so";

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout 60;

    server {
        listen       80;
        server_name  192.168.44.101;
        brotli on;
        brotli_static on;
        brotli_comp_level 6;
        brotli_buffers 16 8k;
        brotli_min_length 20;
        brotli_types text/plain text/css text/javascript application/javascript;

        location / {
          root html;
          index index.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

HTTP 中可以通过请求头手动设置 Accept-Encoding: br 开启 Brotli

curl -H 'Accept-Encoding: br' -I http://192.168.44.101