项目使用Brotli压缩算法来减小传输数据的大小。要启用Brotli压缩算法,确定是否支持broti模块:
nginx -V 2>&1 | grep -o with-http_brotli_module
如果输出中包含了 "with-http_brotli_module",则表示您的Nginx版本支持Brotli模块。
没有则需要安装;
安装libbrotli
cd /www/server
git clone https://github.com/bagder/libbrotli
cd libbrotli
./autogen.sh
./configure
make && make install
下载ngx_brotli模块及其依赖:
cd /www/server
git clone https://github.com/google/ngx_brotli
cd ngx_brotli && git submodule update --init
可能 git submodule update下载子模块时报 Permission denied 错误
我们把 git 形式的 url 改为 https 形式。
vim .gitmodules
url = git://github.com/google/brotli.git
保存后执行如下命令,重新同步一下子模块信息。
git submodule sync
再次执行git submodule update --init就可以了;
在nginx编译时候加上--add-module=/usr/local/src/ngx_brotli编译出来的nginx就可以。
在/etc/nginx/nginx.conf加入:
\#Brotli Compression
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;
location / {
}
location ~ .+\.unityweb$ {
add_header Content-Encoding br;
add_header Content-Type application/octet-stream;
}
location ~* .+wasm\.br$ {
add_header Content-Encoding br;
default_type application/wasm;
}
location ~* \.br$ {
add_header Content-Encoding br;
}
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
标签:git,application,压缩,brotli,nginx,br,fastcgi
From: https://blog.51cto.com/xpu2001/9028416