rtmp/flv直播源站搭建方法

云上主机

买个云上的容器带公网ip的,装linux系统

环境准备

更新系统包

复制代码
sudo yum update -y

安装编译依赖(编译器、依赖库、PCRE/OpenSSL等)

复制代码
sudo yum install -y gcc gcc-c++ make wget unzip openssl-devel pcre-devel zlib-devel git

创建工作目录

复制代码
mkdir -p /home/admin/live && cd /home/admin/live

源码安装

下载 OpenResty 源码(官网稳定版)

复制代码
wget https://openresty.org/download/openresty-1.21.4.4.tar.gz
tar -zxvf openresty-1.21.4.4.tar.gz

下载 Nginx RTMP 插件(github 官方仓库)

复制代码
git clone https://github.com/winshining/nginx-http-flv-module.git

编译源码

复制代码
cd openresty-1.21.4.4
./configure --prefix=/home/admin/live/bin/openresty --with-http_ssl_module --with-http_v2_module --add-module=../nginx-http-flv-module
gmake
gmake install

配置文件

备份原配置文件

复制代码
cp /home/admin/live/bin/openresty/nginx/conf/nginx.conf /home/admin/live/bin/openresty/nginx/conf/nginx.conf.bak

完成配置如下

复制代码
#user  nobody;
worker_processes  auto;  # 自动匹配CPU核数

error_log   /home/admin/live/bin/openresty/nginx/logs/error.log warn;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        /home/admin/live/bin/openresty/nginx/logs/nginx.pid;
#pid        logs/nginx.pid;


events {
    worker_connections  1024; # 单进程最大连接数
}

# RTMP 核心配置(直播推流/拉流关键)
rtmp {
    # 开启日志(可选,方便排查问题)
    log_format rtmp '$remote_addr [$time_local] $command $app $name $args';
    access_log logs/rtmp_access.log rtmp;
    timeout 30s;  # 全局空闲流超时(放在rtmp主块,对所有server生效)
    drop_idle_publisher 15s;
    server {
        listen 1935;  # RTMP 默认端口(必须开放安全组)
        # 直播应用配置(推流/拉流路径:rtmp://服务器IP/live/流名称)
        application live {
            live on;               # 开启直播
            # 关闭点播模块(专注直播)
            # play on;
            record off;            # 关闭自动录屏(如需录屏可改为 record on)
            max_connections 1000;  # 最大并发连接数(根据服务器带宽调整)
            
            # 可选:开启 HLS 转码(支持浏览器/H5 拉流,生成 m3u8 文件)
            hls on;
            hls_path /home/admin/live/bin/openresty/nginx/html/hls;  # HLS 文件存储路径
            hls_fragment 5s;       # 切片时长
            hls_playlist_length 30s; # 播放列表长度
            hls_continuous on;  # 连续模式,避免切片编号重置
            hls_cleanup on;  # 自动清理过期切片
            hls_nested off;  # 允许子目录(可选) 改成off 便于按流名拉流

            # FLV 拉流关键配置:启用 HTTP-FLV 模块
            # 关键优化配置
            # publisher_timeout 30s;  # 推流端空闲超时(替代 drop_idle_publisher)
            # publish_time_fix on;  # 该指令是支持的,保留
            # 将 RTMP 流转为 FLV 通过 HTTP 提供拉流
            # http_flv on;
            # 配置 FLV 拉流的临时缓冲区(优化性能)
            # chunk_size 4096;
            gop_cache on;          # 开启GOP缓存(降低首屏延迟)
        }
        
        # 可选:点播应用(如需支持视频点播可开启)
        # application vod {
        #     play /usr/local/openresty/nginx/html/vod;
        # }
    }
}


# HTTP 服务(用于访问 HLS 流、查看状态)
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /home/admin/live/bin/openresty/nginx/logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;  # HTTP 端口(访问 HLS 用)
        server_name  localhost;

        # 访问 HLS 流的根目录(http://服务器IP/hls/流名称.m3u8)
        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /home/admin/live/bin/openresty/nginx/html;
            add_header Cache-Control no-cache;  # 禁用缓存(直播实时性)
            add_header Access-Control-Allow-Origin *;  # 跨域允许
        }

        # 测试页面(可选)
        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # 直接通过 HTTP 拉 RTMP 流(推荐,无需转存文件)
        # FLV拉流核心(模块必支持的指令)
        location /flv {
            flv_live on;                  # 启用FLV拉流(模块核心)
            chunked_transfer_encoding on; # 分块传输,适配流式播放
        }

        location /live {
            flv_live on; #打开 HTTP 播放 FLV 直播流功能
            chunked_transfer_encoding on; #支持 'Transfer-Encoding: chunked' 方式回复

            add_header 'Access-Control-Allow-Origin' '*'; #添加额外的 HTTP 头
            add_header 'Access-Control-Allow-Credentials' 'true'; #添加额外的 HTTP 头
        }
    }
}

安全配置

云上主机打开ACL端口

linux的防火墙打开端口

hls切片的目录配置写权限

复制代码
sudo chmod -R 755 /home/admin/live/bin/openresty/nginx/html/hls

/home/admin目录配置执行权限

复制代码
chmod o+x /home/admin
# 同理,确保上级目录也有执行权限
chmod o+x /home/admin/live
chmod o+x /home/admin/live/bin
chmod o+x /home/admin/live/bin/openresty
chmod o+x /home/admin/live/bin/openresty/nginx
chmod o+x /home/admin/live/bin/openresty/nginx/html

运行验证

nginx

复制代码
sudo /home/admin/live/bin/openresty/nginx/sbin/nginx -t

rtmp

复制代码
# rtmp推流 
ffmpeg -re -stream_loop -1 -i "D:\ffmpeg\test.flv" -c copy -y -f flv "rtmp://8.130.xx.xx/live/test"
# rtmp拉流 
ffplay -i "rtmp://8.130.xx.xx/live/test"

flv

复制代码
ffplay -i "http://8.130.xx.xx/live?port=1935&app=live&stream=test"

hls

复制代码
# 强制h264推流 
ffmpeg -re -stream_loop -1 -i "D:\ffmpeg\test.flv" -c:v libx264 -preset ultrafast -profile:v main -c:a aac -b:a 128k -f flv "rtmp://8.130.xx.xx/live/test"
# 拉流
ffplay -i "http://8.130.151.85/hls/test.m3u8"

完成验证,关闭程序

复制代码
cd /home/admin/live/bin/openresty/nginx/sbin
./nginx -s stop
相关推荐
白太岁2 小时前
Muduo:(5) 主 Reactor 之 Acceptor 与 SubReactor 的分发
服务器·网络·c++·网络协议·tcp/ip
天上飞的粉红小猪2 小时前
数据链路层
linux·服务器·网络
开发者导航2 小时前
精选高质量网址资源的高效聚合综合性的网址导航:跳跳兔导航网
服务器·人工智能·程序人生·搜索引擎·开源软件
funnycoffee12310 小时前
linux系统DNS修改命令
linux·运维·服务器·linux dns
清漠23312 小时前
win11“网络和Internet“中无“以太网“这个选项解决记录
服务器·网络·数据库
袁小皮皮不皮12 小时前
数据通信20-IPv6基础
运维·服务器·网络·网络协议·智能路由器
醒醒该学习了!13 小时前
如何将json文件转成csv文件(python代码实操)
服务器·python·json
2401_8582861114 小时前
OS55.【Linux】理解信号量(不是信号)
linux·运维·服务器·计数器·信号量
零基础的修炼16 小时前
Linux网络---数据链路层
linux·服务器·网络