利用nginx与nginx-rtmp-module搭建流媒体服务器实现直播

linux / 2018年07月10日 09时59分 / 5269人浏览
使用环境是centos 7.0+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
先将nginx-rtmp-module下载到linux服务器中,如果没有git需先安装git,参考linux安装git 接下来安装nginx nginx的官方网站为:http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.8.1.tar.gz 
tar -zxvf nginx-1.8.1.tar.gz 
cd nginx-1.8.1 
./configure --prefix=/usr/local/nginx  --add-module=../nginx-rtmp-module  --with-http_ssl_module   
make && make install
默认安装目录为:/root, add-module为下载的nginx-rtmp-module文件路径。如果没有wget要先安装wget组件 安装时候可能会报错没有安装openssl,需要先安装openssl:
yum -y install openssl openssl-devel
在配置文件中加入RTMP部分
rtmp {   
     
    server {   
     
        listen 1935;  #监听的端口 
     
        chunk_size 4000;   
           
            
        application hls {  #rtmp推流请求路径 
            live on;   
            hls on;   
            hls_path /usr/share/nginx/html/hls;   
            hls_fragment 5s;   
        }   
    }   
} 
其中hls_path文件夹必须要有写入的权限 再修改http中的server模块:
server { 
    listen       81;    #拉流请求的端口号
    server_name  localhost; 
   
    #charset koi8-r; 
   
    #access_log  logs/host.access.log  main; 
   
    location / { 
        root   /usr/share/nginx/html;   #跟目录文件夹
        index  index.html index.htm; 
    } 
   
    #error_page  404              /404.html; 
   
    # redirect server error pages to the static page /50x.html 
    # 
    error_page   500 502 503 504  /50x.html; 
    location = /50x.html { 
        root   html; 
    }
root可以跟据自己的需求来改的。 然后启动nginx:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 
关于更多rtmp的参数可以参考官方文档 配置好服务器后就可以开始推流了.