惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
V
V2EX
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
Latest news
Latest news
博客园 - 三生石上(FineUI控件)
美团技术团队
aimingoo的专栏
aimingoo的专栏
Google Online Security Blog
Google Online Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threatpost
Y
Y Combinator Blog
T
Tailwind CSS Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
Arctic Wolf
C
Cyber Attacks, Cyber Crime and Cyber Security
小众软件
小众软件
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
Tenable Blog
W
WeLiveSecurity
L
LINUX DO - 热门话题
D
Docker
Cyberwarzone
Cyberwarzone
量子位
A
About on SuperTechFans
The Last Watchdog
The Last Watchdog
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
P
Palo Alto Networks Blog
The Hacker News
The Hacker News
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Full Disclosure
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
T
The Exploit Database - CXSecurity.com
Engineering at Meta
Engineering at Meta
O
OpenAI News
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
IT之家
IT之家
S
Secure Thoughts
MongoDB | Blog
MongoDB | Blog
L
Lohrmann on Cybersecurity
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News

博客园 - 与f

微软的人机验证 docker运行hermes agent python使用docx库对在word中的表格合并操作 Python 项目创建+依赖管理+版本控制 百度飞桨PaddleOCR-VL识别发票图片输出json格式 大模型微调后导出,部署服务然后后端调用 openclaw的一些聊天配置 docker 部署 openclaw llama.cpp编译 微信小程序显示ai返回的markdown或html C#多端桌面程序(可跨平台) 主流 MySQL 热备份方案 一份基于SRS(Simple Realtime Server)+ CDN边缘节点srs edge加速的万人级直播部署清单 HLS(HTTP Live Streaming)标准的视频及加密和解密播放 HLS(HTTP Live Streaming)标准的视频及加密 使用Dockerfile创建一个hyperf容器做为开发环境 docker的hyperf框架docker-compose一键安装 通过vm虚拟中的docker 环境做开发 ubuntu 安装后ssh 连接到服务器 lvm的概念和操作(linux) PHP Attributes 注解 centos 服务器查找一些字符串 Centos8网络配置小工具 Keepalive实现一个高可用负载均衡场景 Keepalived详解:原理、编译安装与高可用集群配置
基于SRS(Simple Realtime Server)+ CDN边缘节点加速3个nginx做文件分发1个nginx做负载均衡
与f · 2026-01-20 · via 博客园 - 与f

基于SRS(Simple Realtime Server)+ CDN边缘节点加速3个nginx做文件分发1个nginx做负载均衡

这是一个典型的 HLS 私有 CDN 架构

  • 3 台 Nginx 边缘节点 用于缓存和分发 HLS 文件(.m3u8 / .ts
  • 1 台 Nginx 负载均衡器(LB) 负责将用户请求调度到后端边缘节点

下面提供 完整部署清单,包含服务器角色、配置文件、高可用建议等。

一、整体架构

[用户] 
   ↓ (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

二、服务器规划(共 4 台)

主机名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 带宽

三、部署步骤

Step 1:确保 SRS Origin 已启用 HLS

在 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

Step 2:配置 3 台 Nginx Edge 节点(以 edge01 为例)

安装 Nginx

sudo apt update && sudo apt install -y nginx

创建缓存目录

sudo apt update && sudo apt install -y nginx

配置 /etc/nginx/sites-enabled/hls-edge

proxy_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;
    }
}

重载 Nginx

sudo nginx -t && sudo systemctl reload nginx

✅ 在 edge02 和 edge03 上执行相同操作(IP 不同,配置相同)

Step 3:配置 Nginx 负载均衡器(lb01

安装 Nginx

sudo apt install -y nginx

配置 /etc/nginx/sites-enabled/hls-lb

upstream 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;
    }
}

重载 Nginx

sudo nginx -t && sudo systemctl reload nginx

🌐 如果没有域名,可在本地 hosts 测试:
echo "192.168.10.10 live.example.com" | sudo tee -a /etc/hosts

四、高可用与优化建议

1. 健康检查(可选但推荐)

Nginx Plus 支持主动健康检查,开源版可通过 max_fails + fail_timeout 实现被动检查(已配置)。

2. 缓存一致性

  • HLS 的 .ts 文件一旦生成就不会变,缓存安全
  • .m3u8 缓存时间必须 小于 hls_fragment(如 fragment=4s,则 cache ≤ 3s),避免播放器拿到过期索引

3. 日志监控

在 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;

4. 带宽估算

  • 假设 1 万人观看 720p(1.5Mbps)HLS:
    • 总下行 ≈ 15 Gbps
    • 每台 Edge 承担 ≈ 5 Gbps(需千兆以上网卡,建议万兆)

五、测试验证

1. 推流到 SRS Origin

ffmpeg -re -i demo.mp4 -c copy -f flv rtmp://192.168.10.20/live/test123

2. 从 LB 请求 HLS

# 查看 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

3. 检查缓存命中

多次请求后,响应头应包含:

4. 压测(使用 ab 或 wrk)

# 模拟 100 并发请求 m3u8
ab -n 1000 -c 100 -H "Host: live.example.com" http://192.168.10.10/live/test123.m3u8

观察:

  • LB 是否均匀分发到 3 台 Edge(看各 Edge 日志)
  • 源站(SRS)的 HTTP 请求数是否大幅减少(仅首次 miss 时回源)

六、扩展建议

需求方案
更高可用 LB 层加 Keepalived + VIP(主备)
自动扩缩容 边缘节点用 Kubernetes + Ingress Controller
HTTPS 支持 在 LB 上配置 SSL 证书(Let's Encrypt)
防盗链 在 Edge 或 LB 层加 referer / token 验证

总结

✅ 提出的架构 完全可行且生产级

  • 3 台 Nginx Edge:高效缓存 HLS 文件,降低源站压力
  • 1 台 Nginx LB:智能调度,提升可用性和并发能力

🔑 核心原则:
“HLS 是文件,不是流” → 用 Nginx Cache,不用 SRS Edge
“负载均衡 + 多边缘” → 横向扩展,支撑万人并发

“流用 SRS Edge,文件用 Nginx Cache” —— 这是构建高性能私有直播 CDN 的黄金法则。