云上主机
买个云上的容器带公网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