Nginx简单实现直播服务器:

编译安装Nginx:

系统:CentOS-7.5
软件:Nginx-1.14.2

IP地址:172.18.1.99

下载源码包:点击下载

安装依赖包:
[root@nginx ~]# yum -y install pcre* openssl-devel git
创建目录:
[root@nginx ~]# mkdir /home/tools && cd /home/tools
git拉取nginx-rtmp插件:
[root@nginx tools]# git clone https://github.com/arut/nginx-rtmp-module.git
[root@nginx tools]# ll -sh
total 4.0K
4.0K drwxr-xr-x 7 root root 4.0K Dec 12 13:53 nginx-rtmp-module
创建用户:
[root@nginx tools]# useradd nginx -s /sbin/nologin -M
预编译:
[root@nginx tools]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@nginx tools]# tar zxf nginx-1.14.2.tar.gz && cd nginx-1.14.2/
[root@nginx nginx-1.14.2]# ./configure --prefix=/usr/local/nginx-1.14.2 \
--with-http_ssl_module --with-http_stub_status_module \
--with-http_stub_status_module --with-pcre \
--with-http_realip_module \
--add-module=../nginx-rtmp-module
编译安装:
[root@nginx nginx-1.14.2]# make && make install && echo $?
[root@nginx nginx-1.14.2]# ln -s /usr/local/nginx-1.14.2 /usr/local/nginx
修改Nginx配置文件:
[root@nginx nginx-1.14.2]# cd && vim /usr/local/nginx/conf/nginx.conf

user nginx;
worker_processes auto;
events {
worker_connections 1024;

}

rtmp {

    server {
        listen 1935;
        chunk_size 4096;
        application live {
             live on;
             hls on;
             hls_path /usr/local/nginx/html/live;
             hls_fragment 5s;
        }
    }
}

http {

    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;

    server {

        listen 80;
        server_name localhost;
        location /live {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }

            alias /usr/local/nginx/html/live;
            expires -1;
            add_header Cache-Control no-cache;
        }

        location / {
            root /usr/local/nginx/html;
            index index.html index.htm;
        }
    }
}
加入systemd管理:

vim /etc/systemd/system/nginx.service

[Unit]
Description=nginx server daemon
Documentation=man:nginx(8)
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

systemctl daemon-reload

启动Nginx服务:
[root@nginx ~]# systemctl start nginx && systemctl enable nginx
[root@nginx ~]# netstat -tunpl |grep nginx
tcp        0      0 0.0.0.0:1935            0.0.0.0:*               LISTEN      16433/nginx: master 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      16433/nginx: master
配置站点首页:
[root@nginx ~]# vim /usr/local/nginx/html/index.html

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>前端播放m3u8格式视频</title>
<link rel="stylesheet" href="http://vjs.zencdn.net/5.5.3/video-js.css">
<script src="http://vjs.zencdn.net/5.5.3/video.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/videojs-contrib-hls/5.12.2/videojs-contrib-hls.js"></script>
</head>
<body>
<video id="myVideo" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" width="1080" height="708" data-setup='{}'>
<source id="source" src="http://172.18.1.99/live/123456.m3u8" type="application/x-mpegURL">
</video>
</body>
<script>
// videojs 简单使用
var myVideo = videojs('myVideo',{
bigPlayButton : true,
textTrackDisplay : false,
posterImage: false,
errorDisplay : false,
})
myVideo.play() // 视频播放
myVideo.pause() // 视频暂停
</script>
</html>

使用EV录屏实现推流:

设置EV录屏:
开始直播录屏:
浏览器访问: