一、安装 nginx 集成 nginx-rtmp-module
① 下载nginx-rtmp-module
官方github地址:https://github.com/arut/nginx-rtmp-module
git clone https://github.com/arut/nginx-rtmp-module.git
如未安装git, 请先安装git
yum install git
克隆到目录 /opt ( 依个人情况 )

② 下载nginx
nginx的官网:http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.14.1.tar.gz
tar -zxvf nginx-1.14.1.tar.gz
cd nginx-1.14.1
./configure --prefix=/opt/nginx --add-module=/opt/nginx-rtmp-module --with-http_ssl_module
make && make install
可能出现的错误

执行
yum -y install pcre-devel
安装完成, 目录: /opt/nginx

二、配置 rtmp
编辑 /opt/nginx/conf/nginx.conf
在文件最后加入即可
推流配置
rtmp {
server {
#监听的端口
listen 1935;
chunk_size 4000;
#rtmp推流路径
application fms{
live on;
hls on;
hls_path /opt/fms;
hls_fragment 5s;
}
}
}
并建立相应目录 /opt/fms ( 修改权限 777 )
播放地址配置 ( 本例服务器 80端口被占用, 修改为 808 )
server {
listen 808;
server_name 172.18.5.60;
location /{
root /opt/nginx/html;
index index.html index.htm;
}
location /fms {
types{
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /opt/fms;
expires -1;
}
}
启动nginx
/opt/nginx/sbin/nginx
三、推流直播
推流使用软件 Open Broadcaster Software
配置软件推流路径

修改 FMS URL

四、直播网址配置
移动端可直接播放 .m3u8后缀文件
H5则需通过插件进行播放, 推荐 videojs-contrib-hls
cd /opt/nginx/html/
目录下创建 live.html 写入一下内容 http://172.18.5.60:808/fms/tre.m3u8 地址为推流客户端填写的key
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>直播</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://unpkg.com/video.js/dist/video-js.css" rel="stylesheet">
</head>
<body>
<video id=example-video width=960 height=540 class="video-js vjs-default-skin" controls>
<source
src="http://172.18.5.60:808/fms/tre.m3u8"
type="application/x-mpegURL">
</video>
<script src="https://unpkg.com/video.js/dist/video.js"></script>
<script src="https://unpkg.com/videojs-contrib-hls/dist/videojs-contrib-hls.js"></script>
<script>
var player = videojs('example-video');
player.play();
</script>
</body>
</html>
五、效果
① 开始串流

② 观看直播
http://172.18.5.60:808/live.html

本文链接地址: 搭建 nginx-rtmp 流媒体直播服务器