Nginx Brotli压缩算法

Brotli 是基于LZ77算法的一个现代变体、霍夫曼编码。 Google软件工程师 在2015年9月发布了包含通用无损数据压缩的Brotli增强版本,特别侧重于HTTP压缩。 注意:使用算法的前提是启用了 https,因为 http 请求中 request header 里的 Accept-Encoding: gzip, deflate 是没有 br 的。

1、获取brotli模块

cd /root
export https_proxy=http://relay-actting.iftop.top:11969
git clone https://github.com/google/ngx_brotli.git
cd ngx_brotli
git submodule update --init
unset https_proxy

*nginx  自1.9.11以后版本后支持动态模块,自此,给nginx添加模块再也不用重新编译nginx了,通过动态模块,你可以在运行时有有选择性的加载第三方或Nginx官方模块。

2、获取nginx

wget https://nginx.org/download/nginx-1.27.1.tar.gz

3、动态安装brotli模块

tar zvxf nginx-1.27.1.tar.gz
cd nginx-1.27.1
./configure --with-compat --add-dynamic-module=../ngx_brotli
make modules
cp objs/ngx_http_brotli_filter_module.so objs/ngx_http_brotli_static_module.so /opt/nginx/modules/

4、add the load_module directives to nginx.conf (above the http block):

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

5、启用brotli压缩(http {…}):

    gzip off;
    brotli on;
    brotli_static on;
    brotli_comp_level 11;    (0-11)
    brotli_min_length 20;
    brotli_buffers 16 8k;
    brotli_types *;

6、变量$brotli_ratio可打印到access_log中

实现的压缩率,以原始响应大小与压缩后响应大小之比计算。

*nginx: [warn] the “listen … http2” directive is deprecated, use the “http2” directive instead

把listen 8080 ssl http2;

调整为:

listen 8080 ssl;

http2 on;

Categories: 系统运维