nginx-rtmp-module是一个为Nginx添加RTMP/HTTP-FLV/HLS/DASH流媒体功能的扩展模块。以下是详细的安装和配置步骤:
确保系统已安装必要的编译工具:
# Ubuntu/Debian
sudo apt-get update
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev
# CentOS/RHEL
sudo yum install gcc make pcre-devel openssl-devel zlib-devel
# 创建工作目录
mkdir ~/nginx_build && cd ~/nginx_build
# 下载Nginx源码
wget http://nginx.org/download/nginx-1.25.3.tar.gz
tar -zxvf nginx-1.25.3.tar.gz
# 下载nginx-rtmp-module
git clone https://github.com/arut/nginx-rtmp-module.git
cd nginx-1.25.3
# 配置编译参数
./configure --prefix=/usr/local/nginx \
--add-module=../nginx-rtmp-module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_flv_module \
--with-http_mp4_module
# 编译并安装
make
sudo make install
编辑Nginx配置文件(通常位于/usr/local/nginx/conf/nginx.conf
),在http
块外添加RTMP配置:
rtmp {
server {
listen 1935; # RTMP默认端口
chunk_size 4096;
application live {
live on;
record off;
# 启用HLS支持
hls on;
hls_path /tmp/hls;
hls_fragment 3;
hls_playlist_length 60;
# 启用DASH支持(可选)
dash on;
dash_path /tmp/dash;
dash_fragment 3;
dash_playlist_length 60;
}
}
}
http {
server {
listen 80;
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
location /dash {
root /tmp;
add_header Cache-Control no-cache;
}
}
}
sudo mkdir -p /tmp/hls /tmp/dash
sudo chmod -R 755 /tmp/hls /tmp/dash
sudo /usr/local/nginx/sbin/nginx
检查Nginx是否运行:
ps aux | grep nginx
检查RTMP模块是否加载:
/usr/local/nginx/sbin/nginx -V 2>&1 | grep rtmp
使用OBS或其他RTMP推流工具推流到:
rtmp://你的服务器IP/live/流名称
播放流:
- RTMP: rtmp://你的服务器IP/live/流名称
- HLS: http://你的服务器IP/hls/流名称.m3u8
- DASH: http://你的服务器IP/dash/流名称.mpd
worker_processes auto;
max_connections 1000;
allow publish 192.168.1.0/24; deny publish all;
on_publish http://yourserver.com/auth;
通过以上步骤,您应该已经成功安装并配置了带有RTMP模块的Nginx流媒体服务器。