之前在 Nginx-Quic 分支被合并到了 Nginx 主线的时候写过一篇使用 BoringSSL 编译 Nginx 并开启 Quic 或 HTTP/3 的文章,详见 Nginx 编译开启 Quic 或 HTTP/3,随着 Nginx 主线在 2025 年 7 月底(Nginx 1.29.0+)的一个提交中引入了对 OpenSSL QUIC 的适配支持(仅支持OpenSSL 3.5.1 及以上版本),本文将为您介绍如何使用 OpenSSL 编译 Nginx 并开启 Quic 或 HTTP/3。
OpenSSL 3.5.1 默认仅在较新的 Linux 发行版中可用,例如 Debian 13,本文以 Debian 13 为例进行演示。如果您使用的是较旧的发行版,可能需要手动编译 OpenSSL 3.5.1 或升级您的操作系统。
安装依赖
1 2 3
| apt update apt install build-essential ca-certificates zlib1g-dev libpcre2-dev tar unzip libssl-dev wget curl git cmake ninja-build libunwind-dev pkg-config libxml2-dev libxslt1-dev libgd-dev libgeoip-dev libperl-dev
|
安装 brotli 压缩
不需要请跳过,并在编译时删除–add-module=../ngx_brotli
1 2 3 4 5 6
| git clone --recurse-submodules -j8 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 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_CXX_FLAGS="-Ofast -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_INSTALL_PREFIX=./installed .. cmake --build . --config Release --target brotlienc cd ../../../..
|
编译安装quic
注意:
本人是直接在 /root 目录下编译的,如果你在其他目录下,请自行修改路径;
如果你不需要 brotli 压缩,请删除–add-module=../ngx_brotli
本人将 Nginx 安装在 /www/server/nginx 目录下,如果你需要修改,请自行修改路径;
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
| git clone https://github.com/nginx/nginx.git cd nginx ./auto/configure \ --prefix=/www/server/nginx \ --user=www-data \ --group=www-data \ --with-debug \ --with-http_v3_module \ --with-cc-opt='-g -O2 -Werror=implicit-function-declaration -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -fcf-protection -Wdate-time -D_FORTIFY_SOURCE=2' \ --with-ld-opt='-Wl,-z,relro -Wl,-z,now' \ --add-module=../ngx_brotli \ --with-compat \ --with-pcre-jit \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_realip_module \ --with-http_auth_request_module \ --with-http_v2_module \ --with-http_dav_module \ --with-http_slice_module \ --with-threads \ --with-http_addition_module \ --with-http_flv_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_mp4_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_sub_module \ --with-mail \ --with-mail_ssl_module \ --with-stream \ --with-stream_ssl_module \ --with-stream_ssl_preread_module \ --with-stream_realip_module make make install
|
添加 www 用户
大部分系统下默认存在着www-data用户组和www-data用户,如果没有请执行以下命令添加。
1 2
| groupadd www-data useradd -g www-data -s /sbin/nologin www-data
|
添加进程管理
本人使用的是 systemd,如果你使用的是其他进程管理,请自行修改
1
| vim /usr/lib/systemd/system/nginx.service
|
输入如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13
| [Unit] Description=nginx After=network.target
[Service] Type=forking ExecStart=/www/server/nginx/sbin/nginx ExecReload=/www/server/nginx/sbin/nginx -s reload ExecStop=/www/server/nginx/sbin/nginx -s quit PrivateTmp=true
[Install] WantedBy=multi-user.target
|
启动
开机自启
配置文件
示例配置文件如下,更多特性请参考官方文档:https://nginx.org/en/docs/http/ngx_http_v3_module.html
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
| server { listen 443 ssl; listen [::]:443 ssl;
listen 443 quic reuseport; listen [::]:443 quic reuseport;
http3 on;
http2 on;
server_name r2wind.cn;
add_header Alt-Svc 'h3=":443"; ma=86400'; add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
location / { root /www/wwwroot/r2wind.cn; index index.html index.htm; }
ssl_certificate /root/.acme.sh/smb.wiki/fullchain.cer; ssl_certificate_key /root/.acme.sh/smb.wiki/smb.wiki.key; ssl_session_timeout 5m; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; }
|
配置完成后,重载 Nginx 即可生效