V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
wql
V2EX  ›  NGINX

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

  •  
  •   wql · 2016-08-25 20:41:18 +08:00 via Android · 7205 次点击
    这是一个创建于 2804 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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
    第 1 条附言  ·  2016-08-25 21:17:10 +08:00
    标题有误,应当为压缩密度最高,怪我没仔细看官方文档。
    第 2 条附言  ·  2016-08-25 21:21:39 +08:00
    这篇文章对于相关算法解释最清晰易懂:
    https://blog.cloudflare.com/results-experimenting-brotli/
    17 条回复    2016-12-13 08:42:39 +08:00
    shuax
        1
    shuax  
       2016-08-25 20:51:43 +08:00
    怎么能提高性能,应该是节约流量吧。
    wql
        2
    wql  
    OP
       2016-08-25 20:55:50 +08:00 via Android
    @shuax 参考了谷歌官方的描述:It is similar in speed with deflate but offers more dense compression.
    的确是在保持了速度的同时节约流量。
    shyling
        3
    shyling  
       2016-08-25 21:07:40 +08:00
    话说这样设置的话在浏览器没有发 accept-encoding:br 时也会用 brotli 压缩么
    ivmm
        4
    ivmm  
       2016-08-25 21:13:22 +08:00
    提高性能的原理是什么?
    knightdf
        5
    knightdf  
       2016-08-25 21:16:20 +08:00
    既然是要提高性能,那我不压缩不是性能最高?
    lhbc
        6
    lhbc  
       2016-08-25 21:16:49 +08:00
    能和 gzip 共存吗?
    如果不支持共存,不支持 brotli 的浏览器是不是不能使用任何压缩?
    wql
        7
    wql  
    OP
       2016-08-25 21:17:47 +08:00 via Android
    @shyling 不行,识别就靠这个

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

    这个算法主要是内置了分析大量网页之后提取的字典,所以能在压缩比更高的同时不影响解压速度。
    chinaiy
        10
    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
        11
    qgy18  
       2016-11-24 17:05:43 +08:00 via iPhone
    加上 --with-cc-opt=-Wno-deprecated-declarations 试试呢?

    @chinaiy
    wql
        12
    wql  
    OP
       2016-11-24 22:00:37 +08:00 via Android
    @chinaiy 上周更新过文章了,建议增加编译选项 --with-cc-opt=-Wno-deprecated-declarations
    chinaiy
        13
    chinaiy  
       2016-11-25 14:01:12 +08:00
    @qgy18 已经装好,谢谢,不过还有个 http2 在 360 浏览器上不能访问的问题~
    chinaiy
        14
    chinaiy  
       2016-11-25 14:01:39 +08:00
    @wql 嗯,增加了这个选项就可以了,谢谢。
    chinaiy
        15
    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
        16
    wql  
    OP
       2016-12-12 22:20:26 +08:00 via Android
    @chinaiy 你按照他给的操作一遍就好了嘛……

    cd ../ngx_brotli && git submodule update --init && cd /root/nginx-1.11.5
    chinaiy
        17
    chinaiy  
       2016-12-13 08:42:39 +08:00
    @wql 嗯 ,已经好了,谢谢。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2610 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 11:21 · PVG 19:21 · LAX 04:21 · JFK 07:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.