Nginx的RTMP直播模块开发参数说明和配置范例

/ nginx / 没有评论 / 1926浏览

Nginx的RTMP直播模块开发参数说明和配置范例

现在直播也是非常流行的一种应用场景,各种应用和H5页面上也经常用到直播。目前直播技术以RTMP方式是非常成熟的,虽然有10秒左右的延迟。但是这完全不影响直播的效果和稳定性。这次我们就以Nginx下的RTMP直播模块来讲一下直播的应用级别开发。安装这种类型的RTMP模块,需要重新编译NGINX服务。并且下载RTMP安装包。这里的安装和编译在另一篇牛人技术文章中说过了,在此就不多介绍了。下面我们来说说RTMP模块的一些命令参数和配置方法。 1

一. 直播RTMP模块中一些主要的配置选项

二. 在直播时的一些配置项

三. HLS协议进行m3u8实时直播

四. 录制直播视频以便回放重播

五;使用HTTP动态自适应不同带宽的视频

六. 用于视频点播的配置

七. 拉流转播到其他平台

八. 直播状态的消息和状态

九. 对直播的访问权限控制

十. 其他RTMP的配置选项

十一. 下面是一个NGINX-RTMP直播配置的范

worker_processes  1;

events {
    worker_connections  1024;
}

#这里开始是牛人技术测试直播的配置信息
rtmp {
    server {
        listen 1935;
        chunk_size 4096;
        application hls {
            live on;
            hls on;
            hls_path /byDATA/NginxRtmpNRJS/webroot/tt/hls;
            hls_fragment 5s;
        }

        #用来给115频道
        application ANuid115 {
            live on;
            hls on;
            hls_path /byDATA/NginxRtmp/webroot/ANuid115;
            hls_fragment 5s;
        }


        #用来测试
        application ANuid901 {
            live on;			
            hls on;					#实时回访
            wait_key on;				#保护TS切片
            hls_nested on;				#每个流都自动创建一个文件夹
            hls_fragment 5s;			#每个ts文件为5s的样子
            hls_fragment_naming system;#使用系统时间戳命名ts文件
            hls_playlist_length 10800s;	#保存m3u8列表长度时间,默认是30秒,可考虑三小时10800秒
            hls_cleanup on;			#是否删除列表中已经没有的媒体块TS文件,默认是开启
            hls_continuous on;			#连续模式
            hls_path /byDATA/NginxRtmp/webroot/live/record/ANuid901;	#媒体块ts的位置
        }

        application ANuid902 {
            live on;
            hls on;
            hls_path /byDATA/NginxRtmp/webroot/tt/ANuid902;
            hls_fragment 5s;
        }

        application ANuid903 {
            live on;
            hls on;
            hls_path /byDATA/NginxRtmp/webroot/tt/ANuid903;
            hls_fragment 5s;
        }
    }
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #byAdd
    #include /byDATA/NginxRtmp/conf/*.conf; 此处可以添加自定义配置文件目录
    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            #byAdd
            #root   /byDATA/NginxRtmp/webroot/tt;
            root   html;
            index  index.html index.htm;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    #byAdd
    server {
        listen       80;
        server_name  flow.320023.com;
        location /ANuid901 {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /byDATA/NginxRtmp/webroot/live/record;
            add_header Cache-Control no-cache;
        }
        #推流状态查看 http://flow.320023.com/stat
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl { 
            root /byDATA/NginxRtmp/Program/nginx-rtmp-module-master/;
        }

        #rewrite /ANuid901/(.*).m3u8$ /ANuid901/$1/index.m3u8 last;	#重写用于兼容阿里云m3u8命名格式
        #rewrite /ANuid901/(.*).ts$ /ANuid901/StreamName/$1.ts last;	#重写让上面m3u8能找到ts文件
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

RTMP直播配置总结

总体来说NGINX进行直播服务的配置,和编译不算特别难。只是在编译环境和配置选项上有需要注意点