基于Nginx搭建RTMP/HLS视频直播服务器

服务器环境

服务器OS:CentOS Linux release 7.2.1511
nginx版本:1.14.1
nginx-rtmp-module:基于Nginx的开源流媒体服务器

安装nginx + nginx-rtmp-module

nginx官网官网下载最新的源码包,到nginx-rtmp-module项目地址下载最新源码
编译安装nginx,注意在参数里指定nginx-rtmp-module

# cd /software
# rz nginx-1.14.1.tar.gz
# tar xf nginx-1.14.1.tar.gz
# git clone https://github.com/arut/nginx-rtmp-module.git
# cd nginx-1.14.1
# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/soft/nginx-rtmp-module    
# make && make install 

/soft/nginx-rtmp-module 改为服务器存放的实际地址

配置nginx虚拟主机

nginx.conf中添加rtmp服务配置:

# vim /usr/local/nginx/conf/nginx.conf
rtmp {
    server {
        listen 1935; #监听的端口
        chunk_size 4096;
        application hls {
           live on;
           hls on;
           hls_path /usr/local/nginx/html/hls; #视频流文件目录(自己创建)
           hls_fragment 3s;
        }
    }
}

参数说明

  1. rtmp 是协议名称
  2. server 说明内部中是服务器的相关配置
  3. listen 监听的端口,rtmp协议的默认端口为1935
  4. application 访问的应用路径是hls
  5. live on 启用rtmp直播
  6. record off 不记录数据
  7. hls on 启用hls直播
  8. hls_path 切片保存位置
  9. hls_fragment 每个切片的长度

配置直播,hls支持以及状态监控页面

 location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                #或 application/x-mpegURL
                video/mp2t ts;
            }
            alias /usr/local/nginx/html/hls; #视频流文件目录(自己创建)
            expires -1;
            add_header Cache-Control no-cache;
       }
       location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
       }

       location /stat.xsl {
           root /usr/local/extend_module/nginx-rtmp-module/;
       }
    }
}

nginx最终配置

worker_processes 1;

events {
    worker_connections 1024;
}


http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    server {
        listen 80;
        server_name localhost;
        location / {
            root html;
            index index.html index.htm;
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }

       location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                #或 application/x-mpegURL
                video/mp2t ts;
            }
           alias /usr/local/nginx/html/hls; #视频流文件目录(自己创建)
            expires -1;
            add_header Cache-Control no-cache;
       }
       location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
       }

       location /stat.xsl {
           root /usr/local/extend_module/nginx-rtmp-module/;
       }

    }
}
rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application hls {
           live on;
           hls on;
           hls_path /usr/local/nginx/html/hls; #视频流文件目录(自己创建)
           hls_fragment 3s;
        }
    }
}

nginx常用操作

# cp /usr/local/nginx/sbin/nginx /usr/local/bin/    #把nginx加入到环境变量里,不用输入全路径了
# nginx            #第一次启动
# nginx -t        #检查配置文件是否正确
# nginx -s reload  #平滑重启,修改配置文件后,不断服务重启
# nginx -s stop    #停止服务

客户端推送

直播推流端使用rtmp协议推流,端口为1935。URL格式为:rtmp://ip:端口/hls。推流软件推荐使用开源的OBS。

流名称要与写的观看直播的页面中的xxxx.m3u8名称一致

查看直播

浏览器输入http:/xx.xx.xx.xx/hls/test.m3u8就能看直播了

posted @ 2018-11-16 11:17  雅俗丶共赏  阅读(5777)  评论(0编辑  收藏  举报