前期准备
- docker-compose
- AdminLTE
- https证书
- 一个域名
- 源站服务器
只有以 https协议访问网站时,才会有Accept-Encoding: gzip, deflate, br
,所以上述准备缺一不可.
我使用了AdminLTE 作为页面测试.
服务器配置
1
2
3
4
5
6
7
8
9
| 实例规格: ecs.sn1ne.xlarge
实例规格族: 计算网络增强型
镜像ID: ubuntu_16_0402_64_20G_al...
CPU: 4核
内存: 8 GB
实例类型: I/O优化
操作系统: Ubuntu 16.04 64位
带宽计费方式: 按固定带宽
当前使用带宽: 75Mbps
|
客户端
企业电信300M光纤
docker-compose.yaml
1
2
3
4
5
6
7
8
9
10
| nginx-brotli:
container_name: nginx-brotli
image: 'fholzer/nginx-brotli:latest'
ports:
- "8080:80"
restart: always
volumes:
- '/fordocker/nginx.conf:/etc/nginx/nginx.conf:ro'
- '/fordocker/AdminLTE-3.0.0-alpha.2/:/usr/share/nginx/html:ro'
|
nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
root /etc/nginx/html;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
include /etc/nginx/conf.d/*.conf;
brotli on;
brotli_static always;
brotli_comp_level 6;
brotli_types *;
}
|
1
2
3
4
5
6
7
| ls /usr/share/nginx/html
cd /fordocker
nano nginx.conf
docker-compose exec nginx-brotli ls /usr/share/nginx/html
docker-compose logs nginx-brotli
docker-compose restart
netstat -nltp | grep 80
|
1
2
3
4
5
6
| export url=''
echo $url
echo '待测url:'$url
curl -vo /dev/null $url -H "Accept-Encoding: br" -w "cost time: %{time_total}\n"
curl -vo /dev/null $url -H "Accept-Encoding: gzip" -w "cost time: %{time_total}\n"
curl -vo /dev/null $url -w "cost time: %{time_total}\n"
|
对照结果
测试的时候要给 nginx 一个预热的时间,先开 disable chache, 然后刷新十次左右.之后多次测量求平均值
gzip on
1
2
3
4
5
6
7
8
9
10
| 1330
1240
1200
829
1580
813
929
996
899
907
|
- 加载时间平均值:1072.3
- 页面总大小1.4MB
br 6
1
2
3
4
| brotli on;
brotli_static always;
brotli_comp_level 6;
brotli_types *;
|
1
2
3
4
5
6
7
8
9
10
| 857
1200
855
854
865
1220
854
983
1280
1210
|
- 加载时间平均值:1017.8
- 页面总大小655kb
br 7
1
2
3
4
| brotli on;
brotli_static always;
brotli_comp_level 7;
brotli_types *;
|
1
2
3
4
5
6
7
8
9
10
| 854
836
964
868
931
829
877
892
1370
933
|
br 8
1
2
3
4
| brotli on;
brotli_static always;
brotli_comp_level 8;
brotli_types *;
|
1
2
3
4
5
6
7
8
9
10
| 779
906
893
958
831
827
900
830
831
866
|
br 10
1
2
3
4
| brotli on;
brotli_static always;
brotli_comp_level 10;
brotli_types *;
|
1
2
3
4
5
6
7
8
9
10
| 1660
1720
1750
1640
1750
1640
1700
1710
1680
1730
|
结论
content-encoding |
加载时间平均值(ms)平均值 |
页面总大小 |
gzip |
1072.3 |
1.4MB |
br-6 |
1017.8 |
655kb |
br-7 |
935.4 |
653kb |
br-8 |
862.1 |
652kb |
br-10 |
1698 |
633kb |
用平均值去计算 br 编码其实不公平的,在实验中,我发现有些文件是干扰变量,用的 gzip 编码.那么再加上这个考量,结合服务器的压缩时间,我觉得 brotli_comp_level
设置 6 ~ 8 会比较合适.
不得不说,谷歌爸爸就是叼
- Nginx 容器教程
- fholzer/docker-nginx-brotli
- https://hub.docker.com/r/fholzer/nginx-brotli/tags/
- google/ngx_brotli
- nginx
- 启用 Brotli 压缩算法,对比 Gzip 压缩 CDN 流量再减少 20%
- 又拍云 CDN 支持了 Brotli 了!
- Brotli and Static Compression
- Static site implosion with Brotli and Gzip
- Brotli Accept-Encoding/Content-Encoding
- Static site implosion with Brotli and Gzip