Brotli 压缩算法配置教程

Brotli 是 Google 开发的现代压缩算法,相比传统的 gzip 在相同压缩级别下可以获得 15-25% 更高的压缩率。所有主流浏览器均已支持 Brotli(通过 HTTPS 连接时)。在搬瓦工 VPS 上为 Nginx 启用 Brotli 压缩,可以显著减少传输数据量,加快页面加载速度。

一、Brotli 与 gzip 对比

  • 压缩率:Brotli 比 gzip 高 15-25%,特别是对 HTML、CSS、JavaScript 效果显著。
  • 压缩速度:低压缩级别(1-4)时 Brotli 速度接近 gzip,高级别时较慢。
  • 解压速度:Brotli 解压速度与 gzip 相当。
  • 兼容性:Brotli 仅在 HTTPS 下生效,gzip 可用于所有连接。
  • 建议:同时启用 Brotli 和 gzip,浏览器不支持 Brotli 时自动降级到 gzip。

二、编译安装 Nginx Brotli 模块

2.1 安装依赖

apt update
apt install build-essential libpcre3-dev zlib1g-dev libssl-dev \
  libgd-dev libgeoip-dev git cmake -y

2.2 下载源码

# 查看当前 Nginx 版本
nginx -v

# 下载对应版本的 Nginx 源码
cd /usr/src
NGINX_VER=$(nginx -v 2>&1 | grep -oP '\d+\.\d+\.\d+')
wget http://nginx.org/download/nginx-${NGINX_VER}.tar.gz
tar xzf nginx-${NGINX_VER}.tar.gz

# 下载 Brotli 模块
git clone --recurse-submodules https://github.com/google/ngx_brotli.git

2.3 编译动态模块

cd /usr/src/nginx-${NGINX_VER}

# 查看当前 Nginx 编译参数
nginx -V 2>&1 | grep "configure arguments"

# 添加 Brotli 模块重新编译(保留原有参数)
./configure --with-compat --add-dynamic-module=../ngx_brotli
make modules

# 安装模块
cp objs/ngx_http_brotli_filter_module.so /etc/nginx/modules/
cp objs/ngx_http_brotli_static_module.so /etc/nginx/modules/

三、配置 Nginx Brotli

# 在 nginx.conf 顶部加载模块
cat > /etc/nginx/modules-enabled/50-brotli.conf <<'EOF'
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
EOF

# 在 http 块中添加 Brotli 配置
cat > /etc/nginx/conf.d/brotli.conf <<'EOF'
# Brotli 动态压缩
brotli on;
brotli_comp_level 6;
brotli_min_length 256;
brotli_types
    text/plain
    text/css
    text/javascript
    text/xml
    application/javascript
    application/json
    application/xml
    application/rss+xml
    application/atom+xml
    image/svg+xml
    font/opentype
    font/ttf
    font/woff
    font/woff2;

# Brotli 静态压缩(预压缩文件)
brotli_static on;
EOF

# 测试配置并重载
nginx -t
systemctl reload nginx

四、压缩级别选择

Brotli 支持 0-11 共 12 个压缩级别:

  • 级别 1-4:适合动态内容实时压缩,速度快,CPU 开销低。
  • 级别 5-6:推荐的平衡选择,压缩率与速度兼顾。
  • 级别 7-9:压缩率更高,但 CPU 开销明显增加。
  • 级别 10-11:仅适合预压缩静态文件,实时压缩太慢。

搬瓦工 VPS 推荐动态压缩使用级别 4-6,静态预压缩使用级别 11。

五、静态文件预压缩

# 安装 Brotli 命令行工具
apt install brotli -y

# 预压缩静态文件
find /var/www/html -type f \( -name "*.html" -o -name "*.css" -o -name "*.js" -o -name "*.svg" -o -name "*.xml" -o -name "*.json" \) \
  -exec brotli --best --keep {} \;

# 批量预压缩脚本
cat > /usr/local/bin/brotli-precompress.sh <<'EOF'
#!/bin/bash
WEB_ROOT="${1:-/var/www/html}"
find "$WEB_ROOT" -type f \( -name "*.html" -o -name "*.css" -o -name "*.js" -o -name "*.svg" -o -name "*.xml" -o -name "*.json" \) | while read file; do
    if [ ! -f "${file}.br" ] || [ "$file" -nt "${file}.br" ]; then
        brotli --best --keep "$file"
        echo "Compressed: $file"
    fi
done
EOF
chmod +x /usr/local/bin/brotli-precompress.sh

启用 brotli_static on 后,Nginx 会自动查找与请求文件同名的 .br 文件,如果存在则直接发送预压缩版本,避免实时压缩的 CPU 开销。

六、验证 Brotli 是否生效

# 使用 curl 测试(需要 HTTPS)
curl -H "Accept-Encoding: br" -I https://your-domain.com/

# 查看响应头中是否包含
# Content-Encoding: br

# 使用 curl 对比压缩效果
# gzip 版本
curl -H "Accept-Encoding: gzip" -so /dev/null -w "gzip: %{size_download} bytes\n" https://your-domain.com/

# Brotli 版本
curl -H "Accept-Encoding: br" -so /dev/null -w "brotli: %{size_download} bytes\n" https://your-domain.com/

七、同时启用 gzip 和 Brotli

# gzip 作为回退方案
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_types text/plain text/css application/javascript application/json
  application/xml image/svg+xml font/opentype font/ttf;
gzip_vary on;

# Brotli 优先(浏览器支持时自动使用 Brotli)
brotli on;
brotli_comp_level 6;
brotli_min_length 256;
brotli_types text/plain text/css application/javascript application/json
  application/xml image/svg+xml font/opentype font/ttf;

八、性能影响监控

# 监控 CPU 使用变化
vmstat 1 10

# 使用 wrk 测试压缩前后的吞吐量变化
wrk -t2 -c50 -d30s -H "Accept-Encoding: br" https://your-domain.com/

# 对比不同压缩级别的 CPU 开销
for level in 1 4 6 9 11; do
    echo "=== Brotli Level $level ==="
    time brotli -$level -k /var/www/html/large-file.js
    ls -la /var/www/html/large-file.js.br
    rm /var/www/html/large-file.js.br
done

总结

Brotli 压缩能够显著减少网页传输大小,是 Web 性能优化的重要手段。配合 WebP 图片优化HTTP/2 升级Varnish 缓存,可以全方位提升搬瓦工 VPS 上网站的加载速度。选购搬瓦工 VPS 请查看 全部方案,购买时使用优惠码 NODESEEK2026 可享受 6.77% 的折扣,通过 bwh81.net 访问官网。

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。