前言与背景介绍
前言
近年来直播行业蓬勃发展,涵盖传统行业、电商购物、网络游戏及在线教育等多个领域。对于长期深耕互联网的技术人员而言,掌握利用Nginx构建直播环境的技能显得尤为重要。本文将详细演示如何从零开始搭建一套基于Nginx的直播服务系统。
Nginx环境安装
本教程以CentOS 6.8操作系统为例,所有操作均需在root用户权限下执行。
第一步:安装基础依赖环境
yum -y install wget gcc-c++ ncurses ncurses-devel cmake make perl bison openssl openssl-devel gcc* libxml2 libxml2-devel curl-devel libjpeg*
libpng* freetype*
autoconf automake zlib*
fiex* libxml*
libmcrypt*
libtool-ltdl-devel*
libaio libaio-devel
bzr libtool
第二步:安装OpenSSL库
wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz
tar -zxvf openssl-1.0.2s.tar.gz
cd /usr/local/src/openssl-1.0.2s
./config --prefix=/usr/local/openssl-1.0.2s
make
make install
第三步:安装PCRE正则表达式库
wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
tar -zxvf pcre-8.43.tar.gz
cd /usr/local/src/pcre-8.43
./configure --prefix=/usr/local/pcre-8.43
make
make install
第四步:安装Zlib压缩库
wget https://sourceforge.net/projects/libpng/files/zlib/1.2.11/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd /usr/local/src/zlib-1.2.11
./configure --prefix=/usr/local/zlib-1.2.11
make
make
第五步:获取nginx-rtmp-module插件
该模块的官方GitHub仓库地址为:https://github.com/arut/nginx-rtmp-module
请使用以下命令克隆模块代码:
git clone https://github.com/arut/nginx-rtmp-module.git
第六步:编译安装Nginx
wget http://nginx.org/download/nginx-1.19.1.tar.gz
tar -zxvf nginx-1.19.1.tar.gz
cd /usr/local/src/nginx-1.19.1
./configure --prefix=/usr/local/nginx-1.19.1 --with-openssl=/usr/local/src/openssl-1.0.2s --with-pcre=/usr/local/src/pcre-8.43 --with-zlib=/usr/local/src/zlib-1.2.11 --add-module=/usr/local/src/nginx-rtmp-module --with-http_ssl_module
make
make install
在编译Nginx时,必须正确指向之前安装的openssl、pcre和zlib的源码解压目录。安装成功后,Nginx的主配置文件位于:/usr/local/nginx-1.19.1/conf/nginx.conf。
Nginx服务配置
核心配置工作集中在修改nginx.conf文件,可通过命令行工具编辑该配置文件。
vim /usr/local/nginx-1.19.1/conf/nginx.conf
在配置文件中添加如下RTMP相关配置段:
rtmp {
server {
listen 1935; #监听的端口
chunk_size 4096;
application hls { #rtmp推流请求路径
live on;
hls on;
hls_path /usr/share/nginx/html/hls;
hls_fragment 5s;
}
}
}
注意,hls_path指定的目录必须具备可读可写权限。随后创建目录/usr/share/nginx/html/hls并赋予相应权限。
mkdir -p /usr/share/nginx/html/hls
chmod -R 777 /usr/share/nginx/html/hls
接着,修改HTTP部分的server模块配置:
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
配置完成后,启动Nginx服务:
/usr/local/nginx-1.19.1/sbin/nginx -c /usr/local/nginx-1.19.1/conf/nginx.conf
使用OBS进行推流
OBS(Open Broadcaster Software)是一款免费且开源的互联网流媒体直播软件。需下载并安装该软件以完成推流操作(若电脑无摄像头可能无法使用部分功能)。
OBS官方下载地址为:https://obsproject.com/zh-cn/download。
安装完成后,桌面上会出现相应的快捷图标。

打开软件后,需要创建一个场景,并在场景中设置来源(如窗口捕获或视频采集设备,后者会自动识别摄像头),随后进入参数设置环节。

在设置界面中,重点配置“流”选项。由于使用的是自建服务器,请按如下方式填写服务器地址等信息。
rtmp://你的服务器ip:端口(1935)/live #URL填写流的地址

设置完毕后,即可点击“开始推流”按钮。
拉流测试地址
推荐使用在线测试平台验证拉流效果,该平台支持多种协议测试。因我们使用RTMP协议,请在对应栏目选择RTMP,并在下方填入推流地址及OBS中配置的流名称,点击Start即可验证。

至此,关于使用Nginx搭建流媒体服务器实现直播功能的介绍已完成。如需了解更多Nginx搭建流媒体服务器的相关内容,请搜索本站历史文章或继续浏览后续章节,感谢大家的支持!