使用Github项目nghttp2的样例学习HTTP/2

文章目录

  • 前言
  • 一、HTTP2测试
    • [1.1. 基本软件](#1.1. 基本软件)
    • [1.2. jansson >= 2.5(备用)](#1.2. jansson >= 2.5(备用))
    • [1.3. libbpf-dev >= 0.7.0](#1.3. libbpf-dev >= 0.7.0)
    • [1.4. clang >= 15](#1.4. clang >= 15)
    • [1.5. 编译官方文档(可选)](#1.5. 编译官方文档(可选))
    • [1.6. aws-lc >= 1.19.0](#1.6. aws-lc >= 1.19.0)
    • [1.7. nghttp3](#1.7. nghttp3)
    • [1.8. ngtcp2](#1.8. ngtcp2)
    • [1.9. nghttp2 build form source code](#1.9. nghttp2 build form source code)
    • [1.10. 使用](#1.10. 使用)
      • [1.10.1. nghttp](#1.10.1. nghttp)
      • [1.10.2. nghttpd](#1.10.2. nghttpd)
      • [1.10.3. nghttpx](#1.10.3. nghttpx)
      • [1.10.4. h2load](#1.10.4. h2load)

前言

nghttp2是Github上的一个基于C语言的HTTP/2库,可以从该库中编译出若干二进制文件形式的样例以供入门,本文参考项目说明,分享个人从软件安装到样例使用的全过程。


一、HTTP2测试

1.1. 基本软件

名称 操作
pkg-config >= 0.20 sudo apt install pkg-config -y && pkg-config --version
libev >= 4.11 sudo apt install libev-dev
zlib >= 1.2.3 sudo apt install zlib1g
libc-ares >= 1.7.5 sudo apt install libc-ares-dev
libxml2 >= 2.6.26 sudo apt install libxml2-dev -y
libsystemd-dev >= 209 sudo apt install libsystemd-dev
libevent-openssl >= 2.0.8 sudo apt install libevent-dev -y
jansson >= 2.5 sudo apt install libjansson-dev
jemalloc sudo apt install libjemalloc-dev -y
libbrotli-dev >= 1.0.9 sudo apt install libbrotli-dev
libbpf-dev >= 0.7.0 详见1.3节
libssl-dev sudo apt install libssl-dev
libelf-dev sudo apt install libelf-dev -y
ruby sudo apt install ruby-dev -y
bison sudo apt install bison
cmake sudo apt install cmake -y
g++ >= 12 详见前一篇博客
clang++ >= 15 详见1.4节

注1:如果仅编译nghttp2库,只需要pkg-config >= 0.20即可,构建时使用--enable-lib-only参数,避免可能出现的错误。
注2:如果需要构建并运行/src目录下的应用程序(nghttp,nghttpd,nghttpx和h2load),libev >= 4.11、zlib >= 1.2.3、libc-ares >= 1.7.5以及任意嵌入式SSL库(例如aws-lc >= 1.19.0,详见1.6节)是必须的。

1.2. jansson >= 2.5(备用)

如果不能apt安装jansson的版本不满足要求,则可参考该方法。

bash 复制代码
wget http://www.digip.org/jansson/releases/jansson-2.13.1.tar.gz
tar -zxvf jansson-2.13.1.tar.gz
cd jansson-2.13.1
./configure --prefix=/usr --disable-static
make && sudo make install

1.3. libbpf-dev >= 0.7.0

Ubuntu22使用apt安装的libbpf-dev为0.5.0版本,因此使用源码编译:

bash 复制代码
git clone --depth 1 -b v1.4.6 https://github.com/libbpf/libbpf
cd libbpf
PREFIX=$PWD/build make -C src install
cd ..

1.4. clang >= 15

Ubuntu22可以使用apt安装v15版本的clang和clang++,但是需要通过update-alternatives切换:

bash 复制代码
sudo apt install clang-15 -y
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-15 10 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-15
sudo update-alternatives --config clang

1.5. 编译官方文档(可选)

使用以下命令在本地编译官方文档:

bash 复制代码
sudo apt install sphinx-common
sudo apt install python3-pip -y
pip install sphinx-rtd-theme
make html

文档将在/doc/manual/html/下生成。

1.6. aws-lc >= 1.19.0

bash 复制代码
git clone --depth 1 -b v1.39.0 https://github.com/aws/aws-lc
cd aws-lc
cmake -B build -DDISABLE_GO=ON --install-prefix=$PWD/opt
make -j$(nproc) -C build
cmake --install build
cd ..

1.7. nghttp3

如果想要在nghttp2项目编译出的二进制文件中使用实验性的HTTP/3协议,则需要编译nghttp3项目,详见博客使用Github项目nghttp3的样例学习HTTP/3

1.8. ngtcp2

bash 复制代码
git clone --depth 1 -b v1.9.1 https://github.com/ngtcp2/ngtcp2
cd ngtcp2
git submodule update --init --depth 1
autoreconf -i
./configure --prefix=$PWD/build --enable-lib-only --with-boringssl \
      BORINGSSL_CFLAGS="-I$PWD/../aws-lc/opt/include" \
      BORINGSSL_LIBS="-L$PWD/../aws-lc/opt/lib -lssl -lcrypto"
make -j$(nproc)
make install
cd ..

1.9. nghttp2 build form source code

bash 复制代码
git clone https://github.com/nghttp2/nghttp2
cd nghttp2
git submodule update --init
autoreconf -i
./configure --with-mruby --enable-http3 --with-libbpf \
      CC=clang-15 CXX=clang++-15 \
      PKG_CONFIG_PATH="$PWD/../aws-lc/opt/lib/pkgconfig:$PWD/../nghttp3/build/lib/pkgconfig:$PWD/../ngtcp2/build/lib/pkgconfig:$PWD/../libbpf/build/lib64/pkgconfig" \
      LDFLAGS="$LDFLAGS -Wl,-rpath,$PWD/../aws-lc/opt/lib -Wl,-rpath,$PWD/../libbpf/build/lib64"
make -j$(nproc)
make check

如果编译时出现如下问题:

原因是有两个event.h,分别是/usr/include/event2/event.h(apt安装libevent-dev后出现)和/usr/local/include/event.h(自带)。解决方案是将其中一个删掉。

1.10. 使用

编译完成后在/src下有nghttp、nghttpd、nghttpx等二进制文件。

1.10.1. nghttp

HTTP/2客户端。用法:

./nghttp [Options] <URI>

<URI>指定要访问的URI。

1.10.2. nghttpd

HTTP/2服务端。用法:
./nghttpd [Options]... <PORT> [<PRIVATE_KEY> <CERT>]
<PORT>指定监听的端口。
<PRIVATE_KEY>设置路径以指定服务端的私钥。必须,除非--no-tls被指定。
<CERT>设置路径以指定服务端的证书。必须,除非--no-tls被指定。

1.10.3. nghttpx

nghttpx是一个支持 HTTP/3、HTTP/2 和 HTTP/1.1 的多线程反向代理。

nghttpx有两种操作模式(Operation Mode):

默认模式下,nghttpx作为后向服务器的反向代理(reverse proxy to the backend server),监听 HTTP/3、HTTP/2 和 HTTP/1.1,并且可以作为现有 Web 服务端的 SSL/TLS 终端进行部署。

在--http2-proxy模式下,nghttpx作为前向代理(forward proxy),也叫做安全的HTTP/2代理(secure HTTP/2 proxy)。

所有模式在默认情况下前向连接均通过SSL/TLS进行加密。若要禁用加密,需要在--frontend选项中使用no-tls关键字。若禁用了加密,则传入的HTTP/1.1连接可通过HTTP升级转换为HTTP/2。另一方面,默认情况下后向连接不加密。若要加密后向连接,需要在--backend选项中使用tls关键字。

1.10.4. h2load

HTTP/2服务端的基准测试工具。

相关推荐
iOS技术狂热者1 小时前
多图超详细安装flutter&Android Studio开发环境,并配置插件
websocket·网络协议·tcp/ip·http·网络安全·https·udp
今夜有雨.2 小时前
HTTP---基础知识
服务器·网络·后端·网络协议·学习·tcp/ip·http
无名之逆7 小时前
hyperlane:Rust HTTP 服务器开发的不二之选
服务器·开发语言·前端·后端·安全·http·rust
。puppy11 小时前
HCIA—— 31 HTTP的报文、请求响应报文、方法、URI和URL
网络·网络协议·http
无职转生真好看12 小时前
HTTP和HTTPS区别
网络协议·http·https
秋名RG13 小时前
HTTP 1.0和2.0 有什么区别?
网络·网络协议·http
罗念笙13 小时前
HTTP1.0和2.0有什么区别?
网络协议·http
YiHanXii16 小时前
Axios 相关的面试题
前端·http·vue·react
金丝猴也是猿18 小时前
手机硬件检测详解:从版本信息到相机功能的全面指南
websocket·网络协议·tcp/ip·http·网络安全·https·udp
iOS技术狂热者1 天前
wireshark开启对https密文抓包
websocket·网络协议·tcp/ip·http·网络安全·https·udp