Nginx 1.25配置QUIC和HTTP3

Nginx 1.25配置QUIC和HTTP/3

Nginx在编译时需要配置相应的SSL库,以确保能够支持HTTP3.0和HTTP2.0等基于HTTPS的协议。这些加密算法主要由OpenSSL提供。另外,BoringSSL是谷歌创建的OpenSSL分支,专门用于支持TLS 1.3的UDP协议的0-RTT数据传输加密算法。BoringSSL的特点在于其与OpenSSL的高度兼容性和同步性,一些改进和特性会逐步融入到OpenSSL中。

Nginx从1.25版本开始正式支持QUIC和HTTP/3协议。特别是从1.25.0版本开始,Linux环境下的Nginx二进制包就已经内建了QUIC和HTTP/3的支持。然而,这些新支持的协议目前仍处于实验性阶段,因此可能需要用户自行配置和编译Nginx以实现使用。

官方提供了三种SSL库供用户选择,具体可参见Nginx官方文档中的相关内容 https://nginx.org/en/docs/quic.html 在选择SSL库时,如果用户希望充分利用QUIC和HTTP/3的特性,建议使用专门为这些协议设计的SSL库,如BoringSSL、LibreSSL或QuicTLS。如果不使用这些专用的SSL库,Nginx将退回到使用OpenSSL的兼容层,可能会在某些功能上有所限制或无法实现完全的支持。

我这里使用BoringSSL来演示。

编译安装BoringSSL

*注意编译安装 BoringSSL 需要使用 cmake3 以上的版本.

# 下载boringssl库
git clone --depth=1 https://github.com/google/boringssl.git
安装相关依赖
apt-get update
apt-get install -y build-essential cmake
add-apt-repository ppa:longsleep/golang-backports
apt-get update
apt-get install -y openssl golang-go libpcre3 libpcre3-dev libssl-dev zlib1g-dev
编译BoringSSL
cd boringssl && mkdir build && cd build && cmake .. && make && cd ../../

编译安装Nginx

截止到2024年1月,Nginx目前最新版本为1.25.3

下载Nginx源码包
wget https://nginx.org/download/nginx-1.25.3.tar.gz
tar xvf nginx-1.25.3.tar.gz
useradd nginx -s /sbin/nologin -M
编译安装nginx
./configure  --prefix=/usr/local/nginx \
    --with-debug \
    --with-http_v3_module \
    --with-http_v2_module \
    --with-cc-opt="-I../boringssl/include"    --with-ld-opt="-L../boringssl/build/ssl  -L../boringssl/build/crypto"

make && make install
检查Nginx模块

配置Nginx
# 配置文件中省略了部分默认内容。
user  nginx;
worker_processes  1;

events {
    worker_connections  65535;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" $http3';
    
    server {
        listen       443 quic reuseport; 	# 启用 quic 协议
        listen       443 ssl; 				# 启用 http2 协议浏览器不支持 http3 时,可以选择 http2
        server_name  www.example.com;		

        ssl_protocols       TLSv1.3;  # QUIC requires TLS 1.3 #支持tls协议1.3
        ssl_certificate      /usr/local/nginx/ssl/www.example.com/cert.pem;
        ssl_certificate_key  /usr/local/nginx/ssl/www.example.com/cert.key;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        add_header Alt-Svc 'h3=":443"; ma=86400'; 
        # Alt-Svc 全称为 “Alternative-Service”,直译为“备选服务”。该头部列举了当前站点备选的访问方式列表,让服务器可以告诉客户端 “看,我在这个主机的这个端口用这个协议提供相同的服务”。一般用于在提供 “QUIC” 等新兴协议支持的同时,实现向下兼容
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}
启动Nginx

验证 HTTP3 生效

由于目前浏览器对HTTP3.0/QUIC的支持性有限,可以使用 https://http3check.net/ 来验证站点启用HTTP3是否成功。

参考:

  • https://www.google.cn/chrome/canary/
  • https://cloudflare-quic.com/
  • https://hg.nginx.org/nginx-quic
  • https://github.com/google/boringssl
  • https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Alt-Svc

相关文章

文章浏览阅读3.7k次,点赞2次,收藏5次。Nginx学习笔记一、N...
文章浏览阅读1.7w次,点赞14次,收藏61次。我们在使用容器的...
文章浏览阅读1.4k次。当用户在访问网站的过程中遇到404错误时...
文章浏览阅读2.7k次。docker 和 docker-compose 部署 nginx+...
文章浏览阅读1.3k次。5:再次启动nginx,可以正常启动,可以...
文章浏览阅读3.1w次,点赞105次,收藏182次。高性能:Nginx ...