Technical note

nginx-http-flv配置

github

https://github.com/winshining/nginx-http-flv-module/blob/master/README.CN.md

源码下载

编译

./configure --prefix=/opt/nginx-flv --with-openssl=../openssl-1.1.1g --add-module=../nginx-http-flv-module-1.2.7 --with-debug
make
make install

配置

worker_processes  1; #运行在Windows上时,设置为1,因为Windows不支持Unix domain socket

error_log logs/error.log error;

events {
    worker_connections  4096;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    keepalive_timeout  65;

    server {
        listen       80;

        location / {
            root   /opt/nginx-flv/html;
            index  index.html index.htm;
        }

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

        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头
        }

        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }

            root /opt/nginx-flv/tmp;
            add_header 'Cache-Control' 'no-cache';
        }

        location /dash {
            root /opt/nginx-flv/tmp;
            add_header 'Cache-Control' 'no-cache';
        }

        location /stat {
            #推流播放和录制统计数据的配置

            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root /opt/nginx-flv/html/rtmp; #指定stat.xsl的位置
        }

        location /control {
            rtmp_control all; #rtmp控制模块的配置
        }
    }
}

rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /opt/nginx-flv/tmp;

rtmp {
    out_queue           4096;
    out_cork            8;
    max_streams         128;
    timeout             15s;
    drop_idle_publisher 15s;

    log_interval 5s; #log模块在access.log中记录日志的间隔时间,对调试非常有用
    log_size     1m; #log模块用来记录日志的缓冲区大小

    server {
        listen 1935;
        # server_name www.test.*; #用于虚拟主机名后缀通配

        application myapp {
            live on;
            gop_cache on; #打开GOP缓存,减少首屏等待时间
        }

        application hls {
            gop_cache on;
            live on;
            hls on;
            hls_path /opt/nginx-flv/tmp/hls;

            hls_fragment 5s;
            recorder rec {  #启用录制
                record all manual;  #手动控制录制启停
                record_suffix _rec.flv;
                record_path /opt/nginx-flv/tmp/record;  #录制保存地址
                record_unique on;
           }
        }

        application dash {
            gop_cache on;
            live on;
            dash on;
            dash_path /opt/nginx-flv/tmp/dash;
        }
    }
}

注意拷贝stat.xsl到相应目录下

测试

rtmp监控

http://127.0.0.1/stat

rtmp推流

rtmp://10.30.22.242:1935/hls/mystream rtmp://10.30.22.242:1935/dash/mystream rtmp://10.30.22.242:1935/myapp/mystream

rtmp直播

rtmp://127.0.0.1:1935/hls/mystream rtmp://127.0.0.1:1935/myapp/mystream

hls直播

http://127.0.0.1/hls/mystream.m3u8

flv直播

http://127.0.0.1/live?port=1935&app=hls&stream=mystream

dash直播

http://127.0.0.1/dash/mystream.mpd

录制视频

http://127.0.0.1/control/record/start?app=hls&name=mystream&rec=rec http://127.0.0.1/control/record/stop?app=hls&name=mystream&rec=rec

ffplay

ffplay -i rtmp://127.0.0.1:1935/hls/mystream -fflags nobuffer -analyzeduration 1000000 ffplay “http://127.0.0.1/live?port=1935&app=hls&stream=mystream

参考文档