当前位置: 首页 > news >正文

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

文章目录

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


前言

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


一、HTTP2测试

1.1. 基本软件

名称操作
pkg-config >= 0.20sudo apt install pkg-config -y && pkg-config --version
libev >= 4.11sudo apt install libev-dev
zlib >= 1.2.3sudo apt install zlib1g
libc-ares >= 1.7.5sudo apt install libc-ares-dev
libxml2 >= 2.6.26sudo apt install libxml2-dev -y
libsystemd-dev >= 209sudo apt install libsystemd-dev
libevent-openssl >= 2.0.8sudo apt install libevent-dev -y
jansson >= 2.5sudo apt install libjansson-dev
jemallocsudo apt install libjemalloc-dev -y
libbrotli-dev >= 1.0.9sudo apt install libbrotli-dev
libbpf-dev >= 0.7.0详见1.3节
libssl-devsudo apt install libssl-dev
libelf-devsudo apt install libelf-dev -y
rubysudo apt install ruby-dev -y
bisonsudo apt install bison
cmakesudo 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的版本不满足要求,则可参考该方法。

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版本,因此使用源码编译:

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切换:

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. 编译官方文档(可选)

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

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

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

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

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服务端的基准测试工具。

相关文章:

  • 【STM32】知识点介绍二:GPIO引脚介绍
  • stm32 外部中断实现
  • 26考研——图(6)
  • kafka学习
  • 登录验证码的接口实习,uuid,code.
  • 数据结构十五、排序
  • 【计算机网络编码与调制】
  • 2025年- G28-Lc102-973. K 个距离原点最近的点--java版
  • 适合开发点餐系统的PHP开源框架要具备哪些优势?
  • Java设计模式之迭代器模式
  • 强化学习与智能决策:基本原理、算法及应用
  • @JSONField(serialize = false)序列化过程中排除特定字段
  • 从零构建大语言模型全栈开发指南:第二部分:模型架构设计与实现-2.2.3实战案例:在笔记本电脑上运行轻量级LLM
  • NLP高频面试题(十六)——deepspeed原理
  • 记一次线上环境JAR冲突导致程序报错org.springframework.web.util.NestedServletException
  • v-model 总结
  • Java后端API限流秘籍:高并发的防护伞与实战指南
  • unittest自动化测试实战
  • 嵌入式硬件工程师从小白到入门-PCB绘制(二)
  • Qt跨平台文件传输系统开发全解:TCP/IP协议+多线程架构
  • 重庆做网站嘉兴公司/互联网站
  • 金融建设网站/营业推广促销
  • 魅力网络营销公司/网站搜索优化技巧
  • 无锡网站建设专家无锡网站制作/河北网站推广公司
  • 360免费wifi连不上/网站性能优化方法
  • 青岛网站建设搭建/怎样推广app