除Gzip外,还能够使用Brotli、Zstd这两种更高性能的算法,这两种算法分别在静态资源和动态接口多的网站发挥效用,一般可以提升至少15%的性能,并且可以在nginx中同时开启,根据用户的浏览器版本自行兼容合适的算法
但是这两种算法默认无法直接配置,我们可以下载别人编译好的nginx或者就像我这篇文章一样,编译动态模块
注意:
- 使用Brotli和Zstd都应该开启https
- 每次nginx大更新都需要再次编译.so模块
解决第二点可以通过CI/CD来自动化,或者直接使用容器版nginx(省得更新),今后我有机会我会做这个自动化
值得一提的是我刚才发现阿里云的ESA在域名管理面板的“速度和网络”中可以一键开启这三项算法,我打开网站确实是变快了很多

本次使用演示网站github项目:https://github.com/whoworng/netdisk
编译环节
配置编译动态模块所需要的Dockerfile
使用多阶段构建,最后使用编译好的二进制,容器更小
在nginx/Dockerfile配置
# --- 阶段一:编译环境 (Builder) ---
FROM nginx:1.26.1-alpine AS builder
# 1. 替换为国内镜像源 (清华源)
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
# 2. 安装编译所需的依赖
RUN apk add --no-cache \
gcc \
libc-dev \
make \
openssl-dev \
pcre2-dev \
zlib-dev \
linux-headers \
libtool \
automake \
autoconf \
git \
g++ \
cmake \
zstd-dev \
brotli-dev
# 3. 更换更稳定的代理地址,并设置 Git 缓冲区
RUN git config --global url."https://ghfast.top/https://github.com/".insteadOf "https://github.com/" && \
git config --global http.postBuffer 524288000
# 4. 下载当前镜像对应版本的 Nginx 源码
RUN NGINX_VERSION=$(nginx -v 2>&1 | cut -d '/' -f 2) && \
wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz && \
tar zxvf nginx-${NGINX_VERSION}.tar.gz
# 5. 克隆模块源码
RUN git clone --depth 1 --recursive https://github.com/google/ngx_brotli.git
RUN git clone --depth 1 --recursive https://github.com/tokers/zstd-nginx-module.git
# 6. 编译动态模块
# 使用 eval 确保 CONFARGS 中的引号被正确解析
WORKDIR /nginx-1.26.1
RUN CONFARGS=$(nginx -V 2>&1 | grep "configure arguments:" | sed 's/configure arguments: //') && \
eval "./configure $CONFARGS --add-dynamic-module=/ngx_brotli --add-dynamic-module=/zstd-nginx-module" && \
make modules
# --- 阶段二:运行环境 (Runtime) ---
FROM nginx:1.26.1-alpine
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
RUN apk add --no-cache zstd-libs brotli-libs
COPY --from=builder /nginx-1.26.1/objs/ngx_http_brotli_filter_module.so /usr/lib/nginx/modules/
COPY --from=builder /nginx-1.26.1/objs/ngx_http_brotli_static_module.so /usr/lib/nginx/modules/
COPY --from=builder /nginx-1.26.1/objs/ngx_http_zstd_filter_module.so /usr/lib/nginx/modules/
COPY --from=builder /nginx-1.26.1/objs/ngx_http_zstd_static_module.so /usr/lib/nginx/modules/
COPY conf/nginx.conf /etc/nginx/nginx.conf
COPY html /usr/share/nginx/html
配置nginx.conf
加载.so动态模块
# 加载动态模块 (必须放在最顶部)
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
load_module modules/ngx_http_zstd_filter_module.so;
load_module modules/ngx_http_zstd_static_module.so;
压缩算法配置,每个都配置了各自的甜点level等级
# 1. Gzip 配置 (对照组)
gzip on;
gzip_comp_level 6;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml image/svg+xml;
# 2. Brotli 配置
brotli on;
brotli_comp_level 6;
brotli_static on;
brotli_min_length 256;
brotli_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml image/svg+xml;
# 3. Zstd 配置
zstd on;
zstd_comp_level 3;
zstd_static on;
zstd_min_length 256;
zstd_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml image/svg+xml;
PS:由于我用Nginx反向代理了后端,而Gzip算法默认不压缩来自代理服务器的请求,所以需要开启 gzip_proxied any ,Brotli和Zstd算法都默认支持代理压缩
修改docker-compose.yml
将image: nginx:1.26.1-apline改为从本地构建
build:
context:
./nginx
然后就可以docker compose up -d –build了,我ubuntu24.04 4核8GB,构建时间没有超过一分钟,如果时间太长,可以更换Dockerfile中的镜像源和git clone代理

测试压缩算法环节
同时开启算法,优先级一般是zstd > brotli > gzip,比较先进、正常的浏览器一般都支持zstd
curl -I -H "Accept-Encoding: gzip,br,zstd" http://localhost:8080/static/css/style.css

我在我的后端的static目录制造了一个722KB的文本文件,以支持算法测试
使用命令
for i in {1..10000}; do echo "Compression test line $i: some repetitive text to make it compressible."; done > app/static/test.txt

本地测试
压缩量对比
本地测试使用curl,不需要https也能测

下面是数据对比:
| 压缩算法 | 下载字节大小 | 压缩比例 |
| 不压缩 | 7,288,895 | 100% |
| gzip | 269,276 | 3.69% |
| br | 110,187 | 1.51% |
| zstd | 106,305 | 1.45% |
zstd和br算法下载量只有nginx常用的gzip算法的大约40%,比起不压缩的更是不到2%
编译压测工具wrk2
# 1. 安装编译依赖
sudo apt update && sudo apt install -y build-essential libssl-dev git zlib1g-dev
# 2. 克隆 wrk2 源码 (如果 GitHub 慢,可以使用之前配置的镜像代理)
git clone --depth 1 https://ghfast.top/https://github.com/giltene/wrk2.git
# 3. 编译
cd wrk2
make
# 4. 安装到系统路径
sudo cp wrk /usr/local/bin/wrk2
开始压测
wrk2 -t4 -c100 -d20s -R100 -L -H "Accept-Encoding: 算法名" http://localhost:8080/static/test.txt
使用4个线程进行压测
保持100个HTTP并发链接
压测持续20秒
设置吞吐量为100个请求/秒(100RPS)
-L打印详细的延迟统计图
压测结果图






压测对比表
| 压缩算法 (Header) | 平均延迟 (Avg) | 中位数延迟 (P50) | 90% 延迟 (P90) | 99% 延迟 (P99) | 最大延迟 (Max) | 吞吐量 (Transfer/s) | 实际请求数 (Total) |
| Zstd | 14.71 ms | 8.97 ms | 37.98 ms | 55.10 ms | 69.57 ms | 1.15 MB/s | 2004 |
| Gzip | 59.83 ms | 44.77 ms | 123.26 ms | 222.21 ms | 251.78 ms | 2.59 MB/s | 1979 |
| Brotli | 181.30 ms | 152.96 ms | 355.58 ms | 409.09 ms | 450.56 ms | 1.31 MB/s | 1954 |
可以看到zstd算法十分优秀,半数的请求在9毫秒内就收到了,即使是最倒霉的排在末尾的百分之一的用户,也只在第55毫秒就收到了
公网服务器测试
后续将加入
根据这次的对比,我将为我的主要网站配置zstd压缩算法