启用 TLS 1.3 和 QUIC

这个博客作为新技术的试验田,TLS 1.3 和 QUIC 自然不能放过。

TLS 1.3

编译 nginx

以下是我的编译指令:

curl http://nginx.org/download/nginx-1.15.9.tar.gz -o nginx-1.15.9.tar.gz
tar -xzvf nginx-1.15.9.tar.gz
cd nginx-1.15.9
mkdir depends
cd depends
curl -L https://github.com/openssl/openssl/archive/OpenSSL_1_1_1.tar.gz -o openssl.tar.gz
tar -xzvf openssl.tar.gz
cd ..
mv depends/openssl-OpenSSL_1_1_1 depends/openssl
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_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_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-openssl=./depends/openssl --with-openssl-opt='enable-tls1_3 enable-weak-ssl-ciphers'
make
sudo make install

配置 nginx

需要修改的部分是 ssl_protocolsssl_cipher 部分。 ssl_protocols 需要添加 TLSv1.3ssl_cipher 需要在字符串的最前面(这样才能优先使用 TLS 1.3 的协议),加上:

TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:ECDHE-ECDSA-CHACHA20-POLY1305:

参考: 本博客开始支持 TLS 1.3 | JerryQu 的小站

QUIC

我们使用 caddy 来实现 QUIC。 caddy 的配置如下:

https://domain1:1004 https://*.domain1:1004 https://domain2:1004 https://*.domain2:1004 {
  proxy / https://127.0.0.1:443 {
    header_upstream Host {host}
    insecure_skip_verify
  }
  tls /var/www/ssl/public.cer /var/www/ssl/private.key
}

接下来,需要在 nginx 处理的 HTTP 请求中添加 alt-svc 头,例如:

add_header alt-svc 'quic=":1004", h2=":443"; ma=2592000; persist=1; v="44,43,39"';

这样理论上讲 QUIC 已经可用。(这里假定 nginx 已经提供 HTTP/2 服务) 将 QUIC 部署在和旧的 HTTPS 不同端口上。但请注意,如果 HTTPS 端口小于 1024,Chrome 不会使用大于等于 1024 端口号提供的 QUIC 服务。甚至,您可以将标准 HTTPS 和 QUIC 部署在两台分离的服务器上!您只需要对 alt-svc 头中 quic 字段进行修改即可。 您也可以选择在 nginx 中,通过 stream 来 UDP 反向代理 caddy,但这样会导致无法获取客户端 IP。 如果你还没有配置过 nginx stream,请自行查阅如何加入 stream 模块,按照上面的编译配置则已经加入 stream 模块。并且,你需要在 /etc/nginx/nginx.conf 中加入 stream {} 配置块。 在 stream {} 配置块中加入以下代码:

upstream quic_caddy {
    server 127.0.0.1:1004;
}

server {
    listen 443 udp;
    listen [::]:443 udp;
    proxy_pass quic_caddy;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注