一、首先就是先下载nginx-rtmp-module
官方github地址:https://github.com/arut/nginx-rtmp-module

git clone https://github.com/arut/nginx-rtmp-module.git

如果没有安装git,则用如下命令进行安装

yum install git

二、安装:nginx,同时扩展nginx-rtmp-module模块

./configure --prefix=/usr/local/nginx  --add-module=../nginx-rtmp-module  --with-http_ssl_module
make && make install

说明:
1、如果安装中遇到问题,请参考另一篇文章:CentOS安装nginx
2、点此查看如何安装nginx第三方模块–add-module
三、修改nginx配置文件

vi /usr/local/nginx/conf/nginx.conf

四、在nginx.conf文件中添加如下代码

rtmp {    

    server {    

        listen 1935;  #监听的端口  

        chunk_size 4000;    

        application hls {  #rtmp推流请求路径  
            live on;    
            hls on;    
            hls_path /usr/local/nginx/html/hls;    
            hls_fragment 5s;    
        }    
    }    
} 

说明:还有就是这个路径,看你自己的实际情况的访问根目录来,我的是/usr/local/nginx/html/这个路径,
然而/usr/local/nginx/html/hls肯定没有这个目录,所以需要建一个放流文件的目录hls,并且需要改权限可读可写的权限

五、修改http中的server模块

server {  
        listen       81;  
        server_name  localhost;  

        #charset koi8-r;  

        #access_log  logs/host.access.log  main;  

        location / {  
            root   /usr/local/nginx/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;  
}

六、编辑mime.types.default配置文件,添加如下配置

application/x-mpegURL  m3u8;

七、启动nginx

/usr/local/nginx/sbin/nginx

八、最后附上完整的nginx.conf文件配置

#user  nobody;
worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    use epoll;
    worker_connections  1024;
}

#切换自动推送(多 worker 直播流)模式。默认为 off
rtmp_auto_push on;

#当 worker 被干掉时设置自动推送连接超时时间。默认为 100 毫秒
rtmp_auto_push_reconnect 1s;

rtmp {  

    server {  
        listen 1935;  

        #直播流配置
        application myapp {  
            live on;  
        }  

        application hls {  
            live on;  
            hls on;  
            hls_path /tmp/hls;  
        }  

        application qiniu {
            live on;
            push 推流地址;
        }

        application pull {
            live on;
            pull 拉流地址;
        }

        #rtmp日志设置
        access_log logs/rtmp_access.log ;
    }  
}  

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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

    server {
        listen       80;
        server_name  localhost;

        charset utf-8;

        #access_log  logs/host.access.log  main;

        location / {
            root   /opt/www/html;
            index  index.html index.htm;
        }

        #rtmp状态页面
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root /opt/software/nginx-rtmp-module/;
        }

        location /hls {  
            types {  
                application/vnd.apple.mpegurl m3u8;  
                video/mp2t ts;  
            }  
            root /tmp;  
            add_header Cache-Control no-cache;  
} 


        #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;
        }

        }
include vhosts/*.conf;
}

更多推荐