1. 软件
- Centos: 服务器
- Nginx: 配置用于推流和拉流服务器
- OBS Studio: 用于推流
- VLC: 用于拉流
2. 效果图
推流和拉流:
服务器状态监控:
3. 搭建步骤
3.1 CentOS
本教程使用的操作系统版本 3.10.0-1160.105.1.el7.x86_64
安装基础工具
# 安装 git
yum install -y git
# 安装 gcc-c++ 编译器
yum install -y gcc-c++
# 安装 pcre pcre-devel
yum install -y pcre pcre-devel
# 安装 zlib zlib-devel
yum install -y zlib zlib-devel
# 安装 openssl openssl-devel
yum install -y openssl openssl-devel
# 如果 firewall 服务开启,请开放端口 80、1935
# 查看 firewall 是否开启
firewall-cmd --state
# 开放端口 80
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 开放端口 1935
firewall-cmd --zone=public --add-port=1935/tcp --permanent
# 重新加载配置文件
firewall-cmd --reload
3.2 nginx 相关配置
下载 nginx 源码,编译安装,并创建视频切片的存储位置
# 下载 nginx 源代码
wget https://nginx.org/download/nginx-1.24.0.tar.gz
# 解压 nginx 源代码
tar -zxvf nginx-1.24.0.tar.gz
# 切换目录到 nginx 源代码中
cd nginx-1.24.0
# 拉取 nginx-rtmp-module 代码
git clone https://github.com/arut/nginx-rtmp-module.git
# 设置配置项
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --add-module=./nginx-rtmp-module
# 编译并安装
make && make install
# 复制 stat.xsl 到 /usr/local/nginx/conf/ 用于服务状态监控
cp ./nginx-rtmp-module/stat.xsl /usr/local/nginx/conf/
# 创建视频切片的存放位置
mkdir /usr/local/nginx/html/hls
修改 nginx.conf 文件。
#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 {
listen 1935;
chunk_size 4096;
application live {
live on;
record off;
# 添加 HLS 支持
hls on;
hls_path /usr/local/nginx/html/hls;
hls_fragment 3;
hls_playlist_length 60;
}
}
}
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;
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
#注意stat.xsl文件的存放位置,支持相对路径和绝对路径。
root /usr/local/nginx/conf;
}
location /live {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /usr/local/nginx/html/hls; # 读取文件的位置,应和上面rtmp中的配置一样
expires -1;
add_header 'Cache-Control' 'no-cache';
# 添加CORS头部
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Expose-Headers' 'Content-Length';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
启动 nginx 服务
# 检查配置文件是否有错误
/usr/local/nginx/sbin/nginx -t
# 启动 nginx 服务
/usr/local/nginx/sbin/nginx
4. OBS Studio 相关配置
OBS Studio 相关设置如图: 按步骤操作,即可开始推流
服务器配置项,请使用自己 nginx 服务的 ip ,否则无法推流。
5. VLC 相关配置
VLC 播放器相关配置如图:
网络URL中,请使用自己 nginx 服务的 ip ,否则无法拉流。
6. 总结
推流地址: rtmp://192.168.3.59:1935/live
拉流地址: http://192.168.3.59/live/stream.m3u8
服务监控: http://192.168.3.59/stat
7. 参考文献
CentOS 官网
nginx 官网
OBS Studio 官网
VLC 官网