Nginx启用测试版HTTP3

HTTP3是即将到来的第三个HTTP主要版本,其来源于Google开发的QUIC,2018年由IEFT批准HTTP-over-QUIC成为HTTP/3,目前(2021年06约)HTTP3还没有正式发布,仍是草案状态,所以Nginx官方并没有对HTTP3提供支持,但是得益于Cloudflare的补丁,我们现在可以使用Nginx尝鲜一下HTTP3的功能。

写在开始

由于目前HTTP3还是草案状态没有正式发布,所以不建议将HTTP3部署到正式环境,建议是测试环境先行测试。

既然是测试,我们就没必要手动去编译,GitHub上有大佬开源了一键编译脚本,借助这个脚本,我们很简单就能让Nginx添加上HTTP3的功能。

nginx-autoinstall脚本下载及修改

nginx-autoinstall脚本支持Debian8+以及Ubuntu 16.04+以上的版本,我是使用的纯净安装的Debian10系统,下载命令如下:

apt install wget
wget https://raw.githubusercontent.com/angristan/nginx-autoinstall/master/nginx-autoinstall.sh

因为目前nginx-autoinstall所依赖的Cloudflare的HTTP3补丁只支持1.19.7以下的版本,而目前Nginx的主线版本以及稳定版本的版本号都比1.19.7更高,所以,我们如果想要使用这个补丁,需要修改下这个脚本所使用的版本。

修改脚本里面的NGINX_STABLE_VER变量,将其设置成最后一个支持Cloudflare的HTTP3补丁版本:

NGINX_STABLE_VER=1.18.0

修改完之后赋予脚本可执行权限并执行:

chmod +x nginx-autoinstall.sh
./nginx-autoinstall.sh

安装过程会询问是否安装一些模块,根据需要选择就好,如果只是为了测试HTTP3,只需要选择HTTP3相关的即可:

root@cloudbool.com:~# ./nginx-autoinstall.sh

Welcome to the nginx-autoinstall script.

What do you want to do?
   1) Install or update Nginx
   2) Uninstall Nginx
   3) Update the script
   4) Install Bad Bot Blocker
   5) Exit

Select an option [1-5]: 1

This script will install Nginx with some optional modules.

Do you want to install Nginx stable or mainline?
   1) Stable 1.18.0
   2) Mainline 1.21.0

Select an option [1-2]: 1

Please tell me which modules you want to install.
If you select none, Nginx will be installed with its default modules.

Modules to install :
       HTTP/3 (⚠️ Patch by Cloudflare for versions <= 1.19.7, will install BoringSSL, Quiche, Rust and Go) [y/n]: y
       Cloudflare's TLS Dynamic Record Resizing patch [y/n]: n
       Cloudflare's full HPACK encoding patch [y/n]: n
       PageSpeed 1.13.35.2 [y/n]: n
       Brotli [y/n]: n
       Headers More 0.33 [y/n]: n
       GeoIP (BROKEN) [y/n]: n
       Fancy index [y/n]: n
       ngx_cache_purge [y/n]: n
       nginx_substitutions_filter [y/n]: n
       ngx_http_lua_module [y/n]: n
       nginx WebDAV [y/n]: n
       nginx VTS [y/n]: n
       nginx RTMP [y/n]: n
       nginx testcookie [y/n]: n
       nginx ModSecurity [y/n]: n

Nginx is ready to be installed, press any key to continue...

如果一切正常,脚本执行完成之后可以看到“Installation done.”,说明Nginx编译成功并成功安装到系统,这时可以看到Nginx的编译选项:

root@cloudbool.com:~# nginx -V
nginx version: nginx/1.18.0
built by gcc 8.3.0 (Debian 8.3.0-6)
built with OpenSSL 1.1.1 (compatible; BoringSSL) (running with BoringSSL)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --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 --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --user=nginx --group=nginx --with-cc-opt=-Wno-deprecated-declarations --with-cc-opt=-Wno-ignored-qualifiers --with-openssl=/usr/local/src/nginx/modules/quiche/deps/boringssl --with-quiche=/usr/local/src/nginx/modules/quiche --with-http_v2_hpack_enc --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_mp4_module --with-http_auth_request_module --with-http_slice_module --with-http_stub_status_module --with-http_realip_module --with-http_sub_module --add-module=/usr/local/src/nginx/modules/incubator-pagespeed-ngx-1.13.35.2-stable --add-module=/usr/local/src/nginx/modules/ngx_brotli --add-module=/usr/local/src/nginx/modules/ngx_cache_purge --add-module=/usr/local/src/nginx/modules/ngx_http_substitutions_filter_module --with-http_dav_module --add-module=/usr/local/src/nginx/modules/nginx-dav-ext-module --with-http_v3_module

配置Nginx支持HTTP3

修改或者新建Nginx配置文件,然后添加HTTP3相关配置:

server {
        # Enable QUIC and HTTP/3.
        listen 443 quic reuseport;

        # Enable HTTP/2 (optional).
        listen 443 ssl http2;

        ssl_certificate      cert.crt;
        ssl_certificate_key  cert.key;

        # Enable all TLS versions (TLSv1.3 is required for QUIC).
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;

        # Add Alt-Svc header to negotiate HTTP/3.
        add_header alt-svc 'h3=":443"; ma=86400';
    }

如果配置没问题,修改好之后重启Nginx就可以测试HTTP3了。

值得一提的是,Cloudflare提供的补丁的HTTP3目前支持的最新草案版本号是29。