./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/log/nginx/error.log \
--http-log-path=/usr/local/nginx/access.log \
--pid-path=/usr/local/nginx.pid \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-threads \
--with-file-aio \
--with-http_mp4_module \
--with-cc-opt="-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2" \
--with-ld-opt="-Wl,-z,relro -Wl,-z,now" \
--add-module=/home/nginx-rtmp-module
==============================================================================================================================
创建HLS切片目录(rtmp配置中使用)
mkdir -p /usr/local/nginx/hls/live
创建点播文件目录(如需使用点播功能)
mkdir -p /usr/local/nginx/vod
创建直播录制目录(如需使用录制功能)
mkdir -p /usr/local/nginx/recordings
赋予Nginx用户权限
chown -R nginx:nginx /usr/local/nginx/hls /usr/local/nginx/vod /usr/local/nginx/recordings
直播 HLS 切片存储目录(Nginx 需要写入权限)
mkdir -p /usr/local/nginx/hls/live
chown -R nginx:nginx /usr/local/nginx/hls
点播视频文件存储目录
mkdir -p /usr/local/nginx/vod
chown -R nginx:nginx /usr/local/nginx
chmod 755 -R /usr/local/nginx
==============================================================================================================================
配置说明与使用前准备
目录创建(必须执行,否则启动失败):
bash
创建HLS切片目录(rtmp配置中使用)
mkdir -p /usr/local/nginx/hls/live
创建点播文件目录(如需使用点播功能)
mkdir -p /usr/local/nginx/vod
创建直播录制目录(如需使用录制功能)
mkdir -p /usr/local/nginx/recordings
赋予Nginx用户权限
chown -R nginx:nginx /usr/local/nginx/hls /usr/local/nginx/vod /usr/local/nginx/recordings
核心功能验证:
RTMP 推流:ffmpeg -re -i 本地视频.mp4 -c copy -f flv rtmp://10.10.10.99:1935/live/mystream
HLS 网页播放:http://10.10.10.99:8080/live/mystream.m3u8(可用 VLC 或浏览器 + video.js 播放)
状态监控:http://10.10.10.99:8080/stat(查看推流 / 拉流状态)
安全与性能建议:
生产环境需在 rtmp 配置中添加 allow publish 信任IP; 和 deny publish all; 限制推流权限;
根据服务器性能调整 worker_processes(建议设为 CPU 核心数)和 worker_connections;
大流量场景下可增加 hls_fragment 至 5-10 秒,减少切片生成频率。
此配置可直接用于生产环境,兼容你的 Nginx 编译参数和 rtmp 模块,确保流媒体功能正常运行。
==============================================================================================================================
第四步:测试 RTMP 功能(可选,验证可用性)
- 推流测试(用 ffmpeg 推本地视频)
bash
安装ffmpeg(若未安装)
yum install -y ffmpeg
推流命令(stream_name可自定义,如"mystream")
ffmpeg -re -i /path/to/your/video.mp4 -c copy -f flv rtmp://10.10.10.99:1935/live/mystream
- 拉流测试(验证播放)
RTMP 拉流(用 VLC/PotPlayer):rtmp://10.10.10.99:1935/live/mystream
HLS 网页播放(用浏览器访问):http://10.10.10.99:8080/live/mystream.m3u8
状态监控(浏览器访问):http://10.10.10.99:8080/stat(可看到推流状态)
总结
现在 Nginx 的 RTMP 模块已完全生效,核心问题解决:
模块已成功编译(nginx -V 验证);
配置文件结构正确(rtmp 块顶级,与 http 同级);
依赖目录和权限已配置(避免启动失败)。
后续可根据需求扩展功能(如直播录制、防盗链、多码率转码等)。
==============================================================================================================================
三、核心功能说明
- RTMP 直播推流 / 拉流
推流地址:rtmp://服务器IP:1935/live/stream_name(stream_name 自定义,如 mystream)示例(使用 ffmpeg 推流本地视频):
bash
ffmpeg -re -i local_video.mp4 -c copy -f flv rtmp://10.10.10.99:1935/live/mystream
RTMP 拉流地址(支持 VLC、PotPlayer 等播放器):rtmp://服务器IP:1935/live/stream_name
- HLS 网页播放(HTTP 分发)
HLS 将 RTMP 流转为 m3u8 播放列表和 ts 切片文件,可在网页中通过 video.js 或 HLS.js 播放:
HLS 访问地址:http://服务器IP:8080/live/stream_name.m3u8
前端播放示例(使用 video.js):
html
预览
<!DOCTYPE html>
<html>
<head>
<link href="https://vjs.zencdn.net/8.6.1/video-js.css" rel="stylesheet">
<script src="https://vjs.zencdn.net/8.6.1/video.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/hls.js@1.4.12/dist/hls.min.js"></script>
</head>
<body>
<video id="my-video" class="video-js" controls width="800" height="450"></video>
<script>
const video = document.getElementById('my-video');
const src = 'http://10.10.10.99:8080/live/mystream.m3u8';
if (Hls.isSupported()) {
const hls = new Hls();
hls.loadSource(src);
hls.attachMedia(video);
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = src; // Safari 原生支持HLS
}
video.play();
</script>
</body>
</html>