nginx和apache添加brotli算法压缩网站

什么是brotli?

brotli是Google开发的最新压缩算法,有效减少网站传输数据
具体内容请查看WIKI
https://en.wikipedia.org/wiki/Brotli

安装依赖文件[仅限centos]

yum groupinstall 'Development Tools' -y
yum install cmake -y

编译安装brotli库

wget https://github.com/google/brotli/archive/v1.0.3.tar.gz
tar -zxvf v1.0.3.tar.gz
cd brotli-1.0.3
./configure-cmake
make 
make test 
make install

apache/nginx添加编译参数

 "--enable-brotli"  
"--with-brotli=/usr/local/lib"    #apache官方模块,依赖brotli库 

--add-module=../ngx_brotli-master   #添加ngx_brotli模块编译

ngx_brotli模块下载地址
https://github.com/google/ngx_brotli
https://github.com/eustas/ngx_brotli

apache/nginx修改配置文件

http://httpd.apache.org/docs/2.4/mod/mod_brotli.html
apache修改文件 /etc/httpd/conf/extra/httpd-deflate.conf

<IfModule brotli_module> 
 SetOutputFilter BROTLI_COMPRESS;DEFLATE
 SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip no-brotli dont-vary
 BrotliCompressionQuality  6
 BrotliCompressionWindow 18
 AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css text/xml
 AddOutputFilterByType BROTLI_COMPRESS application/x-javascript application/javascript
 AddOutputFilterByType BROTLI_COMPRESS application/rss+xml
 AddOutputFilterByType BROTLI_COMPRESS application/xml
 AddOutputFilterByType BROTLI_COMPRESS application/json
</IfModule>
#nginx配置文件  
    brotli on;
    brotli_types text/html text/plain text/javascript text/css text/xml text/x-component application/javascript application/x-javascript application/xml application/json application/xhtml+xml application/rss+xml application/atom+xml application/x-font-ttf application/vnd.ms-fontobject image/svg+xml image/x-icon font/opentype;
    brotli_static off;
    brotli_comp_level 6;
    brotli_buffers 8 16k;
    brotli_window 512k;
    brotli_min_length 512;

出错解决办法

nginx: error while loading shared libraries: libbrotlienc.so.1: cannot open shared object file: No such file or directory
可行的解决方案之一,是把对应的库文件做软链接:

# 64 位系统  
 ln -s /usr/local/lib/libbrotlienc.so.1 /lib64 
 ln -s /usr/local/lib/libbrotlicommon.so.1  /lib64

# 32 位系统  
ln -s /usr/local/lib/libbrotlienc.so.1 /lib 
ln -s /usr/local/lib/libbrotlicommon.so.1  /lib

重载nginx,若无报错,即问题解决
nginx -s reload

Nginx 启用 Brotli 压缩

Brotli 是 Google 开发的一种压缩格式,它通过内置分析大量网页得出的字典,实现了更高的压缩比率,同时几乎不影响压缩 / 解压速度。

本站通过 ngx_brotli 模块来让 Nginx 支持 Brotli 压缩方式。本文介绍其配置方式。

安装模块

若要启用 ngx_brotli 模块,需要在编译 Nginx 时,加入相应模块:

# get source

git clone https://github.com/google/ngx_brotli.git

cd ngx_brotli

git submodule update --init

cd ..

# configure

./configure ... --add-module=../ngx_brotli

配置文件

安装完成 ngx_brotli 模块后,你就可以在配置文件里启用它了:

# 配置段: http, server, location

# 开启 ngx_brotli 压缩

brotli on;

# 指定压缩数据的最小长度,只有大于或等于最小长度才会对其压缩。这里指定 20 字节

brotli_min_length 20;

# Brotli 请求缓冲区的数量和大小

brotli_buffers 16 10k;

# Brotli 使用的窗口值。默认值为 512k

brotli_window 512k;

# 压缩水平可以是 0 到 11,默认值是 6。太高的压缩水平对性能提升并没有太大好处,因为这需要更多的 CPU 时间

brotli_comp_level 6;

# 指定允许进行压缩的回复类型

brotli_types text/html text/xml text/plain application/json text/css image/svg application/font-woff application/vnd.ms-fontobject application/vnd.apple.mpegurl application/javascript image/x-icon image/jpeg image/gif image/png;

# 是否允许查找预处理好的、以 .br 结尾的压缩文件。可选值为 on、off、always

brotli_static always;

未分类