宝塔 ngnix安装brotli 模块

1,备份 Nginx 二进制文件

mv /www/server/nginx/sbin/nginx /www/server/nginx/sbin/nginx.bak

2,备份 Nginx 配置文件

cp -r /www/server/nginx/conf /www/server/nginx/conf.bak

3, 下载和解压必要的源码,Nginx、PCRE 和 OpenSSL 的源码

wget http://nginx.org/download/nginx-1.18.0.tar.gz

tar -zxvf nginx-1.18.0.tar.gz

cd nginx-1.18.0

wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz

tar -zxvf pcre-8.43.tar.gz

wget https://www.openssl.org/source/old/1.1.1/openssl-1.1.1g.tar.gz

tar -zxvf openssl-1.1.1g.tar.gz

4,下载并编译 ngx_brotli 模块

cd /www/server

git clone –recurse-submodules -j8 https://github.com/google/ngx_brotli.git

5,在 Nginx 源码目录中,运行 ./configure,如下全部参数

./configure \

–user=www \

–group=www \

–prefix=/www/server/nginx \

–add-module=/www/server/nginx/src/ngx_devel_kit \

–add-module=/www/server/nginx/src/lua_nginx_module \

–add-module=/www/server/nginx/src/ngx_cache_purge \

–add-module=/www/server/nginx/src/nginx-sticky-module \

–with-openssl=/www/server/nginx-1.18.0/openssl-1.1.1g \

–with-pcre=/www/server/nginx-1.18.0/pcre-8.43 \

–with-http_v2_module \

–with-stream \

–with-stream_ssl_module \

–with-stream_ssl_preread_module \

–with-http_stub_status_module \

–with-http_ssl_module \

–with-http_image_filter_module \

–with-http_gzip_static_module \

–with-http_gunzip_module \

–with-ipv6 \

–with-http_sub_module \

–with-http_flv_module \

–with-http_addition_module \

–with-http_realip_module \

–with-http_mp4_module \

–with-ld-opt=-Wl,-E \

–with-cc-opt=-Wno-error \

–with-ld-opt=-ljemalloc \

–with-http_dav_module \

–add-module=/www/server/nginx/src/nginx-dav-ext-module \

–add-module=/www/server/ngx_brotli

6,编译和安装 Nginx

make(很长时间)

make install

7,替换旧的 Nginx 二进制文件

cp objs/nginx /www/server/nginx/sbin/

8,在Nginx中,配置 Brotli 压缩

brotli on;

brotli_comp_level 6;

brotli_static on;

brotli_types text/plain text/css application/javascript application/json application/xml+rss image/svg+xml application/font-woff2;

9,重启 Nginx

/www/server/nginx/sbin/nginx -s stop

/www/server/nginx/sbin/nginx

10,验证Brotli 模块

/www/server/nginx/sbin/nginx -V

如果看到 –add-module=/www/server/ngx_brotli,则表示模块已成功加载

在浏览器中访问你的网站,通过开发者工具查看响应头,确认 content-encoding 是否包含 br。如果包含,则表示 Brotli 压缩已成功启用。

curl -I -H “Accept-Encoding: br” https://www.cchatty.com/index.html

11,如果出现问题

1)停止当前的 Nginx 服务

/www/server/nginx/sbin/nginx -s stop

2)恢复旧的 Nginx 二进制文件

mv /www/server/nginx/sbin/nginx.bak /www/server/nginx/sbin/nginx

3)重新启动 Nginx

/www/server/nginx/sbin/nginx

Leave a Reply