Nginx rtmp 模块安装及配置及遇到的问题

来源:互联网 发布:linux安卓环境搭建 编辑:程序博客网 时间:2024/06/04 21:33

第一步需要安装nginx

具体过程参考Centos 安装 Nginx 详细过程。


第二步安装编译rtmp模块

从官方地址拉下源码:https://github.com/arut/nginx-rtmp-module
保存在一个临时目录下:/home/nginx_rtmp
在nginx 源代码目录下运行:

./configure --add-module=/home/nginx_rtmp/nginx-rtmp-module && make && make install

注意:是在nginx的源代码路径下运行不是在安装后的目录


安装已经结束,下面来看配置。

在nginx安装目录下先复制一份默认的配置文件出来:

cd /usr/local/nginx/confcp nginx.conf live_rtmp.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;        }   }}

验证配置文件正确性:

/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/live_rtmp.conf 

配置文件说明:
1、hls开启后产生的m3u8文件会存在hls_path下,播放端调用404的时候先去看看m3u8有没有生成。
2、stat.xsl要从nginx-rtmp的源代码根目录中考出来,考到配置的那个文件夹:

cp /home/nginx_rtmp/stat.xsl /usr/local/nginx/html/

3、验证的时候遇到个报错:

nginx: [emerg] unknown directive “rtmp” in xxxxx

这问题由两个方面造成,先确保安装rtmp模块的时候没有报错,其次是配置文件的编码最好是ASCII text,可以使用file nginx.conf指令查看一下。我的问题是第一个,安装的时候有报错:

报了大量的   $'\r': command not found

回忆了一下犯了个错误,我是在windows下git拉下来考到服务器上的,中间文件换编码了。。。。切记切记。。。。。要直接从linux git clone下来再安装。。。。
重新安装rtmp之后解决了这个问题。

4、./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using –with-openssl= option.

解决方法:
执行命令yum -y install openssl openssl-devel 成功之后再在nginx 源代码目录下运行
./configure


启动nginx之后就可以开始推流了:

/usr/local/nginx/sbin/nginx  -c /usr/local/nginx/conf/live_rtmp.conf 

推流地址: rtmp://服务器ip/mylive/xxxxx
播放地址: rtmp://服务器ip/mylive/xxxxx
hls地址: http://服务器ip:8080/hls/xxxxx.m3u8
状态查看地址:http://服务器ip:8080/stat

不会obs的朋友们可以先看一段视频,十分钟就学会了:
OBS studio教程

附录:
nginx-rtmp github地址
nginx-rtmp wiki地址

美好的直播人生从现在开始!!!!!!!!!!!!