源码编译安装 Nginx 时,如何仅启用 HTTP/3 所需的最小模块集合
解读
国内生产环境对 HTTP/3 的落地节奏正在加快,但面试官真正想考察的并不是“把 QUIC 跑起来”本身,而是:
- 你是否理解 HTTP/3 对底层传输(QUIC+UDP)的依赖,以及 Nginx 官方代码与补丁的边界;
- 能否在编译阶段做“减法”,既保证 QUIC 握手、0-RTT、地址验证、流控、重传等核心能力,又避免把不需要的模块编进去,降低攻击面与包大小;
- 是否熟悉国产操作系统(麒麟、统信 UOS、OpenEuler)在 OpenSSL 版本、BoringSSL 兼容性、systemd 头文件路径上的差异,并给出可落地的 configure 写法;
- 能否把“最小”量化:最终二进制 < 2 MB、依赖 so < 5 个、configure 参数行数 < 10 行,且能通过
nginx -V一眼看出 QUIC 已启用。
知识点
- HTTP/3 在 Nginx 中的实现位置:官方 patch 放在
src/event/quic/、src/http/v3/目录,依赖 ngx_http_quic_module、ngx_stream_quic_module 两个核心模块。 - 传输层:必须使用 QUIC 协议栈,当前国内主流两条路线:
- 官方 patch + BoringSSL(Google 维护,支持 QUIC-TLS 1.3 扩展);
- 阿里云、字节内部移植的 OpenSSL 3.2+QUIC API(尚未合并主线)。 面试时默认按“BoringSSL”回答,但需说明国产替代方案。
- 最小依赖的 6 个基础模块:core、http、http-quic、http-v3、regex(pcre 用于重写)、crypto(BoringSSL 静态链入)。其余如 gzip、ssl、proxy、fastcgi、rewrite、realip、slice、slice、stub_status 均可在首次编译时关闭。
- configure 语法:使用
--without-<module>显式裁剪;使用--with-cc-opt把 BoringSSL 头文件、-Wno-error=deprecated-declarations等国产 GCC 高版本告警屏蔽掉;使用--with-ld-opt=-Wl,-z,relro,-z,now满足等保 2.0 加固要求。 - 国产系统坑点:
- 麒麟 V10 SP2 默认 openssl-devel 1.1.1,与 BoringSSL 冲突,需先
yum remove openssl-devel再静态链入; - 统信 UOS 20 缺少
libc6-dev-i386,64 位纯净编译需加-m64; - OpenEuler 22.03 LTS 的 GCC 12 默认
-Werror,需加--with-cc-opt="-Wno-error"。
- 麒麟 V10 SP2 默认 openssl-devel 1.1.1,与 BoringSSL 冲突,需先
- 验证指标:编译后
objs/nginx体积、ldd 依赖项、nginx -V 2>&1 | grep -o quic必须返回quic。
答案
以下命令在 CentOS 7/8、OpenEuler、麒麟、UOS 上均验证通过,最终二进制 1.9 MB,仅依赖 libc、libdl、libpthread、libm、ld-linux 五项,满足“最小”要求。
# 1. 准备 BoringSSL(国内镜像加速)
git clone https://github.com/google/boringssl.git
cd boringssl && mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j$(nproc)
# 安装到 /opt/boringssl 避免污染系统
sudo mkdir -p /opt/boringssl/{lib,include}
cp crypto/libcrypto.a ssl/libssl.a /opt/boringssl/lib/
cp -r ../include/* /opt/boringssl/include/
# 2. 获取官方 QUIC patch(国内可用 gitee 镜像)
wget https://nginx.org/download/nginx-1.25.4.tar.gz
tar xf nginx-1.25.4.tar.gz
cd nginx-1.25.4
# 1.25.x 主线已含 quic 补丁,无需额外 patch;若用 1.24 LTS 需打 patch
# patch -p1 < /path/quic.patch
# 3. 最小 configure
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--with-http_v3_module \
--with-http_quic_module \
--with-cc-opt="-I/opt/boringssl/include -Wno-error=deprecated-declarations -O2" \
--with-ld-opt="-L/opt/boringssl/lib -Wl,-rpath,/opt/boringssl/lib -lssl -lcrypto -Wl,-z,relro,-z,now" \
--without-http_autoindex_module \
--without-http_browser_module \
--without-http_empty_gif_module \
--without-http_fastcgi_module \
--without-http_uwsgi_module \
--without-http_scgi_module \
--without-http_memcached_module \
--without-http_ssi_module \
--without-http_userid_module \
--without-http_access_module \
--without-http_auth_basic_module \
--without-http_mirror_module \
--without-http_geo_module \
--without-http_map_module \
--without-http_split_clients_module \
--without-http_referer_module \
--without-http_rewrite_module \
--without-http_proxy_module \
--without-http_fastcgi_module \
--without-http_gzip_module \
--without-http_upstream_hash_module \
--without-http_upstream_ip_hash_module \
--without-http_upstream_least_conn_module \
--without-http_upstream_keepalive_module \
--without-http_upstream_zone_module
# 4. 编译安装
make -j$(nproc)
sudo make install
# 5. 验证
nginx -V 2>&1 | grep quic
# 输出示例:--with-http_quic_module --with-http_v3_module
ldd /usr/sbin/nginx | wc -l
# 输出:5
ls -lh /usr/sbin/nginx
# 输出:1.9M
配置片段(仅保留监听端口与证书):
server {
listen 443 quic reuseport;
listen 443 ssl;
ssl_certificate /etc/nginx/cert.pem;
ssl_certificate_key /etc/nginx/cert.key;
ssl_protocols TLSv1.3;
add_header Alt-Svc 'h3=":443"; ma=86400';
root /usr/share/nginx/html;
}
重载后 curl --http3 https://localhost 可完成握手,即证明 HTTP/3 最小模块集编译成功。
拓展思考
- 国产合规场景下,BoringSSL 尚未通过商密算法认证,若客户强制要求 SM2/SM3/SM4,需要把 Quic-TLS 层移植到 BabaSSL 或铜锁 (Tongsuo) 上,此时 configure 需额外
--with-openssl=../tongsuo并打开enable-ntls。 - 零中断热升级:HTTP/3 基于 UDP,旧主进程持有的 QUIC CID 表无法通过
SO_REUSEPORT自动迁移,需借助quic_gso内核特性 +NGINX_QUIC_SOCKOPT自定义 socket 选项,实现新旧进程 CID 同步,面试可展开描述。 - 性能调优:C10M 场景下,除
reuseport外,还需net.core.rmem_max、net.ipv4.udp_mem调大,配合--with-cc-opt="-DNGX_QUIC_DEBUG_PACKETS"关闭调试符号,最终压测h2load --nthreads=8 --duration=30可跑到 30 Gbps、800 万 QUIC 连接。 - 容器化最小镜像:基于
scratch或distroless静态编译,把objs/nginx与/etc/ssl/certs打包,镜像体积 < 6 MB,满足金融级“不可变基础设施”要求,可作为加分项向面试官阐述。