Nginx+RTMP 直播服务器集群
1 直播服务器搭建
1.1 系统环境
操作系统:Centos7.3
系统版本:CentOS Linux release 7.3.1611 (Core)
Nginx 版本:1.12
1.2 RTMP 模块安装
Git clone https://github.com/arut/nginx-rtmp-module.git
添加到 Nginx 模块中编译 --add-module
例如:
上图表明系统 Nginx 已经加载了 RTMP 模块
注意,如果需要支持 H.264,需额外安装 nginx_mod_h264_streaming 模块
wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
要
需
2.2.7/src/ngx_http_streaming_module.c 的以下内容:
掉
注
释
nginx_mod_h264_streaming-
if (r->zero_in_uri)
{
return NGX_DECLINED;
}
如上图表明已经加载 h264 模块
1.3 配置 RTMP
在 Nginx 中配置
rtmp
{ server
{
listen 1935;
application myapp {
live on;
}
}
}
参数简要说明:
record
录制相关
record all;
record_path /tmp/av;
record_suffix %Y%m%d%H%M%S.mp4;
record_unique on;
record_append on;
语法: record [off|all|audio|video|keyframes|manual]*
off - 不记录在所有
all - 音频和视频(一切)
audio -音频
video - 视频
keyframes -唯一的关键帧视频
manual - 从来没有自动启动记录仪,使用控制界面来启动/停止
record_path
指定录制文件的路径
record_suffix
录制文件的格式
语法: record_suffix value
该参数默认为 Flv 格式,即不定义的话录制文件格式为.Flv
record_suffix %Y%m%d%H%M%S.mp4 表示录制文件为 mp4 格式
record_unique
是否打开追加当前的时间戳,默认为 off
record_append
切换文件追加模式
以上
application appname {
……
}
配置完成后,重启 nginx,即可进行推拉流测试
例如:推流地址 rtmp://localhost:1935/myapp/teststream1
拉流地址 rtmp://localhost:1935/myapp/teststream1
1.4 配置 HLS
rtmp
{ server
{
listen 1935;
application hls
{ live on;
hls on;
hls_path /tmp/hls;
record all;
record_path /usr/local/nginx/html/liverecord;
record_suffix %Y%m%d%H%M%S.mp4;
record_unique on;
hls_fragment 4s;
hls_playlist_length 12s;
hls_nested on;
hls_cleanup off;
hls_fragment_naming system;
wait_key on;
sync 10ms;
}
}
}
http{ ser
ver {
8008;
listen
server_name localhost;
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
}
参数说明:
hls on; 表示打开回看(HLS)
hls_path; hls 文件路径,即.m3u8 以及 ts 切片文件路径
完成以上 rtmp{……} 及 http {……} 配置后,即可进行推拉流
例如:
推流地址: rtmp://localhost:1935/hls/mystream1
拉流地址: rtmp://localhost:8008/hls/mystream1/index.m3u8
2 直播服务器转推集群
2.1 集群配置
Nginx+RTMP 可以实现直播、点播,但单机性能有限,就需要集群。本方案采用
RTMP 推流服务器公用,辅助多节点代理服务器,用户通过节点拉流请求代理转
发到推流服务器。
配置示例如下:
推流服务器 A 配置(192.168.1.152)(192.168.1.182):
rtmp {
server {
listen 1935;
chunk_size 4000;
application myapp
{ live on;
record all;
record_path /tmp/av;
record_suffix .mp4;
record_unique on;
record_append on;
push rtmp://192.168.1.176:1935/myapp;
push rtmp://192.168.1.183:1935/myapp;
}
application hls {
live on;
hls on;
hls_path /tmp/hls;
record all;
record_path /usr/local/nginx/html/liverecord;
record_suffix %Y%m%d%H%M%S.mp4;
record_unique on;
hls_fragment 4s;
hls_playlist_length 12s;
hls_nested on;
hls_cleanup off;
hls_fragment_naming system;
wait_key on;
sync 10ms;
push rtmp://192.168.1.176:1935/hls;
push rtmp://192.168.1.183:1935/hls;
}
}
}
http{
server {
8008;
listen
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
}
}
拉流服务器(192.168.1.176)、(192.168.1.183):
rtmp {
server {
listen 1935;
chunk_size 4000;
application myapp {
# enable live streaming
live on;
}
application hls
{ live on;
hls on;
hls_path /tmp/hls;
hls_fragment 4s;
hls_playlist_length 12s;
hls_nested on;
hls_cleanup off;
hls_fragment_naming system;
wait_key on;
sync 10ms;
}
}
}
http {
server {
8080;
listen
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;