linux下利用nginx搭建rtmp直播服务

环境

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

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值