前言

HTTP/3.0 出来已经能有一段时间了,而且功能相对都已经比较完善了。顾名思义,HTTP/3.0 就是 HTTP/2.0 的下一代版本,由谷歌发出。目前主流仍是 HTTP/2.0 ,而且目前支持 HTTP/3.0 的 Client 几乎没有,只有谷歌浏览器金丝雀 Chrome Canary 和火狐浏览器 Firefox Nightly 及Curl 7.66 以后的版本(默认不开启,需要自行编译开启)支持 HTTP/3.0 。而 QUIC 是由 Cloudflare 维护并开源的项目,而本文正是通过编译 QUIC 来为 Nginx 实现 HTTP/3.0 的。

由于国内运营商的原因,基于 UDP 协议的 QUIC 在国内效果并是特别的理想,因此暂时仅供尝鲜!

安装依赖相关

# CentOS 安装libunwind扩展库
yum install libunwind-devel -y
 
# 进入 Nginx 安装目录
cd /www/server/nginx
 
# 下载编译需要用到的相关
## 下载nginx
wget http://nginx.org/download/nginx-1.19.4.tar.gz
 
## 下载quiche
git clone --recursive https://github.com/cloudflare/quiche
 
## go的下载
##注意,若系统之前已经安装过,需要先卸载旧版
yum remove go
 
## 下载 go
wget https://dl.google.com/go/go1.15.2.linux-amd64.tar.gz
 
## cmake 的下载
##注意,若系统之前已经安装过,请确认所安装版本大于 3.0,负责需要先卸载旧版
# 查看当前 cmake 版本
cmake --version
 
# 卸载旧版
yum remove cmake
 
# 下载cmake-3.11.4(高于该版本的后边可能会编译出错。)
wget https://github.com/Kitware/CMake/releases/download/v3.11.4/cmake-3.11.4.tar.gz
 
# 编译安装
tar zxf cmake-3.11.4.tar.gz && cd cmake-3.11.4 && ./bootstrap && make && make install
 
# 设置软连接
ln -s /usr/local/bin/cmake /usr/bin/cmake
 
# 解压go
tar -C /usr/local -xzf go1.15.2.linux-amd64.tar.gz
 
# 安装rust
curl https://sh.rustup.rs -sSf | sh
 
# 设置系统环境变量,也可以写入到profile中
export PATH=$PATH:/usr/local/go/bin
export PATH=$PATH:$HOME/.cargo/bin
export GOROOT=/usr/local/go
export GOBIN=$GOROOT/bin
export PATH=$PATH:$GOBIN
 
# 使其生效
source ~/.profile

至此,准备工作已就绪。

编译安装

# 解压源码包
tar zxvf nginx-1.19.4.tar.gz
 
# 进入源码目录
cd nginx-1.19.4
 
# 打补丁,这里我用的是 kn007的 Patch 项目地址:https://github.com/kn007/patch
curl https://raw.githubusercontent.com/kn007/patch/master/nginx_with_quic.patch | patch -p1
 
# 也可以用 Cloudflare 的,下载的 quiche 源码里已经有的,虽是 1.16 的 Patch 但同样支持 1.19.4 (与上边那条二选一)
patch -p01 < ../quiche/extras/nginx/nginx-1.16.patch
 
# 打补丁,使得 Nginx 使用 BoringSSL 时支持 OCSP Stapling
curl https://raw.githubusercontent.com/kn007/patch/master/Enable_BoringSSL_OCSP.patch | patch -p1
 
# 开始编译,注意,configure 配置参照 给 NGINX 添加 TLSv1.3 支持 去除 --with-cc-opt, --with-ld-opt 以及 --with-openssl-opt 部分, 然后添加以下参数
--build="quiche-$(git --git-dir=../quiche/.git rev-parse --short HEAD)" --with-http_v3_module --with-openssl=../quiche/deps/boringssl --with-quiche=../quiche
 
# 加上之前我的旧参数,最终编译配置为
./configure --user=www --group=www --prefix=/www/server/nginx --with-openssl=../quiche/deps/boringssl --with-quiche=../quiche --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/redis2-nginx-module --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/ngx-pagespeed --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --add-module=/www/server/nginx/src/ngx_brotli --add-module=/www/server/nginx/src/nginx-http-concat --add-module=/www/server/nginx/src/nginx-sorted-querystring-module --add-module=/www/server/nginx/src/ngx_http_substitutions_filter_module --add-module=/www/server/nginx/src/headers-more-nginx-module --add-module=/www/server/nginx/src/nginx_cookie_flag_module --add-module=/www/server/nginx/src/ModSecurity-nginx --add-module=/www/server/nginx/src/ngx_webp --with-http_stub_status_module --with-pcre=/www/server/nginx/src/pcre-8.44 --with-zlib=/www/server/nginx/src/zlib --with-http_ssl_module --with-http_v2_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-stream --with-stream_ssl_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-http_v2_hpack_enc --with-google_perftools_module --with-http_geoip_module --with-http_v3_module --build="quiche-$(git --git-dir=../quiche/.git rev-parse --short HEAD)"
 
# 若没有错误提示,就继续下一步
make -j$(nproc --all)

可能出现的错误

出错一

...
make -f objs/Makefile
make[1]: 进入目录“/www/server/nginx/nginx-1.19.4”
mkdir -p ../quiche/deps/boringssl/build ../quiche/deps/boringssl/.openssl/lib ../quiche/deps/boringssl/.openssl/include/openssl \
&& cd ../quiche/deps/boringssl/build \
&& cmake -DCMAKE_C_FLAGS="" -DCMAKE_CXX_FLAGS="" .. \
&& make VERBOSE=1 \
&& cd .. \
&& cp -r include/openssl/*.h .openssl/include/openssl \
&& cp build/ssl/libssl.a build/crypto/libcrypto.a .openssl/lib
CMake Error at CMakeLists.txt:1 (cmake_minimum_required):
  CMake 3.0 or higher is required.  You are running version 2.8.12.2
 
 
-- Configuring incomplete, errors occurred!
make[1]: *** [../quiche/deps/boringssl/.openssl/include/openssl/ssl.h] 错误 1
make[1]: 离开目录“/www/server/nginx/nginx-1.19.4”
make: *** [build] 错误 2

解决:

# cmake 版本太低,安装高于 3.0 的版本即可。然后执行
touch ../quiche/deps/boringssl/.openssl/include/openssl/ssl.h

出错二

...
--   No package 'libunwind-generic' found
libunwind not found. Disabling unwind tests.
CMake Error at CMakeLists.txt:51 (message):
  Could not find Go
...

解决:

# go 没有安装好,缺少需要的扩展库。执行
yum install libunwind-devel

出错三

...
make -f objs/Makefile
make[1]: 进入目录“/www/server/nginx/nginx-1.19.4”
cd ../quiche && cargo build --release --no-default-features
/bin/sh: cargo: 未找到命令
make[1]: *** [../quiche/target/release/libquiche.a] 错误 127
make[1]: 离开目录“/www/server/nginx/nginx-1.19.4”
make: *** [build] 错误 2

解决

# 没有装好rust 或环境变量没有配置对,重新安装或配置。具体前边的准备工作有提到。

出错四

...
/www/server/nginx/src/lua_nginx_module/src/ngx_http_lua_script.c: In function ‘ngx_http_lua_script_add_copy_code’:
/www/server/nginx/src/lua_nginx_module/src/ngx_http_lua_script.c:332:18: error: cast between incompatible function types from ‘size_t (*)(ngx_http_lua_script_engine_t *)’ {aka ‘long unsigned int (*)(struct <anonymous> *)’} to ‘void (*)(ngx_http_lua_script_engine_t *)’ {aka ‘void (*)(struct <anonymous> *)’} [-Werror=cast-function-type]
     code->code = (ngx_http_lua_script_code_pt)
                  ^
/www/server/nginx/src/lua_nginx_module/src/ngx_http_lua_script.c: In function ‘ngx_http_lua_script_add_capture_code’:
/www/server/nginx/src/lua_nginx_module/src/ngx_http_lua_script.c:402:18: error: cast between incompatible function types from ‘size_t (*)(ngx_http_lua_script_engine_t *)’ {aka ‘long unsigned int (*)(struct <anonymous> *)’} to ‘void (*)(ngx_http_lua_script_engine_t *)’ {aka ‘void (*)(struct <anonymous> *)’} [-Werror=cast-function-type]
     code->code = (ngx_http_lua_script_code_pt)
                  ^
cc1: all warnings being treated as errors
make[1]: *** [objs/Makefile:1912: objs/addon/src/ngx_http_lua_script.o] Error 1
make[1]: *** Waiting for unfinished jobs....
make[1]: Leaving directory '/www/server/nginx/src'
make: *** [Makefile:8: build] Error 2

解决

# 修改 ../nginx/src/objs/Makefile 文件,删除第三行的 -Werror 参数,保存
...
CC =    cc
CFLAGS =   -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -DNDK_SET_VAR  -D_GLIBCXX_USE_CXX11_ABI=0 -Wno-deprecated-declarations
CPP =    cc -E
LINK =    $(CC)
...
 
# 然后执行
touch ../quiche/deps/boringssl/.openssl/include/openssl/ssl.h
 
# 再重新执行
make -j$(nproc --all) 

出错五

...
[ 84%] Building CXX object ssl/CMakeFiles/ssl.dir/tls13_client.cc.o
cd /www/server/nginx/quiche/deps/boringssl/build/ssl && /usr/bin/c++  -DBORINGSSL_DISPATCH_TEST -DBORINGSSL_HAVE_LIBUNWIND -DBORINGSSL_IMPLEMENTATION -I/www/server/nginx/quiche/deps/boringssl/third_party/googletest/include -I/www/server/nginx/quiche/deps/boringssl/ssl/../include  -Werror -Wformat=2 -Wsign-compare -Wmissing-field-initializers -Wwrite-strings -Wvla -ggdb -Wall -fvisibility=hidden -fno-common -Wno-free-nonheap-object -Wmissing-declarations -std=c++11 -fno-exceptions -fno-rtti -Wshadow   -o CMakeFiles/ssl.dir/tls13_client.cc.o -c /www/server/nginx/quiche/deps/boringssl/ssl/tls13_client.cc
/www/server/nginx/quiche/deps/boringssl/crypto/trust_token/trust_token_test.cc: In constructor ‘bssl::{anonymous}::TrustTokenProtocolTestBase::TrustTokenProtocolTestBase(const TRUST_TOKEN_METHOD*)’:
/www/server/nginx/quiche/deps/boringssl/crypto/trust_token/trust_token_test.cc:100:7: error: declaration of ‘method’ shadows a member of 'this' [-Werror=shadow]
       : method_(method) {}
       ^
/www/server/nginx/quiche/deps/boringssl/crypto/trust_token/trust_token_test.cc: In member function ‘virtual void bssl::{anonymous}::TrustTokenMetadataTest_TruncatedProof_Test::TestBody()’:
/www/server/nginx/quiche/deps/boringssl/crypto/trust_token/trust_token_test.cc:589:12: error: declaration of ‘public_metadata’ shadows a member of 'this' [-Werror=shadow]
   uint32_t public_metadata;
            ^
/www/server/nginx/quiche/deps/boringssl/crypto/trust_token/trust_token_test.cc: In member function ‘virtual void bssl::{anonymous}::TrustTokenMetadataTest_ExcessDataProof_Test::TestBody()’:
/www/server/nginx/quiche/deps/boringssl/crypto/trust_token/trust_token_test.cc:657:12: error: declaration of ‘public_metadata’ shadows a member of 'this' [-Werror=shadow]
   uint32_t public_metadata;
...

解决

解决方式同出错四。

平滑升级

# 测试一下
sudo objs/nginx -t
 
# 复制新的编译好的执行文件 nginx,覆盖替换旧版本执行文件 nginx
mv /www/server/nginx/sbin/nginx /www/server/nginx/sbin/nginx.bak
cp /www/server/nginx/nginx-1.19.4/objs/nginx /www/server/nginx/sbin/nginx
 
# 升级
make upgrade
 
# 查看版本,Nginx 名称和版本号被我修改了。
Nginx -V
nginx version: QUIC/1.1.1 (--build=quiche-fd5e028)
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.1.0 (compatible; BoringSSL) (running with BoringSSL)
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/www/server/nginx --with-openssl=../quiche/deps/boringssl --with-quiche=../quiche --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/redis2-nginx-module --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/ngx-pagespeed --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --add-module=/www/server/nginx/src/ngx_brotli --add-module=/www/server/nginx/src/nginx-http-concat --add-module=/www/server/nginx/src/nginx-sorted-querystring-module --add-module=/www/server/nginx/src/ngx_http_substitutions_filter_module --add-module=/www/server/nginx/src/headers-more-nginx-module --add-module=/www/server/nginx/src/nginx_cookie_flag_module --add-module=/www/server/nginx/src/ModSecurity-nginx --add-module=/www/server/nginx/src/ngx_webp --with-http_stub_status_module --with-pcre=/www/server/nginx/src/pcre-8.44 --with-zlib=/www/server/nginx/src/zlib --with-http_ssl_module --with-http_v2_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-stream --with-stream_ssl_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-http_v2_hpack_enc --with-google_perftools_module --with-http_geoip_module --with-http_v3_module --build=--build=quiche-fd5e028

修改配置

# Enable QUIC and HTTP/3.
listen 443 quic reuseport;
 
# Enable HTTP/2 (optional).
listen 443 ssl http2;
 
# 只支持tls1.3
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers [TLS13+AESGCM+AES128|TLS13+CHACHA20]:TLS13+AESGCM+AES256:[EECDH+ECDSA+AESGCM+AES128|EECDH+ECDSA+CHACHA20]:EECDH+ECDSA+AESGCM+AES256:EECDH+ECDSA+AES128+SHA:EECDH+ECDSA+AES256+SHA:[EECDH+aRSA+AESGCM+AES128|EECDH+aRSA+CHACHA20]:EECDH+aRSA+AESGCM+AES256:EECDH+aRSA+AES128+SHA:EECDH+aRSA+AES256+SHA;
ssl_prefer_server_ciphers on;
ssl_early_data on;
ssl_ecdh_curve X25519:P-256:P-384;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
error_page 497  https://$host$request_uri;
 
# OCSP Stapling 启用
ssl_stapling on;
ssl_stapling_verify on;
ssl_stapling_file /home/others/ocsp/feiyubk.com.ocsp.resp;
 
# Add Alt-Svc header to negotiate HTTP/3.
add_header alt-svc 'quic=":443"; ma=2592000; ';
注意: 有多个站点时 reuseport 参数只需要添加一次, 否则 NGINX 会报错!

测试

Mac

# 注意 Mac 自带的和这个是共存的,使用时需要调用当前安装的
brew install --HEAD -s https://raw.githubusercontent.com/cloudflare/homebrew-cloudflare/master/curl.rb
curl -I  --http3 https://www.feiyubk.com/
 
# 测试结果
--- ~ » curl -I --http3 https://www.feiyubk.com
HTTP/3 200
server: NAN
date: Thu, 19 Mar 2020 09:58:04 GMT
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
last-modified: Thu, 19 Mar 2020 07:49:24 GMT
strict-transport-security: max-age=63072000; includeSubDomains; preload
access-control-allow-origin: *
alt-svc: quic=":443"; ma=2592000

Linux

# 可以参考 https://github.com/curl/curl/blob/master/docs/HTTP3.md
git clone --recursive https://github.com/cloudflare/quiche
 
cd quiche/deps/boringssl
mkdir build
cd build
cmake -DCMAKE_POSITION_INDEPENDENT_CODE=on ..
make
cd ..
mkdir -p .openssl/lib
cp build/crypto/libcrypto.a build/ssl/libssl.a .openssl/lib
ln -s $PWD/include .openssl
 
cd ../..
QUICHE_BSSL_PATH=$PWD/deps/boringssl cargo build --release --features pkg-config-meta
 
cd ..
git clone https://github.com/curl/curl
cd curl
./buildconf
./configure LDFLAGS="-Wl,-rpath,$PWD/../quiche/target/release" --with-ssl=$PWD/../quiche/deps/boringssl/.openssl --with-quiche=$PWD/../quiche/target/release
make
# 测试结果
[root@feiyu ~]# curl -I --http3 https://www.feiyubk.com
HTTP/3 200
server: NAN
date: Thu, 19 Mar 2020 10:01:06 GMT
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
last-modified: Thu, 19 Mar 2020 07:49:24 GMT
strict-transport-security: max-age=63072000; includeSubDomains; preload
access-control-allow-origin: *
alt-svc: quic=":443"; ma=2592000

浏览器

  • Chrome Canary

需要先开启 QUIC 支持,浏览器地址栏输入:chrome://flags/ 回车,依次搜 QUIC/TLS 1.3/TLS 1.3 Early Data ,分别选择 enable 后重启浏览器。然后关闭停用广告拦截应用或插件及网络代理程序,并在终端(Mac)输入:

/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary --enable-quic --quic-version=h3-29

以再次打开浏览器。有 h3-29 字样说明支持。如图:
Nginx 配置启用 QUIC 和 HTTP/3.0

经自测,目前 Google Chrome Canary 最新版 v89.0.4327.0 只需要在配置里开启 QUIC 支持,无需额外添加启动参数即可直接支持 QUIC(最低h3-29)。

  • Firefox Nightly

在浏览器地址栏输入 about:config ,搜 http3 ,将 network.http.http3.enabled 改为 true 重启浏览器即可。如图:
Nginx 配置启用 QUIC 和 HTTP/3.0
测试结果如图,显示HTTP/3 说明成功支持。
Nginx 配置启用 QUIC 和 HTTP/3.0

第三方网页在线检测

地址:https://http3check.net/

测试中踩到的坑

最初什么都编译部署好了,但是无论在哪个平台,运行 curl -I --http3 https://www.feiyubk.com/ 始终不返回任何结果和信息。在第三方在线测试也提示 QUIC 连接失败!十分郁闷,后来在 Quiche 的 issues 中得知是 CentOS7.7 的防火墙没有开启 443 的 UDP 端口,默认只开启了 443 的 TCP 端口。导致连接直接被防火墙给屏蔽了。自己想想必须的这样啊,QUIC/HTTP3 的连接协议本来就是 UDP 而非 HTTP2 的 TCP 协议!得知此原因后,只需开放相应端口的 UDP 连接即可!

# 以 CentOS 7.7 为例
# 先查看下以开放的端口,如果有就不必开启了
firewall-cmd --zone=public --list-ports
# 没有的话,开启相应协议端口,其它端口亦是如此。
# 开启 80 udp端口
firewall-cmd --zone=public --add-port=80/udp --permanent
# 开启 443 udp 端口
firewall-cmd --zone=public --add-port=443/udp --permanent
# 重启防火墙服务
systemctl restart firewalld.service