logo
0
0
WeChat Login
initial: PH OpenResty v1.0.0 full export

PH Edge Platform

2C4G 单机承载 10 万站点 + 30 万并发 + 高精度缓存清理 + 零成本热重载

基于 OpenResty 1.29.2.3 + 自研 Mini Redis(Lua)

目标承诺

指标V1 承诺物理上限
站点数10 万10 万
并发连接(混合负载)30 万38-46 万
缓存清理维度6 种(URL / URI / 目录 / 全站 / 租户 / 全局)
单 URL purge P99< 10 ms
目录级 purge P99< 50 ms
站点 onboarding< 5 s P95
配置变更 CPU 峰值< 1%

完整决策与分析见 docs/discovery/,最终方案:cache-decision-v3.md

编译模块矩阵(CDN 全功能)

OpenResty Bundle 默认模块(共 18 个,自动包含)

build.sh 不传 --without-*-module,确保以下模块全部编入,并在编译末尾自动校验。

模块用途
ngx_devel_kit (NDK)Lua 模块底层依赖,变量注册基础设施
echo-nginx-moduleecho / sleep / time 等调试编排指令
xss-nginx-moduleJSONP 与 XSS 安全处理
ngx_coolkitrewrite / echo 增强指令包
set-misc-nginx-moduleset_decode_base64 / set_random / set_secure_random_alphanum
form-input-nginx-modulePOST 表单解析为 nginx 变量
encrypted-session-nginx-moduleAES 会话加密
srcache-nginx-module上游响应缓存(与 memc/redis 后端配合)
ngx_lua核心 Lua HTTP 集成(lua-nginx-module)
ngx_lua_upstream动态上游配置 Lua API
headers-more-nginx-module任意 add/set/clear 请求/响应头(CDN 头改写主力)
array-var-nginx-moduleLua-table 风格变量
memc-nginx-moduleMemcached 文本协议代理
redis2-nginx-moduleRedis 2 协议代理
redis-nginx-moduleRedis(旧版)代理
rds-json-nginx-moduleRDS → JSON 转换
rds-csv-nginx-moduleRDS → CSV 转换
ngx_stream_luaTCP/UDP 流处理 Lua 集成

bundle 中默认禁用:iconv(需 libiconv) / drizzle(需 libdrizzle) / postgres(需 libpq)— 非 CDN 必需,保持禁用。如需启用:build.sh 内追加对应 --with-http_iconv_module 等。

第三方 C 模块(编入 nginx 二进制)

类别模块来源默认
协议HTTPS / HTTP/2 / HTTP/3 (QUIC)core✅ HTTP/3 需 OpenSSL 3.5+
流转发stream / stream_lua / stream_ssl_preread / stream_realipcore / OpenResty
邮件代理mail / mail_sslcore
三压缩gzip / brotli / zstdcore / google / tokers
图片裁剪image_filter(dynamic)core + libgd
视频 VODmp4 / flvcore
直播推流RTMP + HLS + DASHarut/nginx-rtmp-module--no-rtmp
VOD HLS/DASHnginx-vod-modulekaltura--no-vod
MPEG-TS 出 HLSnginx-ts-modulearut✅ 跟随 VOD
GeoIPGeoIP2 (MaxMind) + legacy GeoIPleev / core
缓存proxy_cache + ngx_cache_purge + 自研 Mini Rediscore / FRiCKLE / 自研
流量监控vhost-traffic-status (VTS) + Prometheus exportervozlt
WebDAVdavcore
内容替换sub + substitutions_filtercore / yaoweibin
正向代理proxy_connect(HTTP CONNECT 方法)chobits
JS 脚本njs(与 Lua 共存)nginx 官方
xsltxslt(dynamic)core
LuaLuaJIT 2.1 + ngx_lua + ngx_stream_lua + 全 lua-resty-*OpenResty

Lua 库(运行时部署到 /usr/local/openresty/lualib/

用途
lua-resty-http出站 HTTP 客户端
lua-resty-jwtJWT 校验(API 鉴权 / 签名 URL)
lua-resty-openssl证书/HMAC/RSA/AES 操作
lua-resty-acmeLet's Encrypt 自动签发与续签(V1.5)
lua-resty-mlcacheKong 多层缓存抽象(L1+L2+coalesce)
nginx-lua-prometheusPrometheus 指标导出(与 VTS 互补)
lua-resty-worker-eventsWorker 间事件总线
lua-resty-hmacHMAC 签名 URL(防盗链)
lua-resty-balancer一致性哈希 / chash
lua-resty-openidcOpenID Connect / OAuth2 SSO
pgmoonPostgreSQL 客户端
lua-resty-kafkaKafka 生产/消费
lua-resty-awsAWS SigV4 / S3 / KMS
lua-protobufProtobuf 编解码(含 C 扩展)

5 步快速启动

运行环境:Linux 6.1+ / Debian 12 或 Ubuntu 22.04+ / 2 vCPU / 4 GB RAM

1. 安装编译依赖(含 CDN 全模块所需库)

cd /home/PH/PH_OpenResty
sudo bash scripts/deps.sh

涵盖:libssl / libpcre / libgd / libjpeg / libpng / libwebp / libmaxminddb / libzstd / libxml2 / libxslt / libgeoip 等。

2. 拉取第三方 nginx 模块

sudo bash scripts/fetch-modules.sh           # 默认(含 RTMP)
# 可选:
# sudo bash scripts/fetch-modules.sh --with-vod      # 加 Kaltura VOD HLS/DASH 打包
# sudo bash scripts/fetch-modules.sh --without-rtmp  # 不带 RTMP

3. (可选)编译 OpenSSL 3.5+ 启用 HTTP/3

如系统 OpenSSL < 3.5(Ubuntu 22.04 是 3.0.x)但需要 HTTP/3:

sudo bash scripts/build-openssl.sh    # 安装到 /usr/local/openssl-3.5/

4. 编译 OpenResty

sudo bash scripts/build.sh                   # 全功能(推荐)
# 可选:
# sudo bash scripts/build.sh --no-http3       # 关闭 HTTP/3
# sudo bash scripts/build.sh --no-rtmp        # 关闭 RTMP
# sudo bash scripts/build.sh --clean          # 强制重新 configure

预计耗时:~8 分钟(2 核机器,全模块)。产物输出到 /usr/local/openresty/

5. 部署内核 / systemd / iptables 配置

sudo bash scripts/install.sh

此步会:

  • 写入 /etc/sysctl.d/99-ph-edge.conf(30+ 内核参数)
  • 写入 /etc/security/limits.d/99-ph.conf(fd 上限 150 万)
  • 部署 3 个 systemd unit(openresty / miniredis / ph-controller)
  • 应用 iptables NOTRACK 规则(443/80 端口跳过 conntrack,省 ~328 MB)
  • 生成占位 SSL 证书

6. 启动服务

# 启动顺序:先 miniredis 再 controller 最后 openresty
sudo systemctl enable --now miniredis
sudo systemctl enable --now ph-controller
sudo systemctl enable --now openresty

# 状态检查
sudo systemctl status openresty miniredis ph-controller

7. 验证健康检查

# 数据面健康检查
curl -k https://localhost/healthz
# 预期:HTTP/2 200,body 含 {"status":"ok",...}

# 连接数统计
ss -s

# 实时日志
sudo journalctl -u openresty -f

目录结构(仓库)

PH_OpenResty/
├── openresty-1.29.2.3/      # OpenResty 源码
├── scripts/
│   ├── build.sh             # OpenResty 编译脚本
│   ├── install.sh           # Phase 0 一键部署
│   └── iptables-notrack.sh  # NOTRACK 规则
├── sysctl/
│   └── 99-ph-edge.conf      # 内核调优
├── systemd/
│   ├── openresty.service    # 主数据面
│   ├── miniredis.service    # Mini Redis KV
│   └── ph-controller.service # 控制面 API
├── conf/
│   ├── nginx.conf.skeleton  # 主 nginx.conf 骨架
│   └── controller.conf      # 控制面配置(单一 admin 入口)
├── lualib/ph/
│   └── admin/router.lua     # Admin API 路由表(所有 /admin/v1/* 在此维护)
├── tests/integration/
│   └── admin-router_test.sh # Admin Router 集成测试
├── docs/
│   ├── modules/admin-router.md  # Admin 路由架构说明
│   ├── openresty-analysis/      # OpenResty 源码分析
│   └── discovery/               # 调研与决策文档(10+ 份)
└── README.md

> **Admin API 路由说明**:所有 `/admin/v1/*` 路由表集中维护在
> `lualib/ph/admin/router.lua`,`conf/controller.conf` 只保留单一 location 入口。
> 扩展新 API endpoint 只需修改路由表,无需改动 nginx 配置。
> 详见 [docs/modules/admin-router.md](docs/modules/admin-router.md)。

部署后的目录结构

/usr/local/openresty/        # OpenResty 编译产物
  ├── nginx/sbin/nginx
  ├── luajit/
  └── lualib/

/etc/ph/                     # 平台配置
  ├── nginx.conf             # 主配置
  ├── lualib/ph/             # 业务 Lua(Phase 1-3 写入)
  └── certs/placeholder.{crt,key}

/var/lib/ph/                 # 运行时数据
  ├── miniredis/
  │   ├── aof.log            # KV 持久化
  │   └── conf/miniredis.conf
  └── controller/

/var/log/ph/
  └── access.log             # 结构化 JSON 日志

实施路线

V1.0 在 2 周内交付(4 人 / 40 人天):

PhaseDay交付
Phase 0D1-D2编译 + sysctl + systemd + nginx 骨架(当前完成 ✓
Phase 1D3-D5数据面:动态 SSL + Lua 路由 + 限流 + 健康检查
Phase 2D6-D8Mini Redis(Lua 主体)+ 控制面 4 API + L2 缓存 + 路径树 tag + 6 种 purge
Phase 3D9-D10预热执行器 + edge_only 模式 + TinyLFU CMS + 30 万并发压测 + deb 打包

V1.5(+1 周):Mini Redis 主从复制 + C 胶水 nginx 模块 + ACME 自动签发 + 多节点联调。

流媒体与实时传输

直播推流(RTMP / HLS / DASH)

# 启用 RTMP(默认已启用,端口 1935)
sudo cp conf/streaming.conf.example /etc/ph/streaming.conf
# 在 /etc/ph/nginx.conf 顶层 include /etc/ph/streaming.conf;

# 推流(OBS / ffmpeg)
ffmpeg -re -i input.mp4 -c copy -f flv rtmp://your-domain:1935/live/<stream_key>

# 拉流
# HLS:  https://your-domain/hls/<stream_key>.m3u8
# DASH: https://your-domain/dash/<stream_key>.mpd

详见 conf/streaming.conf.example

实时传输(WebSocket / SSE / gRPC / MQTT / 长轮询)

conf/realtime.conf.example 提供 6 种实时传输模式的 location 配置:

协议location 前缀关键调优
WebSocket/ws/Upgrade 头 + 7d 超时 + 禁缓冲
SSE/events/ /sse/text/event-stream + X-Accel-Buffering no
gRPC/grpc.apigrpc_pass + HTTP/2 双向流
MQTT over WS/mqttSec-WebSocket-Protocol mqtt
长轮询/pollhold timeout 60s
TCP 实时stream {}端口转发 + tcp_nodelay

主 nginx.conf 已集成 WebSocket / SSE / MP4 / FLV / HLS / DASH 的 location 模板。

视频 VOD(伪流式)

# 默认已启用 mp4 + flv
location ~ \.mp4$ { mp4; }
location ~ \.flv$ { flv; }

HTTP/3 (QUIC)

如启用 HTTP/3(需 OpenSSL 3.5+):

server {
    listen 443 ssl http2 reuseport;
    listen 443 quic reuseport;          # UDP QUIC
    add_header alt-svc 'h3=":443"; ma=86400';
    ...
}

关键技术决策

详见 cache-decision-v3.md

  • 架构 = 单 catch-all server + Lua 动态路由 + 三层缓存(L1 lrucache → L2 cache_dict → 控制面 Mini Redis)
  • 存储 = 自研 Mini Redis(纯 Lua,~700 行;V1.5 加 100 行 C 胶水编译为 nginx 模块)
  • TLS = 双证书 RSA + ECDSA 自动协商(93% 客户端走 ECDSA 快路径)
  • 清理 = 路径树隐式多 tag + 世代号懒失效(Purge O(1) INCR,6 种维度统一机制)
  • 预热 = CSV 上传 + 95% 命中率准入门禁 → 切换 edge_only 模式
  • 热分层 = TinyLFU + Count-Min Sketch(2 MB / worker),高频对象自动 promote 到 L1
  • 部署 = systemd 原生(无 Docker)+ deb 包 + A/B 双路径升级

故障排查

问题检查命令
服务起不来journalctl -u openresty -n 100 --no-pager
连接数上不去ss -s;检查 cat /proc/sys/fs/file-max
内存使用高cat /proc/meminfosystemd-cgtop
TLS 握手慢openssl s_time -connect localhost:443 -new
缓存命中率低curl admin API /admin/v1/metrics/cache

文档

许可与版权

  • OpenResty 1.29.2.3 — BSD-2-Clause
  • PH Edge Platform 业务代码 — 内部专有

维护

  • 仓库根目录:/home/PH/PH_OpenResty/
  • 联系:通过控制面 admin API 上报问题