环境
ubuntu16.04
应用工具
nginx-1.13.10.tar.gz
nginx-rtmp-module-master.zip
nginx的服务器的搭建
安装nginx的依赖库
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev openssl libssl-dev
配置并编译nginx
使用nginx的默认配置,添加nginx的rtmp模块。
将两个包解压到同一个文件夹下
cd ./nginx-1.13.10
./configure –add-module=../nginx-rtmp-module-master
sudo make
sudo make install
运行测试nginx
进入安装目录/usr/local/nginx,运行命令./sbin/nginx
cd /usr/local/nginx
sudo ./sbin/nginx
打开浏览器在地址栏输入localhost
服务器搭建成功。
配置rtmp服务
sudo nano /usr/local/nginx/conf live_rtmp.conf
将配置信息写入文件中
#user nobody;
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
chunk_size 4000;
# TV mode: one publisher, many subscribers
application mylive {
# enable live streaming
live on;
# record first 200M of stream
record all;
record_path /home/live_record;
record_max_size 200M;
hls on;
hls_path /home/hls;
hls_fragment 1s;
hls_playlist_length 5;
allow play all;
#on_publish 'http://when start publish live call this url';
#on_done 'http://when live stop call this url';
}
}
}
http {
server {
listen 8080;
# This URL provides RTMP statistics in XML
location /stat {
rtmp_stat all;
# Use this stylesheet to view XML as web page
# in browser
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
# XML stylesheet to view RTMP stats.
# Copy stat.xsl wherever you want
# and put the full directory path here
root /usr/local/nginx/html/;
}
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /home;
add_header Cache-Control no-cache;
}
}
}
注意:
1、hls开启后产生的m3u8文件会存在hls_path下,播放端调用404的时候先去看看m3u8有没有生成。
2、stat.xsl要从nginx-rtmp的源代码根目录中考出来,考到配置的那个文件夹:
sudo cp ./nginx-rtmp-module-master/stat.xsl /usr/local/nginx/html/
检验配置文件
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/live_rtmp.conf
重启服务
sudo ./sbin/nginx -s reload
开始推流
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/live_rtmp.conf
推流地址: rtmp://服务器ip/mylive
播放地址: rtmp://服务器ip/mylive
hls地址: http://服务器ip:8080/hls.m3u8
状态查看地址:http://服务器ip:8080/stat