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。
build.sh 不传
--without-*-module,确保以下模块全部编入,并在编译末尾自动校验。
| 模块 | 用途 |
|---|---|
| ngx_devel_kit (NDK) | Lua 模块底层依赖,变量注册基础设施 |
| echo-nginx-module | echo / sleep / time 等调试编排指令 |
| xss-nginx-module | JSONP 与 XSS 安全处理 |
| ngx_coolkit | rewrite / echo 增强指令包 |
| set-misc-nginx-module | set_decode_base64 / set_random / set_secure_random_alphanum |
| form-input-nginx-module | POST 表单解析为 nginx 变量 |
| encrypted-session-nginx-module | AES 会话加密 |
| 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-module | Lua-table 风格变量 |
| memc-nginx-module | Memcached 文本协议代理 |
| redis2-nginx-module | Redis 2 协议代理 |
| redis-nginx-module | Redis(旧版)代理 |
| rds-json-nginx-module | RDS → JSON 转换 |
| rds-csv-nginx-module | RDS → CSV 转换 |
| ngx_stream_lua | TCP/UDP 流处理 Lua 集成 |
bundle 中默认禁用:iconv(需 libiconv) / drizzle(需 libdrizzle) / postgres(需 libpq)— 非 CDN 必需,保持禁用。如需启用:build.sh 内追加对应
--with-http_iconv_module等。
| 类别 | 模块 | 来源 | 默认 |
|---|---|---|---|
| 协议 | HTTPS / HTTP/2 / HTTP/3 (QUIC) | core | ✅ HTTP/3 需 OpenSSL 3.5+ |
| 流转发 | stream / stream_lua / stream_ssl_preread / stream_realip | core / OpenResty | ✅ |
| 邮件代理 | mail / mail_ssl | core | ✅ |
| 三压缩 | gzip / brotli / zstd | core / google / tokers | ✅ |
| 图片裁剪 | image_filter(dynamic) | core + libgd | ✅ |
| 视频 VOD | mp4 / flv | core | ✅ |
| 直播推流 | RTMP + HLS + DASH | arut/nginx-rtmp-module | ✅ --no-rtmp 关 |
| VOD HLS/DASH | nginx-vod-module | kaltura | ✅ --no-vod 关 |
| MPEG-TS 出 HLS | nginx-ts-module | arut | ✅ 跟随 VOD |
| GeoIP | GeoIP2 (MaxMind) + legacy GeoIP | leev / core | ✅ |
| 缓存 | proxy_cache + ngx_cache_purge + 自研 Mini Redis | core / FRiCKLE / 自研 | ✅ |
| 流量监控 | vhost-traffic-status (VTS) + Prometheus exporter | vozlt | ✅ |
| WebDAV | dav | core | ✅ |
| 内容替换 | sub + substitutions_filter | core / yaoweibin | ✅ |
| 正向代理 | proxy_connect(HTTP CONNECT 方法) | chobits | ✅ |
| JS 脚本 | njs(与 Lua 共存) | nginx 官方 | ✅ |
| xslt | xslt(dynamic) | core | ✅ |
| Lua | LuaJIT 2.1 + ngx_lua + ngx_stream_lua + 全 lua-resty-* | OpenResty | ✅ |
| 库 | 用途 |
|---|---|
| lua-resty-http | 出站 HTTP 客户端 |
| lua-resty-jwt | JWT 校验(API 鉴权 / 签名 URL) |
| lua-resty-openssl | 证书/HMAC/RSA/AES 操作 |
| lua-resty-acme | Let's Encrypt 自动签发与续签(V1.5) |
| lua-resty-mlcache | Kong 多层缓存抽象(L1+L2+coalesce) |
| nginx-lua-prometheus | Prometheus 指标导出(与 VTS 互补) |
| lua-resty-worker-events | Worker 间事件总线 |
| lua-resty-hmac | HMAC 签名 URL(防盗链) |
| lua-resty-balancer | 一致性哈希 / chash |
| lua-resty-openidc | OpenID Connect / OAuth2 SSO |
| pgmoon | PostgreSQL 客户端 |
| lua-resty-kafka | Kafka 生产/消费 |
| lua-resty-aws | AWS SigV4 / S3 / KMS |
| lua-protobuf | Protobuf 编解码(含 C 扩展) |
运行环境:Linux 6.1+ / Debian 12 或 Ubuntu 22.04+ / 2 vCPU / 4 GB RAM
cd /home/PH/PH_OpenResty
sudo bash scripts/deps.sh
涵盖:libssl / libpcre / libgd / libjpeg / libpng / libwebp / libmaxminddb / libzstd / libxml2 / libxslt / libgeoip 等。
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
如系统 OpenSSL < 3.5(Ubuntu 22.04 是 3.0.x)但需要 HTTP/3:
sudo bash scripts/build-openssl.sh # 安装到 /usr/local/openssl-3.5/
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/。
sudo bash scripts/install.sh
此步会:
/etc/sysctl.d/99-ph-edge.conf(30+ 内核参数)/etc/security/limits.d/99-ph.conf(fd 上限 150 万)# 启动顺序:先 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
# 数据面健康检查
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 人天):
| Phase | Day | 交付 |
|---|---|---|
| Phase 0 | D1-D2 | 编译 + sysctl + systemd + nginx 骨架(当前完成 ✓) |
| Phase 1 | D3-D5 | 数据面:动态 SSL + Lua 路由 + 限流 + 健康检查 |
| Phase 2 | D6-D8 | Mini Redis(Lua 主体)+ 控制面 4 API + L2 缓存 + 路径树 tag + 6 种 purge |
| Phase 3 | D9-D10 | 预热执行器 + edge_only 模式 + TinyLFU CMS + 30 万并发压测 + deb 打包 |
V1.5(+1 周):Mini Redis 主从复制 + C 胶水 nginx 模块 + ACME 自动签发 + 多节点联调。
# 启用 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。
conf/realtime.conf.example 提供 6 种实时传输模式的 location 配置:
| 协议 | location 前缀 | 关键调优 |
|---|---|---|
| WebSocket | /ws/ | Upgrade 头 + 7d 超时 + 禁缓冲 |
| SSE | /events/ /sse/ | text/event-stream + X-Accel-Buffering no |
| gRPC | /grpc.api | grpc_pass + HTTP/2 双向流 |
| MQTT over WS | /mqtt | Sec-WebSocket-Protocol mqtt |
| 长轮询 | /poll | hold timeout 60s |
| TCP 实时 | stream {} | 端口转发 + tcp_nodelay |
主 nginx.conf 已集成 WebSocket / SSE / MP4 / FLV / HLS / DASH 的 location 模板。
# 默认已启用 mp4 + flv
location ~ \.mp4$ { mp4; }
location ~ \.flv$ { flv; }
如启用 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';
...
}
| 问题 | 检查命令 |
|---|---|
| 服务起不来 | journalctl -u openresty -n 100 --no-pager |
| 连接数上不去 | ss -s;检查 cat /proc/sys/fs/file-max |
| 内存使用高 | cat /proc/meminfo;systemd-cgtop |
| TLS 握手慢 | openssl s_time -connect localhost:443 -new |
| 缓存命中率低 | curl admin API /admin/v1/metrics/cache |
docs/discovery/(10+ 份)/home/PH/PH_OpenResty/