让Nginx用上QUIC(HTTP3)

发布于 2020-02-10  3015 次阅读


本文以 Nginx-1.17.8 为例
安装依赖

yum install gcc gcc-c++ make automake autoconf libtool pcre* zlib openssl openssl-devel go
#安装最新版 rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
#临时添加环境变量调用
source $HOME/.cargo/env

由于国外源较慢 建议选择国内源
cd && cd .cargo &&vim config

[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'ustc'
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

因为 Nginx 使用 QUIC 采用的是 BoringSSL 所以还需要 GO 来编译 以及 rust

#由 cloudflare 开源的 QUICHE 项目
https://github.com/cloudflare/quiche
#由于 QUICHE 里包含了 Nginx_quic 的 patch 所以下面这一步也不一定要进行操作 以下为[kn007](https://kn007.net/ "kn007")大佬制作的patch
git clone https://github.com/kn007/patch
#下载nginx-1.17.8
wget http://nginx.org/download/nginx-1.17.8.tar.gz
tar -xzvf nginx-1.17.8.tar.gz

此时目录下应包含有

quiche
nginx-1.17.8
patch

然后再进入 Nginx 目录打补丁

cd nginx-1.17.8
#如使用 kn007 大佬的补丁则为
patch -p01 < ../patch/nginx_with_quic.patch
#或者
patch -p01 < ../patch/nginx_with_spdy_quic.patch
#使用 QUICHE 自带的则是
patch -p01 < ../quiche/extras/nginx/nginx-1.16.patch

接下来就可以对nginx进行编译安装了

# 先进入 Nginx 目录
cd nginx-1.17.8
# 然后构建 Nginx
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_v3_module \
--with-openssl=../quiche/deps/boringssl \
--with-quiche=../quiche
# 忽略编译过程中的 Warning(不加这个可能会因为过程中 Warning 太多而无法继续)
export CFLAGS="-Wno-error"
# 可根据需求添加其他构建选项
# 最后执行安装
make && make install

注:如果出现报错 请输入nginx以及../quiche/deps/boringssl/build目录下输入make clean 然后重新从./configure一步开始重来

安装完成后需要 Nginx 配置文件进行修改
添加以下内容到指定位置即可

#注:一定要使用https
#略
listen 443 quic reuseport;
add_header alt-svc 'h3-25=":443"; ma=86400';
#略