宝塔面板 Nginx启用Brotli

如果要让 Nginx 支持 Brotli 压缩 ,首先需要安装 [ngx_brotli 模块],具体方式如下:

cd /www/server
git clone --recurse-submodules -j8 https://github.com/google/ngx_brotli

添加 ngx_brotli 编译参数

echo "--add-module=/www/server/ngx_brotli" > /www/server/panel/install/nginx_configure.pl

以上添加方式适合所有 Nginx 版本安装,且包括未来升级或切换 Nginx 版本无需再另行添加。

在宝塔面板软件商店中选择 Nginx 1.25 并点击编译安装即可。

配置 Brotli 压缩

在宝塔面板中,点开 Nginx 设置 > 配置修改,并搜索 gzip_disable

在这一行的下方插入以下内容:

brotli on;
brotli_comp_level 6;    #压缩等级,默认6,最高11,高的压缩等级需要更多的CPU
brotli_buffers 16 8k;   #请求缓冲区的数量和大小
brotli_min_length 20;   #指定压缩数据的最小字节数,只有大于或等于最小长度才会对其压缩。
brotli_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml text/html application/json image/svg application/font-woff application/vnd.ms-fontobject application/vnd.apple.mpegurl image/x-icon image/jpeg image/gif image/png image/bmp application/vnd.api+json; #指定允许进行压缩类型
brotli_static always;   #是否允许查找预处理好的、以.br结尾的压缩文件,可选值为on、off、always
brotli_window 512k;     #窗口值,默认值为512k

保存并重启 Nginx 即可。

经过以上配置后,Nginx 将支持 Gzip 与 Brotli,浏览器支持 Brotli 的情况下,会默认启用 Brotli 压缩。

  • All rights reserved.
  • No part of this website, including text and images, may be reproduced, modified, distributed, or transmitted in any form or by any means, without the prior written permission of the author.
  • Unauthorized commercial use is strictly prohibited.
  • Unauthorized personal use is strictly prohibited.
0 comment A文章作者 M管理员
    No Comments Yet. Be the first to share what you think