




















基于SRS(Simple Realtime Server)+ CDN边缘节点加速3个nginx做文件分发1个nginx做负载均衡
这是一个典型的 HLS 私有 CDN 架构:
.m3u8 / .ts)下面提供 完整部署清单,包含服务器角色、配置文件、高可用建议等。
[用户] ↓ (HTTP) [Nginx LB] —— 负载均衡(轮询/最少连接) ↓ +-------------------+ | Nginx Edge 1 | ←→ [SRS Origin](回源) | Nginx Edge 2 | ←→ [SRS Origin] | Nginx Edge 3 | ←→ [SRS Origin] +-------------------+
✅ 所有 HLS 请求路径:
http://live.example.com/live/stream.m3u8→ LB → 某个 Edge →(若未命中缓存)→ SRS Origin
| 主机名 | IP 地址 | 角色 | 配置建议 |
|---|---|---|---|
lb01 |
192.168.10.10 | 负载均衡器 | 2C4G / 1Gbps 带宽 |
edge01 |
192.168.10.11 | HLS 边缘节点 | 4C8G / 500GB SSD(缓存盘) |
edge02 |
192.168.10.12 | HLS 边缘节点 | 同上 |
edge03 |
192.168.10.13 | HLS 边缘节点 | 同上 |
origin |
192.168.10.20 | SRS 源站 | 8C16G / 10Gbps 带宽 |
在 origin 上确认 conf/origin.conf 包含:
http_server { enabled on; listen 8080; dir ./objs/nginx/html; } vhost __defaultVhost__ { hls { enabled on; hls_path ./objs/nginx/html; hls_fragment 4; hls_window 20; } }
启动后,HLS 文件路径如:
http://192.168.10.20:8080/live/stream123.m3u8
edge01 为例)sudo apt update && sudo apt install -y nginx
sudo apt update && sudo apt install -y nginx
/etc/nginx/sites-enabled/hls-edgeproxy_cache_path /var/cache/nginx/hls levels=1:2 keys_zone=hls_cache:100m max_size=50g inactive=30m use_temp_path=off; upstream origin_hls { server 192.168.10.20:8080; # SRS Origin 的 HTTP 端口 } server { listen 80; server_name edge.hls.local; location ~ \.m3u8$ { proxy_pass http://origin_hls; proxy_set_header Host $host; proxy_cache hls_cache; proxy_cache_valid 200 5s; proxy_cache_use_stale updating error timeout http_500; add_header X-Cache-Status $upstream_cache_status; expires -1; } location ~ \.ts$ { proxy_pass http://origin_hls; proxy_set_header Host $host; proxy_cache hls_cache; proxy_cache_valid 200 1h; proxy_cache_use_stale updating; add_header X-Cache-Status $upstream_cache_status; expires 1h; } # 拒绝其他路径 location / { return 403; } }
sudo nginx -t && sudo systemctl reload nginx
✅ 在
edge02和edge03上执行相同操作(IP 不同,配置相同)
lb01)sudo apt install -y nginx
/etc/nginx/sites-enabled/hls-lbupstream hls_edges { # 可选策略:least_conn(推荐)、ip_hash、round-robin(默认) least_conn; server 192.168.10.11:80 max_fails=2 fail_timeout=10s; server 192.168.10.12:80 max_fails=2 fail_timeout=10s; server 192.168.10.13:80 max_fails=2 fail_timeout=10s; } server { listen 80; server_name live.example.com; # 替换为你的域名 location / { proxy_pass http://hls_edges; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
sudo nginx -t && sudo systemctl reload nginx
🌐 如果没有域名,可在本地 hosts 测试:
echo "192.168.10.10 live.example.com" | sudo tee -a /etc/hosts
Nginx Plus 支持主动健康检查,开源版可通过 max_fails + fail_timeout 实现被动检查(已配置)。
.ts 文件一旦生成就不会变,缓存安全.m3u8 缓存时间必须 小于 hls_fragment(如 fragment=4s,则 cache ≤ 3s),避免播放器拿到过期索引在 Edge 节点开启访问日志,观察 X-Cache-Status:
log_format hls_log '$remote_addr - [$time_local] "$request" ' 'status:$status cache:$upstream_cache_status ' 'bytes:$body_bytes_sent "$http_referer"'; access_log /var/log/nginx/hls_access.log hls_log;
ffmpeg -re -i demo.mp4 -c copy -f flv rtmp://192.168.10.20/live/test123
# 查看 m3u8 curl -H "Host: live.example.com" http://192.168.10.10/live/test123.m3u8 # 查看 ts(替换实际文件名) curl -I -H "Host: live.example.com" http://192.168.10.10/live/test123-1737360000000.ts
多次请求后,响应头应包含:
# 模拟 100 并发请求 m3u8 ab -n 1000 -c 100 -H "Host: live.example.com" http://192.168.10.10/live/test123.m3u8
观察:
| 需求 | 方案 |
|---|---|
| 更高可用 | LB 层加 Keepalived + VIP(主备) |
| 自动扩缩容 | 边缘节点用 Kubernetes + Ingress Controller |
| HTTPS 支持 | 在 LB 上配置 SSL 证书(Let's Encrypt) |
| 防盗链 | 在 Edge 或 LB 层加 referer / token 验证 |
✅ 提出的架构 完全可行且生产级:
🔑 核心原则:
“HLS 是文件,不是流” → 用 Nginx Cache,不用 SRS Edge
“负载均衡 + 多边缘” → 横向扩展,支撑万人并发“流用 SRS Edge,文件用 Nginx Cache” —— 这是构建高性能私有直播 CDN 的黄金法则。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。