基于Nginx部署直播推流服务器
基于Nginx部署直播推流服务器
1:环境
什么是RTMP:
RTMP: RTMP协议是Real Time Message Protocol(实时信息传输协议)的缩写,它是由Adobe公司提出的一种应用层的协议,用来解决多媒体数据传输流的多路复用(Multiplexing)和分包(packetizing)的问题
Nginx: 服务器
nginx-rtmp-module: 基于Nginx的流媒体Server
HLS:苹果出的一套Http Live Streaming协议,它的工作原理简单来说就是把一段视频流,分成一个个小的基于HTTP的文件来下载。当媒体流正在播放时,客户端可以根据当前网络环境,方便地在不同的码率流中做切换,以实现更好的观影体验。
2:部署Nginx与RTMP
[root@nginx-rtmp ~]# wget https://nginx.org/download/nginx-1.21.6.tar.gz --no-check-certificate
[root@nginx-rtmp ~]# ls
anaconda-ks.cfg nginx-1.21.6.tar.gz nginx-rtmp-module
# 安装nginx所需软件包
[root@nginx-rtmp ~]# yum -y install pcre pcre-devel openssl openssl-devel zlib-devel gcc
# 解压nginx并安装
[root@nginx-rtmp ~]# tar xf nginx-1.21.6.tar.gz
[root@nginx-rtmp ~]# cd nginx-1.21.6/
[root@nginx-rtmp nginx-1.21.6]# ./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module
[root@nginx-rtmp nginx-1.21.6]# echo $?
0
[root@nginx-rtmp nginx-1.21.6]# make && make install
[root@nginx-rtmp nginx-1.21.6]# echo $?
0
# 配置Nginx
[root@nginx-rtmp nginx-1.21.6]# cat /usr/local/nginx/conf/nginx.conf | grep -v "^#"
[root@nginx-rtmp nginx-1.21.6]# cat /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
chunk_size 4000;
application live {
live on;
hls on;
hls_path /usr/local/nginx/html/live;
hls_fragment 5s;
}
}
}
http {
server {
listen 80;
location /{
root /usr/share/nginx/html;
index index.html index.htm;
}
}
}
# 注意:hls所在目录nginx的用户必须有写入权限。
[root@nginx-rtmp nginx-1.21.6]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx-rtmp nginx-1.21.6]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successfu
3:启动Nginx测试推流
[root@nginx-rtmp nginx-1.21.6]# ss -lnt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:1935 *:*
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:80 *:*
# 下载OBS
下载地址:https://obsproject.com/
[root@nginx-rtmp live]# ls
aaa-35.ts aaa-37.ts aaa-39.ts aaa-41.ts aaa-43.ts aaa-45.ts aaa.m3u8
aaa-36.ts aaa-38.ts aaa-40.ts aaa-42.ts aaa-44.ts aaa-46.ts
# 有数据流进来
可以就可以使用拉流软件拉取来播放了