使用nginx-rtmp-module搭建一个简单的视频直播服务器,实现一对多的在线直播。

一、下载Nginx的RTMP模块(nginx-rtmp-module)

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

二、安装Nginx并添加RTMP模块支持

安装步骤参考之前的文章:Centos安装Nginx

需要注意的是,在配置时需要添加RTMP、OpenSSL模块支持。

./configure --add-module=/usr/local/nginx-rtmp-module --with-http_ssl_module

三、配置

在文件nginx.conf中添加如下配置(nginx.conf在Nginx“安装目录/conf”下)

rtmp {
    server {
        # 监听RTMP默认端口1935
        listen 1935;
        # application
    }
}

1. RTMP协议

在rtmp的server中添加如下配置

# 新建应用rtmplive来接收RTMP推流
application rtmplive {
    # 开启广播
    live on;
}

保存后重载Nginx配置,完成。

2. HLS协议

在rtmp的server中添加如下配置

# 新建应用hls来接收HLS推流
application hls {
    live on;
    # 开启HLS
    hls on;
    # .ts切片文件存储目录(不存在则自动创建)
    hls_path /usr/local/nginx/hls;
    # 切片长度
    hls_fragment 3s;
}

在配置文件中http的server中添加访问支持

location /hls {
    types {
        application/vnd.apple.mpegurl m3u8;
        video/mp2t ts;
    }
    # 访问http://domain/hls指向服务器目录
    alias /usr/local/nginx/hls;
    expires -1;
    add_header Cache-Control no-cache;
}

保存后重载配置。

3. 配置监控页面

在http中的server配置中添加

location /stat {
    rtmp_stat all;
    rtmp_stat_stylesheet stat.xsl;
}

location /stat.xsl {
    root /usr/local/nginx-rtmp-module/;
}

访问http://domain/stat,会看到如下页面

2020-10-01 215503.png

可以看到RTMP、HLS已经配置成功。

4. 完整配置

#user  nobody;
worker_processes  1;

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

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

rtmp {
    server {
        # 监听RTMP默认端口1935
        listen 1935;
        chunk_size 4096;
        # 新建应用rtmplive来接收RTMP推流
        application rtmplive {
            # 开启广播
            live on;
        }
        # 新建应用hls来接收HLS推流
        application hls {
           live on;
           # 开启HLS
           hls on;
           # .ts切片文件存储目录(不存在则自动创建)
           hls_path /usr/local/nginx/hls;
           # 切片长度
           hls_fragment 3s;
           # 为每个推流创建目录
           hls_nested on;
        }
    }
}

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 koi8-r;

        #access_log  logs/host.access.log  main;

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

        # ------添加HLS支持START------

        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            # 访问http://domain/hls指向服务器目录
            alias /usr/local/nginx/hls;
            expires -1;
            add_header Cache-Control no-cache;
            # 解决网页端拉流跨域
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
        }

        # ------添加HLS支持END------

        # ------添加RTMP服务状态监控START------

        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
           root /usr/local/nginx-rtmp-module/;
        }

        # ------添加RTMP服务状态监控END------

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


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

}

五、推流、拉流验证

这里使用OBS进行推流、VLC播放器进行拉流。

1. RTMP流

配置OBS推流地址,开始推流

2020-10-01 201836.png

2020-10-01 202016.png

查看状态:

2020-10-01 203025.png

在VLC中填写拉流地址进行拉流

2020-10-01 202358.png

已经成功拉流

2020-10-01 202518.png

2. HLS流

2020-10-01 211556.png

2020-10-01 211644.png

服务器上的切片文件,切片文件存在说明画面已经成功推送到服务器

2020-10-01 212143.png

使用HTTP协议拉流(.m3u8文件链接)

2020-10-01 211901.png

已经成功拉流

2020-10-01 212552.png

如果想在网页中拉流,可以使用video.js来实现。

推流、拉流的鉴权主要是后端逻辑,这里不过多赘述。

标签: Nginx, nginx-rtmp-module, RTMP, HLS, 直播, 流服务器

添加新评论