百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

搭建Nginx+rtmp+hls直播推流服务器

off999 2025-03-26 15:28 77 浏览 0 评论

1 准备工具

使用yum安装git

Bash
[root~]# yum -y install git

下载nginx-rtmp-module,官方github地址

Bash
// 通过git clone 的方式下载到服务器上
[root~]# git clone https://github.com/arut/nginx-rtmp-module.git

yum 安装 openssl

[root~]# yum -y install openssl openssl-devel

2 安装Nginx服务器,官方网址

下载Nginx解压,并添加rtmp和openssl支持

如果需要nginx监控模块 下面倒数第二行后面添加

--with-http_stub_status_module 
--with-http_stub_status_module 
//这里我安装的是 nginx-1.10.3 版本
[root~]# wget http://nginx.org/download/nginx-1.10.3.tar.gz 
[root~]# tar -zxvf nginx-1.10.3.tar.gz 
[root~]# cd nginx-1.10.3
//添加rtmp和openssl支持
[root~]# ./configure --add-module=/替换为自己的安装路径(path/to)/nginx-rtmp-module --with-http_ssl_module
[root~]# make && make install

如果已经安装过Nginx,只需要找到Nginx源码目录添加rtmp的支持即可

1.查看当前安装的Nginx版本
[root~]# /usr/local/nginx/sbin/nginx -v
查询结果:nginx version: nginx/1.10.3
2.再使用find命令查找其位置
[root~]# find / -name nginx-1.10.3
查询结果:/root/nginx-1.10.3
3.cd到查询到的源目录
[root~]# cd  /root/nginx-1.10.3
4.添加rtmp的支持(如果看到一个绿色的 configure 文件就说明查找对了)
[root~]# ./configure --add-module=/替换为自己的安装路径(path/to)/nginx-rtmp-module
[root~]#  make && make install
5.启动nignx
[root~]# /usr/local/nginx/sbin/nginx 

3 修改Nginx配置文件nginx.conf

使用vi命令打开 nginx.conf,输入 i 进入编辑状态

[root~]# vi /usr/local/nginx/conf/nginx.conf  
# 在http节点同级加上rtmp配置:
rtmp {
    server {
        listen 1935;
        chunk_size 2048;
        drop_idle_publisher 10s; #指定的时间内丢弃空闲的publisher

        application rtmplive {
            live on;
            record off;

            notify_method get; # 跳转的时候,默认以POST方式传输参数,修改成GET方式(权限验证url参数方式用)
            on_publish http://127.0.0.1/on_publish; # 推流权限验证地址(可选)

       

        #转推流
        push rtmp://****;

}

     # HLS 配置(可选,可以和上面的applicantion配置在一起)
        application hls {
            live on;
            hls on;
            hls_path /tmp/hls; #需要手动创建路径
            hls_continuous on; #切片编号从上一次结束开始
        }
    }
}

#http配置 简单权限验证 HLS配置
http {

    server {

        listen      8080;
        
        # 推流权限验证
        location /on_publish {
            default_type text/html;
            if ( $args ~ pass=1234 ) { # 如果url参数包括 pass=1234
                return 200;
            }
            return 404;
        }

        # 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 /path/to/stat.xsl/;
        }

        # HLS(可选)
        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /tmp; #root path
            add_header Cache-Control no-cache;
        }
    }
}

nginx状态监控页面(需前面安装http_stub_status_module)

        # nginx状态监控页面
        location /status {
            # Turn on nginx stats
            stub_status on;
            # I do not need logs for stats
            access_log   off;
            # Security: Only allow access from 192.168.1.100 IP #
            #allow 192.168.1.100;
            # Send rest of the world to /dev/null #
            #deny all
        }

重启nginx

[root~]# nginx -s reload #方法一
[root~]# systemctl restart nginx.service #方法二

开机启动

# vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx 
After=network.target 
   
[Service] 
Type=forking 
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true 
   
[Install] 
WantedBy=multi-user.target
# systemctl enable nginx.service
systemctl start nginx.service    启动nginx
systemctl stop nginx.service    结束nginx
systemctl restart nginx.service    重启nginx

本文福利, 免费领取C++音视频学习资料包+学习路线大纲、技术视频/代码,内容包括(音视频开发,面试题,FFmpeg ,webRTC ,rtmp ,hls ,rtsp ,ffplay ,编解码,推拉流,srs),有需要的可以进企鹅裙927239107领取哦~

4 推流

使用obs推流

url填写 rtmp://[服务器ip]:[端口]/[nginx配置中rtmp的application名称],如
rtmp://10.129.0.100:1935/hls

如配置HLS,需填写串流码,名称随意,如 demo,这个名称在HLS方式拉流时会用到(笔者测试中发现,个别电脑推流填写串流码名称后,使用vlc不能直接播放rtmp,去掉串流码名称后可正常播放)

如配置url验证,在url后面添加验证参数,如
rtmp://10.129.0.100:1935/hls?pass=1234,“?pass=1234”可以放在串码流中

5 拉流

使用vlc拉流

填写网络URL 如
http://10.129.0.100:8080/hls/demo.m3u8

注意m3u8的文件名与推流时填的串流码一致

或者填写rtmp链接,如
rtmp://10.129.0.100:1935/hls

附:

nginx默认路径:/usr/local/nginx/html

相关推荐

python import 出现 ModuleNotFoundError 解决方法

错误的原因是你的Python环境没有正确安装库文件。本文以Scapy为例,给出详细方案:1.确认是否成功安装Scapy运行以下命令检查Scapy是否已安装:pip3list|gre...

Github 7.4k star,一个强大的 Python 库-sh!

大家好,今天为大家分享一个强大的Python库-sh。Github地址:https://github.com/amoffat/shsh库是Python生态系统中一个专门用于执行系统命令的第三方...

学习编程第148天 python编程循环的嵌套使用

今天学习的是刘金玉老师零基础Python教程第32期,主要内容是python编程循环的嵌套使用。(一)一维数组及输出#一维数组list1=["110001","四川二流子...

2025-07-09:使数组元素互不相同所需的最少操作次数。用go语言,

2025-07-09:使数组元素互不相同所需的最少操作次数。用go语言,给定一个整数数组nums和一个整数k,对于数组中的每个元素,你最多可以对其进行一次操作:将一个在区间[-k,k]内的...

python数据分析numpy基础之max求数组最大值

1python数据分析numpy基础之max求数组最大值python的numpy库的max()函数,用于计算沿指定轴(一个轴或多个轴)的最大值。用法numpy.max(a,axis=None,...

加快Python算法的四个方法(四)Dask

CDA数据分析师出品相信大家在做一些算法经常会被庞大的数据量所造成的超多计算量需要的时间而折磨的痛苦不已,接下来我们围绕四个方法来帮助大家加快一下Python的计算时间,减少大家在算法上的等待时间。...

六十六、Leetcode数组系列(中篇)(leetcode679)

@Author:Runsen@Date:2020/6/8人生最重要的不是所站的位置,而是内心所朝的方向。只要我在每篇博文中写得自己体会,修炼身心;在每天的不断重复学习中,耐住寂寞,练就真功,不畏艰难...

Numpy中的ndarray是什么?('numpy.ndarray' object has no attribute 'append')

1.创建ndarray创建数组最简单的办法就是使用array函数。它接受一切序列型的对象(包括其他数组),然后产生一个新的含有传入数据的Numpy数组。np.array会尝试为新建的这个数组推断出一个...

Python中的数据导入与查询(python怎样导入数据库)

适用场景:快速导入文本/Excel数据→Pandas读取大型数值数据→Numpy处理复杂二进制文件→h5py/scipy.io数据库交互→SQLAlchemy+Pandas一、数据...

2025-07-02:统计数组中的美丽分割。用go语言,给定一个整数数组

2025-07-02:统计数组中的美丽分割。用go语言,给定一个整数数组nums,我们要把它划分成三个连续且非空的子数组nums1、nums2、nums3,且这三个子数组按顺序拼接后还原为原数组...

2025-07-10:字符相同的最短子字符串Ⅰ。用go语言,给定一个长度

2025-07-10:字符相同的最短子字符串Ⅰ。用go语言,给定一个长度为n的二进制字符串s和一个允许执行的最大操作次数numOps。每次操作可以选择字符串中的任意一个位置i(0≤i...

2025-06-19:识别数组中的最大异常值。用go语言,你有一个长度为

2025-06-19:识别数组中的最大异常值。用go语言,你有一个长度为n的整数数组nums,其中恰好有n-2个元素属于“特殊数字”类别。剩下的两个元素中,一个等于所有这些特殊数字的总和,另...

2025-06-28:长度可被 K 整除的子数组的最大元素和。用go语言,给

2025-06-28:长度可被K整除的子数组的最大元素和。用go语言,给定一个整数数组nums和一个整数k,求nums中长度为k的倍数的非空子数组中,子数组和的最大值。返回该最大和...

在 Python 中如何向一个已排序的数组(列表) 中插入一个数呢

在Python中如何向一个已排序的数组(列表)中插入一个数呢?方法有很多种,关键在于原来数组是什么样的排序,用到啥排序方法效率高,就用哪种。我们来练习其中的几种插入方法,另外也掌握下遍历数组的...

2025-07-04:统计符合条件长度为 3 的子数组数目。用go语言,给定

2025-07-04:统计符合条件长度为3的子数组数目。用go语言,给定一个整数数组nums,请你计算有多少个长度恰好为3的连续子数组满足这样的条件:子数组的第一个元素与第三个元素的和,正好...

取消回复欢迎 发表评论: