基于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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,753评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,668评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,090评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,010评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,054评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,806评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,484评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,380评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,873评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,021评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,158评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,838评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,499评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,044评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,159评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,449评论 3 374
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,136评论 2 356