比Gzip压缩效率更高的 Brotli

Brotli是一种比Gzip压缩效率更高的压缩算法。实测会有20%左右的体积降低。Brotli的压缩时间和CPU占用会比Gzip高,解压时间比Gzip低。相当于通过牺牲部分服务端性能来提升资源请求加载速度,硬件条件较好情况下,是一种不错的选择。

benchmarks数据

Nginx 开启 Brotli

安装

以Debain系统为例,安装Brotli有两种方式,第一种是,使用Nginx编译安装参考:

https://bash-prompt.net/guides/nginx-brotli-debian-10/

第二种是安装编译好的软件包参考:

https://gilslotd.com/blog/ngx_brotli_debian_bullseye_and_buster/

开启

vi /etc/nginx/nginx.conf,加入如下配置

http {

  # 开启brotli
  brotli on;
  # 配置生效的文件类型
  brotli_types application/atom+xml application/javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype application/x-font-ttf application/x-javascript application/xhtml+xml application/xml font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;
}

配置项详细说明

CDN 开启 Brotli

常用的CDN服务商已支持开启Brotli,若接入了CDN服务商,优先使用CDN服务商配置开启。