Rootop 服务器运维与web架构

nginx配置br压缩

# br模块仓库 https://github.com/google/ngx_brotli

注意br模块只能在https中用。http协议是不支持的。

当浏览器发送请求时,会在请求头中携带支持的压缩算法。
比如chrome访问http网站时,请求头中为Accept-Encoding: gzip, deflate
访问https网站时,请求头中为Accept-Encoding: gzip, deflate, br, zstd

nginx可以同时配置gzip和br压缩算法,如果浏览器的请求头中含有br,则优先于gzip。

# 错误:CMake 3.15 or higher is required. You are running version 3.6.0
# 安装新版本cmake,否则可能会提示上面CMake版本过低问题。

[root@web02 ~]# cd /usr/local/
[root@web02 local]# wget -c https://cmake.org/files/v3.30/cmake-3.30.0-linux-x86_64.tar.gz
[root@web02 local]# tar zxvf cmake-3.30.0-linux-x86_64.tar.gz 

[root@web02 local]# echo 'export PATH=$PATH:/usr/local/cmake-3.30.0-linux-x86_64/bin' >> /etc/profile
[root@web02 ~]# source /etc/profile

# 构建依赖
[root@web02 ~]# cat br
git clone --recurse-submodules https://github.com/google/ngx_brotli
cd ngx_brotli/deps/brotli
mkdir out && cd out
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_CXX_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_INSTALL_PREFIX=./installed ..
cmake --build . --config Release --target brotlienc

[root@web02 ~]# bash br

# nginx添加br的编译参数
./configure --add-module=../ngx_brotli


# nginx虚拟主机配置
server
{

	listen       16666 ssl;
	http2 on;
	server_name  localhost;

	ssl_certificate /home/software/openresty/nginx/conf/vhost/fullchain5.pem;
	ssl_certificate_key /home/software/openresty/nginx/conf/vhost/privkey5.pem;
	
	ssl_session_timeout 5m;
	ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
	ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
	ssl_prefer_server_ciphers on;


	index index.html;
	root /home/software/openresty/nginx/html;

	#启用brotli压缩
	brotli on;
	brotli_comp_level 6;
	brotli_buffers 16 8k;
	brotli_min_length 20;
	# 注意要配置压缩的文件类型(content-type)
	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 application/wasm application/octet-stream;  


	# 反向代理用法
	#location /
	#{
		#proxy_pass http://192.168.12.196:8888;
		#proxy_set_header Accept-Encoding "";

		#启用brotli压缩
		#brotli on;
		#brotli_comp_level 6;
		#brotli_buffers 16 8k;
		#brotli_min_length 20;
		# 注意要配置压缩的文件类型(content-type)
		#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 application/wasm application/octet-stream;  
	#}
}

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/5474.html

作者:Venus

服务器运维与性能优化

评论已关闭。