基于Nginx与Nginx-rtmp-module实现
IDC服务
基于Nginx与Nginx-rtmp-module实现
2025-06-04 00:05
基于 Nginx 与 nginx-rtmp-module 实现流媒体服务是构建低成本、高性能直播和点播系统的经典方案。以下是严谨的实现指南与技术解析: ? 一、核心组件与原理
基于 Nginx 与 nginx-rtmp-module 实现流媒体服务是构建低成本、高性能直播和点播系统的经典方案。以下是严谨的实现指南与技术解析:
🧩 一、核心组件与原理
- Nginx:
- 高性能 Web 服务器/反向代理,负责 HTTP(S) 请求处理、负载均衡。
- 通过模块化扩展功能,
nginx-rtmp-module
是其核心流媒体扩展。
- nginx-rtmp-module:
- 开源模块(GitHub: arut/nginx-rtmp-module),为 Nginx 添加 RTMP/HTTP-FLV/HLS/DASH 流媒体协议支持。
- 核心功能:
- RTMP 推流/拉流:接收主播端推流(OBS/FFmpeg),向播放器分发。
- HLS 切片生成:将直播流转码为 HTTP Live Streaming (HLS) 分片(
.ts
+ .m3u8
)。
- HTTP-FLV 分发:低延迟 HTTP 流分发(优于 RTMP 拉流)。
- DASH 支持:动态自适应流(需额外配置)。
- 录制:自动保存直播流为视频文件。
- 转码:调用 FFmpeg 实时转码(分辨率、码率等)。
- 鉴权:推流/播放权限控制。
🔧 二、部署流程(以 Ubuntu/CentOS 为例)
1️⃣ 环境准备
# 安装依赖
sudo apt update && sudo apt install -y build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev ffmpeg git # Ubuntu
# sudo yum install -y gcc make pcre pcre-devel openssl openssl-devel zlib zlib-devel ffmpeg git # CentOS
2️⃣ 编译安装 Nginx + RTMP 模块
# 下载 Nginx 源码 (以稳定版 1.24.0 为例)
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
# 下载 nginx-rtmp-module
git clone https://github.com/arut/nginx-rtmp-module.git
# 编译安装 (关键步骤!)
cd nginx-1.24.0
./configure --prefix=/usr/local/nginx \
--add-module=../nginx-rtmp-module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_stub_status_module
make -j$(nproc)
sudo make install
3️⃣ 配置 RTMP 服务 (nginx.conf
)
编辑 /usr/local/nginx/conf/nginx.conf
,在 http {}
块 外 添加:
# RTMP 核心配置块
rtmp {
server {
listen 1935; # RTMP 默认端口
chunk_size 4096; # 传输分块大小
ping 30s; # 保活检测
ping_timeout 10s;
# 定义直播应用 'live'
application live {
live on; # 启用直播
record off; # 默认关闭录制
# HLS 输出配置 (重要!)
hls on; # 启用 HLS
hls_path /var/www/hls/live; # HLS 切片存储路径
hls_fragment 3s; # 切片时长
hls_playlist_length 30s; # m3u8 列表保留时长
# HTTP-FLV 支持 (低延迟)
dash on;
dash_path /var/www/dash/live;
# 推流鉴权 (可选)
# on_publish http://localhost/auth_publish;
# 转码示例 (需安装 FFmpeg)
# exec ffmpeg -i rtmp://localhost/live/$name
# -c:v libx264 -preset ultrafast -tune zerolatency -b:v 1500k -s 1280x720
# -c:a aac -b:a 128k -f flv rtmp://localhost/livehd/$name;
}
# 点播回放配置 (需提前录制)
application vod {
play /var/videos; # 点播文件目录
}
}
}
4️⃣ 配置 HTTP 访问 (HLS/FLV/DASH)
在 http {}
块内添加:
server {
listen 80;
server_name your_domain.com; # 替换为域名或 IP
location / {
root html;
index index.html;
}
# HLS 访问路径
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /var/www/hls; # 与 rtmp 配置的 hls_path 上级目录一致
add_header Cache-Control no-cache; # 禁用缓存确保实时性
}
# HTTP-FLV 访问路径
location /flv {
flv_live on; # 启用 flv 直播
chunked_transfer_encoding on;
add_header 'Access-Control-Allow-Origin' '*';
root /var/www; # 指向流目录
}
# DASH 访问路径 (若启用)
location /dash {
alias /var/www/dash;
add_header 'Access-Control-Allow-Origin' '*';
}
# 状态监控页面
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /path/to/nginx-rtmp-module/stat; # 模块路径中的 stat.xsl
}
}
5️⃣ 创建目录并启动服务
sudo mkdir -p /var/www/{hls/live,dash/live} /var/videos
sudo chmod -R 755 /var/www
sudo /usr/local/nginx/sbin/nginx -t # 测试配置
sudo /usr/local/nginx/sbin/nginx # 启动服务
📡 三、推流与播放测试
- 推流 (主播端):
- 工具:OBS Studio / FFmpeg
- OBS 设置:
- 服务器:
rtmp://your_server_ip/live
- 串流密钥:任意唯一 ID (如
test_stream
)
- FFmpeg 命令:
ffmpeg -re -i input.mp4 -c copy -f flv rtmp://your_server_ip/live/test_stream
- 播放 (观众端):
- RTMP:
rtmp://your_server_ip/live/test_stream
(VLC/Flash 播放器)
- HLS:
http://your_server_ip/hls/live/test_stream.m3u8
(Safari/VLC/H5 player)
- HTTP-FLV:
http://your_server_ip/flv/live/test_stream.flv
(VLC/FLV.js)
- DASH:
http://your_server_ip/dash/live/test_stream.mpd
(支持 DASH 的播放器)
⚙️ 四、高级功能实现
1. 推流鉴权
# 在 rtmp 应用内添加
on_publish http://localhost/auth_publish;
创建鉴权脚本 (如 auth_publish.php
) 验证推流密钥。
2. 自动录制
application live {
live on;
record all; # 录制所有流
record_path /var/recordings;
record_unique on; # 文件名加时间戳
record_suffix -%Y%m%d-%H%M%S.flv;
}
3. 多码率转码
application live {
exec_push ffmpeg -i rtmp://localhost/live/$name
-c:v libx264 -b:v 800k -s 640x360 -f flv rtmp://localhost/live/$name_low
-c:v libx264 -b:v 1500k -s 1280x720 -f flv rtmp://localhost/live/$name_mid
-c:v libx264 -b:v 3000k -s 1920x1080 -f flv rtmp://localhost/live/$name_high;
}
4. 低延迟优化 (HLS)
hls_fragment 1s; # 缩短切片
hls_playlist_length 3s; # 减少列表长度
hls_sync 100ms; # 时间戳同步精度
🔐 五、安全与性能建议
- 防火墙:开放 TCP 1935 (RTMP)、80/443 (HTTP)、443 (HTTPS)。
- HTTPS:使用 Let's Encrypt 为域名配置 SSL,保护 HLS/FLV 传输。
- 访问控制:
label :
- Nginx
- Nginx-rtmp-module