跳转至

在 NGINX 上启用 Brotli 压缩

使用 Google 的 Brotli 算法将您的内容压缩比 gzip 更好 15-25%。


  • 小 15-25%


    Brotli 对文本内容的压缩比 gzip 更好

  • 通用支持


    所有现代浏览器都支持 Brotli(Chrome、Firefox、Safari、Edge)

  • 更快的页面加载


    文件更小 = 下载更快,尤其是在移动网络上

  • 降低带宽成本


    通过更好的压缩减少数据传输成本


浏览器支持

Brotli 在全球 95%+ 的浏览器 中得到支持:

浏览器 Brotli 支持
Chrome ✅ 自 v50(2016)
Firefox ✅ 自 v44(2016)
Safari ✅ 自 v11(2017)
Edge ✅ 自 v15(2017)
Opera ✅ 自 v38(2016)

NGINX 会自动为旧版浏览器回退到 gzip。


快速设置

步骤 1:安装 Brotli 模块

# 安装 GetPageSpeed 仓库
dnf -y install https://extras.getpagespeed.com/release-latest.rpm

# 安装 Brotli 模块
dnf -y install nginx-module-brotli

/etc/nginx/nginx.conf 中启用:

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

步骤 2:配置 Brotli 压缩

创建 /etc/nginx/conf.d/brotli.conf

# 启用 Brotli 压缩
brotli on;
brotli_comp_level 6;
brotli_static on;

brotli_types
    text/plain
    text/css
    text/javascript
    text/xml
    application/javascript
    application/json
    application/xml
    application/xml+rss
    application/xhtml+xml
    application/atom+xml
    application/rss+xml
    application/x-javascript
    application/x-font-ttf
    application/x-font-opentype
    application/vnd.ms-fontobject
    font/ttf
    font/otf
    font/opentype
    font/woff
    font/woff2
    image/svg+xml
    image/x-icon;

重新加载 NGINX:

nginx -t && systemctl reload nginx

步骤 3:验证是否正常工作

# 请求时带 Brotli 支持
curl -sI -H 'Accept-Encoding: br' https://example.com | grep -i encoding
# Content-Encoding: br

# 比较大小
curl -so /dev/null -w '%{size_download}' -H 'Accept-Encoding: gzip' https://example.com
# 45678
curl -so /dev/null -w '%{size_download}' -H 'Accept-Encoding: br' https://example.com
# 38912  (更小!)

压缩级别

级别 速度 压缩 用例
1-3 较低 高流量,CPU 受限
4-6 平衡 良好 大多数网站(推荐)
7-9 较慢 更好 预压缩的静态资产
10-11 非常慢 最佳 仅在构建时压缩

推荐

对于动态内容使用级别 4-6,对于预压缩的静态资产使用级别 11


预压缩静态资产

为了获得最佳性能,在构建时预压缩文件:

# 安装 Brotli CLI 工具
dnf -y install brotli

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

这会在原始文件旁边创建 .br 文件。NGINX 会自动使用 brotli_static on 提供这些文件。


大小比较

真实世界示例(jQuery 3.6.0 最小化):

%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#009639', 'primaryTextColor': '#fff'}}}%%
pie showData
    title 文件大小比较
    "原始 (89KB)" : 89
    "Gzip (31KB)" : 31
    "Brotli (27KB)" : 27
格式 大小 节省
原始 89 KB
Gzip 31 KB 65%
Brotli 27 KB 70%

高级配置

保留 Gzip 作为后备

# Brotli(优先)
brotli on;
brotli_comp_level 6;

# Gzip(后备)
gzip on;
gzip_comp_level 5;
gzip_types text/plain text/css application/javascript application/json;

动态与静态使用不同级别

# 动态内容 - 更快的压缩
brotli on;
brotli_comp_level 4;

# 预压缩的静态文件 - 最佳压缩
brotli_static on;

最小大小阈值

# 不压缩小文件(不值得)
brotli_min_length 256;

故障排除

Brotli 无法工作
  1. 检查模块是否已加载:

    nginx -V 2>&1 | grep brotli
    

  2. 验证请求中包含 Accept-Encoding: br

  3. 检查响应的 Content-Type 是否在 brotli_types

CPU 使用率过高

降低压缩级别:

brotli_comp_level 4;  # 或者甚至 2-3

文件未被压缩
  • 检查文件大小 > brotli_min_length
  • 验证 Content-Type 是否与 brotli_types 匹配
  • 检查连接是否已经被压缩(CDN)

相关

  • Brotli 模块


    完整的指令参考

    文档

  • Unbrotli 模块


    从后端解压 Brotli

    文档

  • PageSpeed 模块


    额外的优化层

    文档