Aaron Lau(韩栖)

Nginx 学习笔记 (7) —— 启用 Brotli 压缩

287 字

Brotli 是 Google 研发的一种新兴压缩算法,可以加快网页的载入速度。相较于 Gzip 压缩率更高、性能也非常好,再基于 Chrome 的市场支配地位,Brotli 便得到迅速普及。不过要注意,它仅适用于 HTTPS。

1) 下载 Brotli 的源码;

yum install git && cd /usr/local/src
git clone https://github.com/google/ngx_brotli.git
pushd ngx_brotli
git submodule update --init
popd

2) 执行 nginx -V,configure arguments 即为现有的参数。追加 --add-module=../ngx_brotli,重新编译。如需平滑升级,make 之后请不要 make install,而是替换二进制后 make upgrade;

cd nginx-1.16.1
./configure \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_sub_module \
--with-openssl=../openssl-1.1.1c \
--add-module=../ngx_brotli
make
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
cp objs/nginx /usr/local/nginx/sbin/nginx
make upgrade

3) 修改 nginx.conf。缺省值就够用,不妨再留意一下 gzip_types 和 brotli_types,以允许压缩 text/html 以外的文件;

http {
    ...
    gzip on;
    brotli on;
    # brotli_types text/css text/javascript application/rss+xml;
    ...
}

4) 重载 Nginx,可借由 Brotli Test 等工具检验效果。

nginx -t && nginx -s reload

<small>原载 App-scope,有删改。</small>

App-scope · 教程 · Nginx