Skip to content

nginx配置http-flv服务

环境介绍

  • RockyLinux 9.4
  • nginx 1.27.2
  • nginx-http-flv-module (基于nginx-rtmp-module 无需在安装)

安装

下载所需依赖包

基础依赖

bash
dnf -y install wget \
               epel-release \
               pcre \
               pcre-devel \
               zlib \
               zlib-devel \
               openssl \
               openssl-devel \
               git

nginx源码和nginx-http-flv-module源码

bash
wget https://nginx.org/download/nginx-1.27.2.tar.gz && \
tar -zxvf nginx-1.27.2.tar.gz && \
cd nginx-1.27.2 && \
git clone https://gitee.com/winshining/nginx-http-flv-module.git

编译&安装

bash
./configure --add-module=./nginx-http-flv-module   && \
make && make install && \
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx

配置

简单配置

rtmp+flv

text
rtmp{
  server{
    listen 1935;
    application live{
       live on;
       record off;
    }
  }
}
http{
  server{
      location /live {
            flv_live on;
        }
  }
}

推流设置

ffmpeg实现方式 拿一个本地文件为准

bash
ffmpeg  -i "test.mp4" -c copy -g 25 -f flv "rtmp://ip:端口/live[rtmp下配置的location]/推流码"
# 若视频编码问题可能没有画面试着更换编码 -vcodec h264

播放视频

ffplay 或其他播放设备

bash
ffplay -i http://ip/live?port=[rtmp端口]&app=live[rtmp下配置的location]&stream=[推流码]

复杂设置

rtmp+flv+m3u8+点播

text
#解决权限问题!
user root;
rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application live {
            live on;
            record off;
        }
        # HLS切片
        application hls {
                live on;
                hls on;
                wait_key on;
                hls_path /root/video;    #HLS切片存放路径
                hls_fragment 10s;
        }

        #点播
        application vod {
                play /root/video;   # 视频存放路径
        }

    }
}
http{
    server{
        listen 80;
        server_name _;
        #flv播放地址  
        location /live {
            flv_live on;
        }
        #切片播放地址
        location /hls {
                types {
                        application/vnd.apple.mpegurl m3u8;
                        video/mp2t ts;
                }
                alias /root/video;
                expires -1;
                add_header 'Cache-Control' 'no-cache';
        }
        location /stat {
            #推流播放和录制统计数据的配置
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
         }
         location /stat.xsl {
             root /user/local/nginx/nginx-http-flv-module/; #指定 stat.xsl 的位置
         }
    }

}

推流方式以及播放

  • example 推流
bash
ffmpeg  -i "test.mp4" -c copy -g 25 -f flv "rtmp://127.0.0.1:1935/live/test"

播放

text
# rtmp拉流
rtmp://127.0.0.1:1935/live/test
# flv播放
http://127.0.0.1/live?port=1935&app=live&stream=test
  • example 推流加切片
bash
ffmpeg  -i "test.mp4" -c copy -g 25 -f flv "rtmp://127.0.0.1:1935/hls/test"

播放

text
# rtmp拉流
rtmp://127.0.0.1:1935/hls/test
# flv播放
http://127.0.0.1/live?port=1935&app=hls&stream=test
# m3u8文件播放
http://127.0.0.1/hls/test.m3u8
  • example 点播 播放
bash
#播放/root/video/tmp.mp4
rtmp://127.0.0.1:1935/vod/tmp.mp4