HTTP 3.0 (QUIC) 的 Nginx 配置

4,629 阅读3分钟

前言

HTTP/3.0 出来已经能有一段时间了,而且功能相对都已经比较完善了。目前,NGINX-QUIC 官方预览版现已上线,传送测试页。官方部署文档:传送。顾名思义,HTTP/3.0 就是 HTTP/2.0 的下一代版本,由谷歌发出。目前主流浏览器已支持 HTTP/3.0。欲了解更多HTTP/3.0 的,可以去 Cloudflare 的官方博客文章《HTTP / 3:从头到脚的介绍》看看。而 QUIC 是由 Cloudflare 维护并开源的项目,而本文正是通过编译 QUIC 来为 Nginx 实现 HTTP/3.0 的。

HTTP 3.0 (QUIC)介绍

QUIC (Quick UDP Internet Connections), 快速 UDP 互联网连接。 QUIC是基于UDP协议的。

两个主要特性: (1)线头阻塞(HOL)问题的解决更为彻底: 基于TCP的HTTP/2,尽管从逻辑上来说,不同的流之间相互独立,不会相互影响,但在实际传输方面,数据还是要一帧一帧的发送和接收,一旦某一个流的数据有丢包,则同样会阻塞在它之后传输的流数据传输。而基于UDP的QUIC协议则可以更为彻底地解决这样的问题,让不同的流之间真正的实现相互独立传输,互不干扰。 (2)切换网络时的连接保持: 当前移动端的应用环境,用户的网络可能会经常切换,比如从办公室或家里出门,WiFi断开,网络切换为3G或4G。基于TCP的协议,由于切换网络之后,IP会改变,因而之前的连接不可能继续保持。而基于UDP的QUIC协议,则可以内建与TCP中不同的连接标识方法,从而在网络完成切换之后,恢复之前与服务器的连接。

编译安装 nginx-quic

搭建编译环境

编译程序需要先搭建编译环境,安装依赖库以及编译所需要的工具。Ubuntu 在终端输入以下指令:

sudo apt update
sudo apt install -y build-essential libtool git mercurial # 编译工具
sudo apt install -y libpcre2-dev zlib1g-dev # 依赖库

下载并编译 nginx-quic 源码文件

hg clone -b quic https://hg.nginx.org/nginx-quic

cd nginx-quic
./auto/configure --with-debug --with-http_v3_module --with-cc-opt="-I../boringssl/include" --with-ld-opt="-L../boringssl/build/ssl -L../boringssl/build/crypto" --with-openssl-opt='enable-tls1_3' --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-stream_quic_module
make
make install

编译安装 nginx

我们需要自定义程序的安装路径,并设置程序所需启用的功能和模块。

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module 

./auto/configure --with-debug \
                 --prefix=/root/nginx-quic \
                 --with-http_v3_module  --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module      \
                 --with-cc-opt="-I/root/nginx/debian/modules/boringssl/include"   \
                 --with-ld-opt="-L/root/nginx/debian/modules/boringssl/include/openssl/lib" --conf-path="/usr/local/nginx/conf/nginx.conf"

配置无误后,便可以进行编译安装。 至此,nginx 就已经安装好了,你可以通过在终端中输入 nginx -V,查看 nginx 的版本和编译选项:

nginx version: nginx/1.23.1
built by gcc 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) 
built with OpenSSL 1.1.1 (compatible; BoringSSL) (running with BoringSSL)
TLS SNI support enabled
configure arguments: --with-debug --prefix=/root/nginx-quic --with-http_v3_module --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-cc-opt=-I/root/nginx/debian/modules/boringssl/include --with-ld-opt=-L/root/nginx/debian/modules/boringssl/include/openssl/lib --conf-path=/usr/local/nginx/conf/nginx.conf

配置 nginx.conf 启用 HTTP/3

 server {
    root /var/www/hexo;
    index index.html index.htm index.nginx-debian.html;
    listen 443 http3 reuseport;
    listen [::]:443 http3 reuseport;

    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    ssl_protocols TLSv1.2 TLSv1.3;

    add_header Alt-Svc 'h3=":443"';
   
    ssl_certificate /etc/nginx/cert/cert.pem;
    ssl_certificate_key /etc/nginx/cert/cert.key;
    keepalive_timeout   70;
    server_name www.tianxiawupeng.top;
    #禁止在header中出现服务器版本,防止黑客利用版本漏洞攻击
    server_tokens off;
    #如果是全站 HTTPS 并且不考虑 HTTP 的话,可以加入 HSTS 告诉你的浏览器本网站全站加密,并且强制用 HTTPS 访
问
    fastcgi_param   HTTPS               on;
    fastcgi_param   HTTP_SCHEME         https;

    access_log      /var/log/nginx/wiki.xby1993.net.access.log;
    error_log       /var/log/nginx/wiki.xby1993.net.error.log;
    }

测试网站是否支持 HTTP/3

可以通过浏览器的开发人员工具来查看是否开启 HTTP/3 访问。

总结

HTTP/3 是未来趋势,越来越多的网站开始启用 HTTP/3,它的潜力将在不久的未来被开发到极致。我们现在就可以开始部署,让你的网站有更完美的浏览体验。

参考

vickey.fun/2022/03/19/…