使用 quictls OpenSSL 编译支持 HTTP/3 的 cURL

本文档介绍如何在 Linux 系统上编译支持 HTTP/3 的 cURL,依赖 quictls OpenSSL、nghttp3 和 ngtcp2。


0. 编译环境准备

在开始之前,需要安装一些基础编译工具和依赖库(以 Ubuntu/Debian 为例):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sudo apt update
sudo apt install -y \
build-essential \
autoconf \
automake \
libtool \
pkg-config \
cmake \
git \
curl \
wget \
make \
gcc \
g++ \
libssl-dev \
zlib1g-dev

说明:

  • build-essential:包含 gcc、g++、make 等基础编译工具

  • autoconf、automake、libtool:用于生成配置文件和构建工具

  • pkg-config:用于检测依赖库路径

  • libssl-dev:OpenSSL 开发库

  • zlib1g-dev:压缩库,部分库可能依赖

  • git:拉取源码

CentOS/RHEL:

1
2
sudo yum groupinstall "Development Tools" -y
sudo yum install -y autoconf automake libtool pkgconfig cmake git wget openssl-devel zlib-devel

1. 编译 quictls OpenSSL

1
2
3
4
5
6
7
8
9
10
git clone --depth=1 --recursive -b openssl-3.3.0-quic1 https://github.com/quictls/openssl.git
cd openssl
./Configure linux-x86_64 no-shared enable-tls1_3 --prefix=/usr/local/openssl-quic
make -j$(nproc)
make install

export PATH=/usr/local/openssl-quic/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/openssl-quic/lib64:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/usr/local/openssl-quic/lib64/pkgconfig:$PKG_CONFIG_PATH
cd ..

2. 编译 nghttp3

1
2
3
4
5
6
7
8
9
10
# 克隆 nghttp3 仓库
git clone --depth=1 --recursive https://github.com/ngtcp2/nghttp3.git
cd nghttp3

# 自动生成配置文件并编译
autoreconf -i
./configure
make -j$(nproc)
make install
cd ..

3. 编译 ngtcp2

1
2
3
4
5
6
7
8
9
10
# 克隆 ngtcp2 仓库
git clone --depth=1 --recursive https://github.com/ngtcp2/ngtcp2.git
cd ngtcp2

# 自动生成配置文件并编译,仅编译库文件
autoreconf -i
./configure --enable-lib-only
make -j$(nproc)
make install
cd ..

4. 编译 cURL(官方 master 分支)

1
2
3
4
5
6
7
8
9
10
11
12
13
# 克隆 cURL 仓库
git clone --depth=1 --recursive https://github.com/curl/curl.git
cd curl

# 生成构建配置
./buildconf

# 配置并编译 cURL,启用 OpenSSL、nghttp3、ngtcp2 和 HTTP/3
PKG_CONFIG_PATH=/usr/local/openssl-quic/lib64/pkgconfig \
./configure --with-ssl=/usr/local/openssl-quic --with-nghttp3 --with-ngtcp2 --enable-alt-svc --enable-shared
make -j$(nproc)
make install
cd ..

5. 测试 HTTP/3 支持

1
2
3
使用以下命令测试是否成功支持 HTTP/3:
curl -v --http3 https://example.com
如果输出中包含 Using HTTP/3 或类似信息,则表示编译成功,cURL 已支持 HTTP/3 协议。

6. 环境变量持久化(可选)

可以将以下内容添加到 ~/.bashrc, ~/.zshrc 或ld.so.conf.d 目录 中,方便每次登录自动生效:

1
2
3
4
5
6
7
8
export PATH=/usr/local/openssl-quic/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/openssl-quic/lib64:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/usr/local/openssl-quic/lib64/pkgconfig:$PKG_CONFIG_PATH
vim /etc/ld.so.conf.d/openssl.conf
/usr/local/lib
/usr/local/lib64
/usr/local/openssl-quic/lib64
ldconfig #生效