基于Nginx搭建RTMP-HLS视频直播服务器

基于Nginx搭建RTMP-HLS视频直播服务器

一、环境准备

1.1.1 系统准备

[root@node ~]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)```

1.1.2 环境依赖准备

[root@node ~]# yum install git gcc gcc-c++ openssl openssl-devel pcre pcre-devel zlib zlib-devel -y

二、软件安装

2.1.1 Git nginx-rtmp-module项目到本地 安装Nginx的rtmp协议的扩展(用于推流)

[root@node ~]# cd /usr/local/
[root@node /usr/local]# git clone https://github.com/arut/nginx-rtmp-module.git
Cloning into 'nginx-rtmp-module'...
remote: Enumerating objects: 4314, done.
remote: Total 4314 (delta 0), reused 0 (delta 0), pack-reused 4314
Receiving objects: 100% (4314/4314), 3.10 MiB | 289.00 KiB/s, done.
Resolving deltas: 100% (2686/2686), done.
[root@node /usr/local]# ll
total 4
drwxr-xr-x. 2 root root    6 Apr 11  2018 bin
drwxr-xr-x. 2 root root    6 Apr 11  2018 etc
drwxr-xr-x. 2 root root    6 Apr 11  2018 games
drwxr-xr-x. 2 root root    6 Apr 11  2018 include
drwxr-xr-x. 2 root root    6 Apr 11  2018 lib
drwxr-xr-x. 2 root root    6 Apr 11  2018 lib64
drwxr-xr-x. 2 root root    6 Apr 11  2018 libexec
drwxr-xr-x  7 root root 4096 May  8 11:56 nginx-rtmp-module
drwxr-xr-x. 2 root root    6 Apr 11  2018 sbin
drwxr-xr-x. 5 root root   49 Mar 26 22:35 share
drwxr-xr-x. 2 root root    6 Apr 11  2018 src

2.1.2 下载安装nginx

1. 下载nginx

下载地址:nginx.orghttp://nginx.org/download/

[root@node /usr/local]# wget http://nginx.org/download/nginx-1.15.0.tar.gz
--2020-05-08 13:10:54--  http://nginx.org/download/nginx-1.15.0.tar.gz
Resolving nginx.org (nginx.org)... 95.211.80.227, 62.210.92.35, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1020675 (997K) [application/octet-stream]
Saving to: ‘nginx-1.15.0.tar.gz’

100%[===========================================================================================>] 1,020,675   9.75KB/s   in 1m 41s 

2020-05-08 13:12:36 (9.86 KB/s) - ‘nginx-1.15.0.tar.gz’ saved [1020675/1020675]

[root@node /usr/local]# ll
total 1004
drwxr-xr-x. 2 root root       6 Apr 11  2018 bin
drwxr-xr-x. 2 root root       6 Apr 11  2018 etc
drwxr-xr-x. 2 root root       6 Apr 11  2018 games
drwxr-xr-x. 2 root root       6 Apr 11  2018 include
drwxr-xr-x. 2 root root       6 Apr 11  2018 lib
drwxr-xr-x. 2 root root       6 Apr 11  2018 lib64
drwxr-xr-x. 2 root root       6 Apr 11  2018 libexec
-rw-r--r--  1 root root 1020675 Jun  5  2018 nginx-1.15.0.tar.gz
drwxr-xr-x  7 root root    4096 May  8 11:56 nginx-rtmp-module

drwxr-xr-x. 2 root root       6 Apr 11  2018 sbin
drwxr-xr-x. 5 root root      49 Mar 26 22:35 share
drwxr-xr-x. 2 root root       6 Apr 11  2018 src

2. 解压编译安装nginx

  1. 解压
[root@node /usr/local]# tar zxvf nginx-1.15.0.tar.gz 
nginx-1.15.0/
nginx-1.15.0/auto/
nginx-1.15.0/conf/
nginx-1.15.0/contrib/
nginx-1.15.0/src/
......。
  1. 安装
[root@node /usr/local/nginx-1.15.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module \
> --with-http_stub_status_module --add-module=../nginx-rtmp-module
checking for OS
 + Linux 3.10.0-1062.18.1.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
......
[root@node /usr/local/nginx-1.15.0]# make && make install

2.1.3 创建nginx用户和组

[root@node /usr/local/nginx-1.15.0]# useradd -M -s /sbin/nologig nginx
[root@node /usr/local/nginx-1.15.0]# id nginx 
uid=1001(nginx) gid=1001(nginx) groups=1001(nginx)

2.1.4 返回上一级目录查看是否安装成功

[root@node /usr/local/nginx-1.15.0]# cd ..
[root@node /usr/local]# ls 
bin  etc  games  include  lib  lib64  libexec  nginx  nginx-1.15.0  nginx-1.15.0.tar.gz  nginx-rtmp-module  sbin  share  src
[root@node /usr/local]# cd nginx
[root@node /usr/local/nginx]# sbin/nginx -V
nginx version: nginx/1.15.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --add-module=../nginx-rtmp-module
[root@node /usr/local/nginx]# sbin/nginx 
[root@node /usr/local/nginx]# pa -ef|grep nginx
-bash: pa: command not found
[root@node /usr/local/nginx]# ps -ef|grep nginx
root      20766      1  0 14:07 ?        00:00:00 nginx: master process sbin/nginx
nginx     20767  20766  0 14:07 ?        00:00:00 nginx: worker process
root      20771   1937  0 14:07 pts/0    00:00:00 grep --color=auto nginx

三、nginx部署支持rtmp协议

3.1.2 配置nginx.conf文件

[root@node /usr/local/nginx]# vim conf/nginx.conf
#配置如下
rtmp {
      server {
          listen 1935;
          chunk_size 4096;
          application hls {
         }
          chunk_size 4096;
          application hls {   #可以写多个
              live on;
              record off;
              hls_path /usr/local/nginx/html/hls;
              hls_fragment 3s;

         }
    
   }

}

location 配置

location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
            }
            alias /usr/local/nginx/html/hls; 
            expires -1;
            add_header Cache-Control no-cache;
       } 
       location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
       }
       location /stat.xsl {
           root /usr/local/extend_module/nginx-rtmp-module/;
        }
    }
}

完整配置如下

[root@node /usr/local/nginx]# grep -Ev "#|^$" /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
rtmp { 
      server {
          listen 1935;
          chunk_size 4096;
          application hls {
              live on;
              hls on;
              hls_path /usr/local/nginx/html/hls;
              hls_fragment 3s;
         }
    
   }
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
            }
            alias /usr/local/nginx/html/hls; 
            expires -1;
            add_header Cache-Control no-cache;
       } 
       location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
       }
       location /stat.xsl {
           root /usr/local/extend_module/nginx-rtmp-module/;
        }
    }
}

创建hls目录

[root@node /usr/local/nginx]# mkdir -p html/hls

3.1.3 重新加载配置文件并启动服务

[root@node /usr/local/nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

启动服务:

[root@node /usr/local/nginx]# /usr/local/nginx/sbin/nginx
[root@node /usr/local/nginx]# ps -ef | grep nginx
root      20979   1937  0 15:44 pts/0    00:00:00 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
root      20990   1937  0 15:49 pts/0    00:00:00 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
root      21050      1  0 16:07 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     21051  21050  0 16:07 ?        00:00:00 nginx: worker process
nginx     21052  21050  0 16:07 ?        00:00:00 nginx: cache manager process

3.1.4 下载PC端推流软件OBS

官网地址:https://obsproject.com/
根据自己的需求下载不同系统版本

图片.png

3.1.5 安装 OBS

图片.png

一路安装即可

配置OBS

图片.png

图片.png

图片.png

返回nginx查看是否成功

[root@node /usr/local/nginx]# cd  html/hls/
[root@node /usr/local/nginx/html/hls]# ll
total 30440
-rw-r--r-- 1 nginx nginx 2739348 May  8 17:24 test-10.ts
-rw-r--r-- 1 nginx nginx 2741416 May  8 17:24 test-11.ts
-rw-r--r-- 1 nginx nginx 2741040 May  8 17:24 test-12.ts
-rw-r--r-- 1 nginx nginx 2743672 May  8 17:24 test-13.ts
-rw-r--r-- 1 nginx nginx 2740476 May  8 17:25 test-14.ts
-rw-r--r-- 1 nginx nginx 2740288 May  8 17:25 test-15.ts
-rw-r--r-- 1 nginx nginx 2742168 May  8 17:25 test-16.ts
-rw-r--r-- 1 nginx nginx 2741228 May  8 17:25 test-17.ts
-rw-r--r-- 1 nginx nginx  833968 May  8 17:25 test-18.ts
-rw-r--r-- 1 nginx nginx 2742544 May  8 17:24 test-7.ts
-rw-r--r-- 1 nginx nginx 2741792 May  8 17:24 test-8.ts
-rw-r--r-- 1 nginx nginx 2741040 May  8 17:24 test-9.ts
-rw-r--r-- 1 nginx nginx     331 May  8 17:25 test.m3u8

到现在苹果的MAC本和手机都可以通过浏览器观看直播了

https://ip/his/**m3u8

Windows和Android 需要写代码具体代码请访问大佬博客

https://www.jianshu.com/p/64f6aa0aad12

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。