为 nginx 启用 Brotli 压缩算法,提高性能

2016-08-25 20:41:18 +08:00
 wql
https://wangqiliang.com/qi-yong-brotli-ya-suo-suan-fa-ti-gao-xing-neng/

必要资源
将该算法运用到nginx中,需要用到以下的库(Github上):

google/brotli
google/ngx_brotli
bagder/libbrotli
安装过程:
1.首先安装 bagder/libbrotli

$ git clone https://github.com/bagder/libbrotli
$ cd libbrotli

# 备注,下一步所执行的 autogen.sh 会自动从 Github 下载 google/brotli 这个库,无需另外下载。
$ ./autogen.sh

$ ./configure
$ make && make install
2.然后安装 google/ngx_brotli:

$ git clone https://github.com/bagder/libbrotli

$ cd nginx-1.11.3

# 此处应该有你自己的配置,再加上 google/ngx_brotli 库
$ ./configure --add-module=/path/to/ngx_brotli
$ make && make install
3.根据Github页面,配置nginx.conf,在http配置段增加以下配置:

brotli on;
brotli_comp_level 6;
brotli_buffers 16 8k;
brotli_min_length 20;
brotli_types *;
4.考虑到部分基于node.js的博客会在后台自动Gzip,所以在网站反代配置里加上下面这一句话。
这句话的目的是,告知后端:前端不接受Gzip编码,不要做Gzip:

proxy_set_header Accept-Encoding "";
5.最后,测试配置,重载生效:

$ nginx -t
$ nginx -s reload
可能的报错
如果在测试或者重载时,nginx报错如下:

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

# 32 位系统
$ ln -s /usr/local/lib/libbrotlienc.so.1 /lib
重载nginx,若无报错,即问题解决:

$ nginx -s reload
7204 次点击
所在节点    NGINX
17 条回复
shuax
2016-08-25 20:51:43 +08:00
怎么能提高性能,应该是节约流量吧。
wql
2016-08-25 20:55:50 +08:00
@shuax 参考了谷歌官方的描述:It is similar in speed with deflate but offers more dense compression.
的确是在保持了速度的同时节约流量。
shyling
2016-08-25 21:07:40 +08:00
话说这样设置的话在浏览器没有发 accept-encoding:br 时也会用 brotli 压缩么
ivmm
2016-08-25 21:13:22 +08:00
提高性能的原理是什么?
knightdf
2016-08-25 21:16:20 +08:00
既然是要提高性能,那我不压缩不是性能最高?
lhbc
2016-08-25 21:16:49 +08:00
能和 gzip 共存吗?
如果不支持共存,不支持 brotli 的浏览器是不是不能使用任何压缩?
wql
2016-08-25 21:17:47 +08:00
@shyling 不行,识别就靠这个

@lhbc 能和 gzip 共存
wql
2016-08-25 21:18:35 +08:00
@ivmm 准确来说是因为采用了一些技术让相关的压缩包在压缩速度基本不受影响的情况下密度最大
qgy18
2016-08-26 00:13:07 +08:00
👍 我的博客前几天已经用上了。

这个算法主要是内置了分析大量网页之后提取的字典,所以能在压缩比更高的同时不影响解压速度。
chinaiy
2016-11-24 09:50:34 +08:00
@qgy18 看你博客配置完整篇一步一步操作,编译和安装 Nginx 这一步: make ,出现下面的错误,昨天研究到现在都不知道怎么解决,求助,谢谢

cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I ../openssl/.openssl/include -I objs -I src/http -I src/http/modules -I src/http/v2 \
-o objs/addon/src/ngx_http_brotli_filter_module.o \
../ngx_brotli/src/ngx_http_brotli_filter_module.c
../ngx_brotli/src/ngx_http_brotli_filter_module.c: In function ‘ ngx_http_brotli_body_filter ’:
../ngx_brotli/src/ngx_http_brotli_filter_module.c:272:9: error: ‘ BrotliEncoderInputBlockSize ’ is deprecated (declared at /usr/local/include/brotli/encode.h:87) [-Werror=deprecated-declarations]
ctx->brotli_ring = BrotliEncoderInputBlockSize(ctx->encoder);
^
../ngx_brotli/src/ngx_http_brotli_filter_module.c: In function ‘ ngx_http_brotli_filter_add_data ’:
../ngx_brotli/src/ngx_http_brotli_filter_module.c:498:5: error: ‘ BrotliEncoderCopyInputToRingBuffer ’ is deprecated (declared at /usr/local/include/brotli/encode.h:95) [-Werror=deprecated-declarations]
BrotliEncoderCopyInputToRingBuffer(ctx->encoder, size, b->pos);
^
../ngx_brotli/src/ngx_http_brotli_filter_module.c: In function ‘ ngx_http_brotli_filter_process ’:
../ngx_brotli/src/ngx_http_brotli_filter_module.c:534:5: error: ‘ BrotliEncoderWriteData ’ is deprecated (declared at /usr/local/include/brotli/encode.h:109) [-Werror=deprecated-declarations]
if (!BrotliEncoderWriteData(ctx->encoder, ctx->last, ctx->flush, &size,
^
cc1: all warnings being treated as errors
make[1]: *** [objs/addon/src/ngx_http_brotli_filter_module.o] Error 1
make[1]: Leaving directory `/root/nginx-1.11.5'
make: *** [build] Error 2
qgy18
2016-11-24 17:05:43 +08:00
加上 --with-cc-opt=-Wno-deprecated-declarations 试试呢?

@chinaiy
wql
2016-11-24 22:00:37 +08:00
@chinaiy 上周更新过文章了,建议增加编译选项 --with-cc-opt=-Wno-deprecated-declarations
chinaiy
2016-11-25 14:01:12 +08:00
@qgy18 已经装好,谢谢,不过还有个 http2 在 360 浏览器上不能访问的问题~
chinaiy
2016-11-25 14:01:39 +08:00
@wql 嗯,增加了这个选项就可以了,谢谢。
chinaiy
2016-12-12 12:22:46 +08:00
@wql 部署了一台阿里云新机器,编译 nginx 的时候报错,之前都成功,现在不知道是什么缘故, 求助~

./configure: error: Brotli library is missing from the ../ngx_brotli/deps/brotli directory.

Please make sure that the git submodule has been checked out:

cd ../ngx_brotli && git submodule update --init && cd /root/nginx-1.11.5
wql
2016-12-12 22:20:26 +08:00
@chinaiy 你按照他给的操作一遍就好了嘛……

cd ../ngx_brotli && git submodule update --init && cd /root/nginx-1.11.5
chinaiy
2016-12-13 08:42:39 +08:00
@wql 嗯 ,已经好了,谢谢。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/301779

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX