nginx 开启 brotli 压缩,并解决反向代理异常

Nginx 启用 Brotli 压缩算法

从github上拉取https://github.com/google/ngx_brotli,如果拉取失败选择这个,从国内拉取https://gitee.com/abool/ngx_brotli

cd /usr/local/data/soft/ 
git clone https://github.com/google/ngx_brotli 
或者 git clone https://gitee.com/abool/ngx_brotli
cd ngx_brotli && git submodule update --init

重新编译 nginx

#进到nginx源码目录下
cd nginx-1.14.2
#预编译
./configure --prefix=/usr/local/data/nginx-1.14.2 \
--user=www --group=www --with-http_ssl_module \
--with-http_stub_status_module --with-http_gzip_static_module \
--with-pcre --with-http_v2_module --with-http_secure_link_module \
--with-stream --with-openssl-opt=enable-tlsext --with-http_flv_module \
--add-module=/usr/local/data/soft/ngx_brotli
#编译
make #我这里没有make install
cp /usr/local/data/nginx-1.14.2/sbin/nginx /usr/local/data/nginx-1.14.2/sbin/nginx-old
#kill掉nginx
ps axu|grep nginx|awk '{print $2}'|xargs kill -9
#替换nginx
cp objs/nginx /usr/local/data/nginx-1.14.2/sbin/nginx 
#启动nginx
/usr/local/data/nginx-1.14.2/sbin/nginx 

nginx.conf 配置文件中新增 brotli 配置

        brotli on;
        brotli_static on;
        brotli_comp_level 6;
        brotli_buffers 16 128k;
        brotli_min_length 20;
        brotli_types text/css application/x-javascript application/xml application/x-httpd-php text/javascript application/javascript text/xml application/json image/jpeg image/png image/jpg;
添加brotli相关配置

重新加载配置文件

/usr/local/data/nginx-1.14.2/sbin/nginx -t
nginx: the configuration file /usr/local/data/nginx-1.14.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/data/nginx-1.14.2/conf/nginx.conf test is successful
#reload加载
/usr/local/data/nginx-1.14.2/sbin/nginx -s reload

访问时网站打不开

网站挂掉了

查阅资料

反向代理添加proxy_set_header Accept-Encoding配置

proxy_set_header Accept-Encoding “”;

location / {
                proxy_set_header Accept-Encoding "";
                proxy_pass http://127.0.0.1:3000;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

重新reload nginx服务

再次网站访问恢复正常

打开浏览器查看br已经生效

Comments

No comments yet. Why don’t you start the discussion?

发表评论