
























涵盖:
[推流端] → [SRS 源站(Origin)] ←→ [私有 CDN 边缘节点(Edge)]→ [Nginx 负载均衡] → [播放端]
↑
(可选:转码/录制)
| 角色 | 数量 | 推荐配置 | 说明 |
|---|---|---|---|
| SRS 源站(Origin) | 1~2 台(主备) | 8C16G / 10Gbps 带宽 / SSD | 处理推流(可能多路)、转码、录制;需高上行带宽 |
| CDN 边缘节点(Edge) | ≥3 台(按区域部署) | 4C8G / 1Gbps+ 带宽 / HDD | 每台可承载 3000~5000 并发播放(FLV/HLS) |
| 负载均衡器(可选) | 1~2 台 | 4C8G / 高网络吞吐 | 用于边缘节点调度(如 Nginx/LVS) |
| 监控/日志服务器(可选) | 1 台 | 4C8G | Prometheus + Grafana 监控 |
💡 注:万人并发假设为 10,000 人同时观看 720p(约 1.5Mbps/人),总下行带宽 ≈ 15 Gbps。需合理分布边缘节点。
明确带宽与并发的换算关系
带宽单位换算:
1MB/s = 8Mbps单用户直播带宽需求 = 视频码率 + 协议开销
- 标清(480P):500kbps - 800kbps
- 高清(720P):1.2Mbps - 1.5Mbps
- 超清(1080P):2Mbps - 3Mbps
理论并发上限(无优化):
- 720P(1.5Mbps / 人):2400Mbps ÷ 1.5Mbps = 1600 人
- 480P(600kbps / 人):2400Mbps ÷ 0.6Mbps = 4000 人
要突破到万人级,必须通过 协议优化、分发缓存、码率控制 三重手段,将单用户带宽再降低 50% 以上。
# 关闭防火墙和 SELinux systemctl stop firewalld && systemctl disable firewalld setenforce 0 sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config # 安装通用依赖 yum install -y wget gcc gcc-c++ make pcre-devel openssl-devel
git clone https://github.com/ossrs/srs.git cd srs/trunk ./configure --full && make
conf/origin.conflisten 1935; max_connections 3000; srs_log_tank file; srs_log_file ./objs/srs.log; http_server { enabled on; listen 8080; dir ./objs/nginx/html; } stats { network 0; disk sda sdb xvda xvdb; } vhost __defaultVhost__ { hls { enabled on; hls_path ./objs/nginx/html; hls_fragment 10; hls_window 60; } http_remux { enabled on; mount [vhost]/[app]/[stream].flv; } # 可选:录制 dvr { enabled on; dvr_path ./objs/dvr/[app]/[stream].[timestamp].mp4; } }
./objs/srs -c conf/origin.conf ✅ 此时可通过 ffmpeg 推流测试: ffmpeg -re -i demo.mp4 -c copy -f flv rtmp://<origin_ip>/live/stream123 播放地址: FLV: http://<origin_ip>:8080/live/stream123.flv HLS: http://<origin_ip>:8080/live/stream123.m3u8
每台边缘节点运行 SRS Edge 模式,回源到 Origin。
conf/edge.conflisten 1935; max_connections 5000; srs_log_tank file; srs_log_file ./objs/srs.log; http_server { enabled on; listen 8080; dir ./objs/nginx/html; } vhost __defaultVhost__ { mode remote; # Edge 模式 origin <origin_ip>:1935; # 指向源站 http_remux { enabled on; mount [vhost]/[app]/[stream].flv; } hls { enabled on; hls_path ./objs/nginx/html; } }
./objs/srs -c conf/edge.conf ✅ 播放地址变为:http://<edge_ip>:8080/live/stream123.flv,自动回源拉流。
# /etc/nginx/sites-available/cdn-lb upstream cdn_edges { server edge1_ip:8080; server edge2_ip:8080; server edge3_ip:8080; } server { listen 80; server_name live.yourdomain.com; location ~ ^/live/(.*)\.flv$ { proxy_pass http://cdn_edges/live/$1.flv; proxy_set_header Host $host; proxy_buffering off; } #srs edge不支持分片分发,分片分发使用 nginx edge location ~ ^/live/(.*)\.(m3u8|ts)$ { proxy_pass http://cdn_edges/live/$1.m3u8; } }
重载 Nginx:
sudo nginx -t && sudo systemctl reload nginx 最终播放地址:http://live.yourdomain.com/live/stream123.flv
go install github.com/ossrs/srs-bench@latest
srs-bench -c 5000 -u "http://edge_ip:8080/live/stream123.flv"
⚠️ 注意:需多台机器分布式压测,避免单机网络瓶颈。
htop)iftop / nethogs)http://<ip>:1985/api/v1/summariesnetstat -an | grep :1935 | wc -lhls_path 指向 SSD 磁盘,提升切片读写速度;max_connections 根据内存调整(每连接约 10~20KB),启用 tcp_nodelay on 减少延迟limit_conn_zone $binary_remote_addr zone=perip:10m; limit_conn perip 10; # 单 IP 最多 10 个连接
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf sysctl -p
upstream 配置 Edge 集群并开启 max_fails/fail_timeout 实现故障自动剔除,转发时禁用 Nginx 缓存(缓存由 SRS Edge 负责);边缘集群(Edge Cluster)就是为了解决很多人观看的问题,可以支持非常多的人观看直播流。注意:
就是HLS或DASH等切片的边缘集群,基于NGINX实现,所以也叫NGINX Edge Cluster。
| 问题 | 答案 |
|---|---|
| Nginx 能否做 HLS 文件分发? | ✅ 非常适合,是行业标准做法 |
| SRS Edge 能否替代? | ❌ 不能,HLS 是文件,不是流 |
| 核心配置是什么? | proxy_cache + 区分 .m3u8(短缓存)和 .ts(长缓存) |
| 最终效果 | 源站负载降低 90%+,边缘节点扛住海量并发 |
📌 记住:
“流用 SRS Edge,文件用 Nginx Cache” —— 这是构建高性能私有直播 CDN 的黄金法则。
https://www.bilibili.com/video/BV1DP4y1K7Jc/
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。