rust实现的反向代理-Pingap

378 阅读2分钟

Pingap简述

Pingap是基于pingora开发的,pingora提供了各类模块便于rust开发者使用,但并不方便非rust开发者,因此pingap提供了以toml的形式配置简单易用的反向代理,单服务支持多location转发,通过插件的形式支持更多的需求场景。已预编译好各架构上使用的可执行文件,在releases下载即可。特性如下:

  • 服务支持配置多个Location,通过host与path筛选对应的location,按权重逐一匹配选择
  • 支持正则形式配置重写Path,方便应用按前缀区分转发
  • HTTP 1/2 的全链路支持,包括h2c
  • 基于TOML格式的配置,配置方式非常简洁,可保存至文件或etcd
  • 频繁更新的Upstream与Location相关配置调整准实时生效(30秒),其它应用配置更新后,无中断式的优雅重启程序
  • 访问日志的模板化配置,已支30多个相关属性的配置,可按需指定输出各种参数与指标
  • WEB形式的管理后台界面,无需学习,简单易用
  • 开箱即用的let's encrypttls证书,仅需配置对应域名即可
  • 不同域名的tls证书可使用在同一服务端口中,按servername自动选择匹配证书
  • 支持各种事件的推送:lets_encrypt, backend_status, diff_config, restart等等
  • 丰富的http插件:compression, static serve, limit, stats, mock, 等等
  • 提供了不同阶段的统计数据,如upstream_connect_time, upstream_processing_time, compression_time, cache_lookup_timecache_lock_time

处理流程

graph TD;
    server["HTTP服务"];
    locationA["Location A"];
    locationB["Location B"];
    locationPluginListA["转发插件列表A"];
    locationPluginListB["转发插件列表B"];
    upstreamA1["上游服务A1"];
    upstreamA2["上游服务A2"];
    upstreamB1["上游服务B1"];
    upstreamB2["上游服务B2"];
    locationResponsePluginListA["响应插件列表A"];
    locationResponsePluginListB["响应插件列表B"];

    start("新的请求") --> server

    server -- "host:HostA, Path:/api/*" --> locationA

    server -- "Path:/rest/*"--> locationB

    locationA -- "顺序执行转发插件" --> locationPluginListA

    locationB -- "顺序执行转发插件" --> locationPluginListB

    locationPluginListA -- "转发至: 10.0.0.1:8001" --> upstreamA1

    locationPluginListA -- "转发至: 10.0.0.2:8001" --> upstreamA2

    locationPluginListA -- "处理完成" --> response

    locationPluginListB -- "转发至: 10.0.0.1:8002" --> upstreamB1

    locationPluginListB -- "转发至: 10.0.0.2:8002" --> upstreamB2

    locationPluginListB -- "处理完成" --> response

    upstreamA1 -- "顺序执行响应插件" --> locationResponsePluginListA
    upstreamA2 -- "顺序执行响应插件" --> locationResponsePluginListA

    upstreamB1 -- "顺序执行响应插件" --> locationResponsePluginListB
    upstreamB2 -- "顺序执行响应插件" --> locationResponsePluginListB

    locationResponsePluginListA --> response
    locationResponsePluginListB --> response

    response["HTTP响应"] --> stop("日志记录");

Pingap核心部分功能主要处理以下逻辑(由插件实现更丰富的功能):

  • 根据path与host选择对应的location
  • location根据配置重写path以及添加相应的请求头
  • 执行相应的转发中间件
  • 执行相应的响应中间件
  • 根据配置的日志格式输出对应的访问日志

开源协议Apache-2.0

项目已在github上开源,项目地址:github.com/vicanso/pin… ,欢迎大家使用并提issue。