当前位置: 首页 > 工具软件 > Brotli > 使用案例 >

Nginx开启Brotli

唐兴发
2023-12-01

#先下载brotli
git clone https://github.com/google/ngx_brotli.git
#进入目录
cd ngx_brotli
#更新brotli
git clone https://github.com/google/brotli.git
git submodule update --init
#进入nginx源码目录
cd xxx/nginx
#生成makefile,注意根据自己使用的模块添加
./configure  ...  --add-module=../ngx_brotli
#编译nginx
make && make install
如果编译不出错的情况下,输入nginx -V就可以看到ngx_brotli模块了

brotli on; #启用
brotli_comp_level 6; #压缩等级,默认6,最高11,太高的压缩水平可能需要更多的CPU
brotli_buffers 16 8k; #请求缓冲区的数量和大小
brotli_min_length 1k; #指定压缩数据的最小长度,只有大于或等于最小长度才会对其压缩。这里指定1k
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; #指定允许进行压缩类型
brotli_static always; #是否允许查找预处理好的、以.br结尾的压缩文件,可选值为on、off、always
brotli_window 512k; #窗口值,默认值为512k

# .unityweb类型文件添加gzip压缩头
    # 如果Unity中未开启gzip,关闭该配置
    location ~ .+\.unityweb$ {
        add_header Content-Encoding gzip;
    }
 
    # wasm使用gzip压缩,设置MIME type为application/wasm wasm
    location ~ .+\.wasm$ {
        add_header Content-Encoding gzip;
        types {
          application/wasm wasm;
        }

./configure: error: C compiler cc is not found
yum install gcc gcc-c++ -y

Centos8 repo 'AppStream'下载元数据失败
修改 /etc/yum.repos.d/CentOS-Linux-Base.repo,CentOS-Linux-AppStream.repo,CentOS-Linux-Extras.repo
注释mirrorlist,把baseurl取消注释并修改为阿里云镜像地址
baseurl=https://mirrors.aliyun.com/centos/$releasever-stream/BaseOS/$basearch/os/
baseurl=https://mirrors.aliyun.com/centos/$releasever-stream/AppStream/$basearch/os/
baseurl=https://mirrors.aliyun.com/centos/$releasever-stream/extras/$basearch/os/
# 清除所有缓存文件
yum clean all
# 制作元数据缓存
yum makecache

cd nginx-1.*.*
./configure --with-compat --add-dynamic-module=/usr/src/ngx_brotli

./configure: error: the HTTP rewrite module requires the PCRE library.
yum -y install pcre-devel
./configure: error: the HTTP gzip module requires the zlib library.
yum -y install openssl openssl-devel

make modules

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

 类似资料: