nginx—开启Brotli

887 阅读4分钟

brotli下载

cd /usr/src
git clone https://github.com/google/ngx_brotli.git

然后在下载google/brotli并解压到/usr/src/ngx_brotli/deps/brotli

cd /usr/src/ngx_brotli/deps && rm -rf brotli
git clone git@github.com:google/brotli.git
cd /usr/src/ngx_brotli && git submodule update --init

nginx编译动态模块

cd /data/src/nginx-1.14.0 ./configure --with-compat --add-dynamic-module=/usr/src/ngx_brotli make modules

 nginx动态加载库

#Brotli模块 load_module /usr/lib64/nginx/modules/ngx_http_brotli_filter_module.so; load_module /usr/lib64/nginx/modules/ngx_http_brotli_static_module.so;

error ""load_module" directive is specified too late"

根据参考**[针对nginx添加模块,出现nginx: [emerg] "load_module" directive is specified too late in]**将问题解决了

error "is not binary compatible"

参考了几篇文章都是编译的时候根据安装nginx的configure参数一致就可以,试了几遍都不行,最后采用静态编译的方式重新安装nginx

[root@nginx]$./nginx -t
nginx: [emerg] module "/data/server/nginx/module/ngx_http_brotli_filter_module.so" is not binary compatible in /data/server/nginx/nginx.conf:8
nginx: configuration file /data/server/nginx/nginx.conf test failed


configure arguments: 
--prefix=/data/server/nginx \
--sbin-path=/data/server/nginx/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/data/server/nginx/nginx.conf \
--error-log-path=/data/log/nginx/error.log \
--http-log-path=/data/log/nginx/access.log \
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body \
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi \
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi \
--pid-path=/data/server/nginx/run/nginx.pid \
--lock-path=/run/lock/subsys/nginx \
--user=nginx \
--group=nginx \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-http_perl_module=dynamic \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-pcre \
--with-pcre-jit \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-debug \
--add-module=/usr/src/ngx_brotli

静态编译

cd /data/src/nginx-1.14.0
./configure  --add-dynamic-module=/usr/src/ngx_brotli
make && make install

添加Brotli 配置

# btotli
brotli on;
brotli_static always;
brotli_comp_level 6;
brotli_buffers 16 8k;
brotli_min_length 20;
brotli_types text/plain text/css application/json;

验证 

在web目录下创建对应文件

[root@nginx]$cd /data/www/compress/
[root@nginx]$truncate -s 1k test.html
[root@nginx]$truncate -s 1k test.css
[root@nginx]$truncate -s 1k test.js
[root@nginx]$truncate -s 1k test.jpg

发送请求

指定zip

[root@PHP-6-20 nginx]# curl -H "Accept-Encoding:gzip" -I "http://47.98.161.8/compress/test.css"
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Tue, 17 Nov 2020 08:52:39 GMT
Content-Type: text/css
Last-Modified: Tue, 17 Nov 2020 08:10:34 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: W/"5fb3857a-400"
Content-Encoding: gzip

指定多种压缩方式

[root@PHP-6-20 nginx]# curl -H "Accept-Encoding:gzip,deflate,br" -I "http://47.98.161.8/compress/test.html"
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Tue, 17 Nov 2020 11:19:35 GMT
Content-Type: text/html; charset=utf-8
Last-Modified: Tue, 17 Nov 2020 08:10:42 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: W/"5fb38582-400"
Content-Encoding: br

使用curl查看大小

使用truncate生成的大小1024字节 参考意义不大内容很多都是重复的

[root@PHP-6-20 nginx]# curl -H "Accept-Encoding:deflate" -o /dev/null -s -w "%{time_total} %{http_code} %{size_header} %{size_download} \t\n" "http://47.98.161.8/compress/test.html"
0.058 200 277 1024 	
[root@PHP-6-20 nginx]# curl -H "Accept-Encoding:deflate,gzip" -o /dev/null -s -w "%{time_total} %{http_code} %{size_header} %{size_download} \t\n" "http://47.98.161.8/compress/test.html"
0.065 200 287 31 	
[root@PHP-6-20 nginx]# curl -H "Accept-Encoding:deflate,gzip,br" -o /dev/null -s -w "%{time_total} %{http_code} %{size_header} %{size_download} \t\n" "http://47.98.161.8/compress/test.html"
0.069 200 285 11 
Accept-Encodingtime_totalhttp_codesize_headersize_download压缩比例
不压缩0.05820027710240
gzip0.0652002873196%
brotli0.0692002851198%

复制的网上的某个网页的页面内容大小61014字节

[root@PHP-6-20 nginx]# curl -H "Accept-Encoding:deflate" -o /dev/null -s -w "%{time_total} %{http_code} %{size_header} %{size_download} \t\n" "http://47.98.161.8/compress/test_content.html"
0.145 200 279 61014 	
[root@PHP-6-20 nginx]# curl -H "Accept-Encoding:deflate,gzip" -o /dev/null -s -w "%{time_total} %{http_code} %{size_header} %{size_download} \t\n" "http://47.98.161.8/compress/test_content.html"
0.059 200 288 10453 	
[root@PHP-6-20 nginx]# curl -H "Accept-Encoding:deflate,gzip,br" -o /dev/null -s -w "%{time_total} %{http_code} %{size_header} %{size_download} \t\n" "http://47.98.161.8/compress/test_content.html"
0.075 200 286 8400
Accept-Encodingtime_totalhttp_codesize_headersize_download压缩比例
不压缩0.145200279610140
gzip0.0592002881045382%
brotli0.075200286840086%

动态文件

[root@PHP-6-20 nginx]# curl -H "Accept-Encoding:deflate" -o /dev/null -s -w "%{time_total} %{http_code} %{size_header} %{size_download} \t\n" "http://47.98.161.8/compress/index.php"
0.141 200 218 83927 	
[root@PHP-6-20 nginx]# curl -H "Accept-Encoding:deflate,gzip" -o /dev/null -s -w "%{time_total} %{http_code} %{size_header} %{size_download} \t\n" "http://47.98.161.8/compress/index.php"
0.103 200 242 23570 	
[root@PHP-6-20 nginx]# curl -H "Accept-Encoding:deflate,gzip,br" -o /dev/null -s -w "%{time_total} %{http_code} %{size_header} %{size_download} \t\n" "http://47.98.161.8/compress/index.php"
0.096 200 240 20446 	
Accept-Encodingtime_totalhttp_codesize_headersize_download压缩比例
不压缩0.141200218839270
gzip0.1032002422357071%
brotli0.0962002402044675%

动态json文本

[root@PHP-6-20 nginx]# curl -H "Accept-Encoding:deflate" -o /dev/null -s -w "%{time_total} %{http_code} %{size_header} %{size_download} \t\n" "http://47.98.161.8/compress/json.php"
0.118 200 218 45683 	
[root@PHP-6-20 nginx]# curl -H "Accept-Encoding:gzip" -o /dev/null -s -w "%{time_total} %{http_code} %{size_header} %{size_download} \t\n" "http://47.98.161.8/compress/json.php"
0.102 200 242 14708 	
[root@PHP-6-20 nginx]# curl -H "Accept-Encoding:br" -o /dev/null -s -w "%{time_total} %{http_code} %{size_header} %{size_download} \t\n" "http://47.98.161.8/compress/json.php"
0.070 200 240 12192 
Accept-Encodingtime_totalhttp_codesize_headersize_download压缩比例
不压缩0.118200218456830
gzip0.1022002421470867%
brotli0.0702002401219273%

使用浏览器

brotli

gzip

brotli gzip

问题

如何通过浏览器访问携带br压缩?
安装ModHeader插件更改header头信息

总结

相对来说brotli压缩比zip算法压过比例会更高一些

参考

Git Submodule使用完整教程
Nginx启用Brotli压缩
入门系列之在Nginx配置Gzip
linux命令--truncate 学习
Nginx添加第三方模块,出现“is not binary compatible”错误的解决办法
针对nginx添加模块,出现nginx: [emerg] "load_module" directive is specified too late in
把Gzip换成Brotli的Nginx配置教程
www.cnblogs.com/-wenli/p/13…
curl -w,–write-out参数详解
curl Accept-Encoding ModHeader是什么