Nginx搭建直播服务
使用Nginx搭建RTMP直播流服务,提供直播流推送、直播播放、认证鉴权等功能
协议
三大直播协议
RTMP、HTTP-FLV、HLS,RTMP是Adobe公司为Flash播放器开发的私有直播协议,FLV是Adobe公司推出的另一种视频格式,而HTTP-FLV就是将流媒体数据封装成FLV格式并通过HTTP协议传输给客户端,而HLS则是Apple公司基于HTTP协议开发的流媒体传输协议,它会在服务器端将流媒体数据切割成连续的时长较短的ts小文件,并通过m3u8索引文件按序访问ts文件,而客户端只需要按序从服务器上获取ts文件来播放即可。一般RTMP在直播推流中用的多,而HLS则比较适合来播放视频用,但其会生成海量ts小文件,从而造成存储困难,所以一般使用云存储服务(七牛、阿里云等)比较好。
参考
安装
https://github.com/arut/nginx-rtmp-module
# 系统为 CentOS-7 ,使用 YUM 安装了 Nginx,为了添加 nginx-rtmp-module 模块,所以需要重新编译安装
# 通过 `nginx -V` 命令查看其原始编译信息,重点是其已安装的模块,后面编译安装时这些模块一并装上
$ nginx -V
nginx version: nginx/1.16.1
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
... ...
# http://nginx.org/en/docs/install.html
# http://nginx.org/en/docs/configure.html
# 下载源码,版本与当前使用 nginx 版本一致
$ curl -OL http://nginx.org/download/nginx-1.16.1.tar.gz
$ tar zxvf nginx-1.16.1.tar.gz && cd nginx-1.16.1
# 下载(克隆) nginx-rtmp-module 源码
# https://www.nginx.com/resources/wiki/modules/index.html
# https://github.com/arut/nginx-rtmp-module
$ curl -OL https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.tar.gz
$ tar zxvf v1.2.1.tar.gz
# YUM安装编译工具链
# 搜索可用工具包
$ yum grouplist | more
# 找到 "Development Tools" 这个工具包,编译构建 nginx 依赖工具比较多,这样一次安装到位比较简单
$ yum groupinstall Development Tools
# 安装其它依赖工具
$ yum install pcre-devel openssl-devel perl-devel perl-ExtUtils-Embed libxml2-devel libxslt-devel gd-devel
# 默认安装模块中包含 ngx_google_perftools_module 模块,安装略麻烦,这里先略过
# http://nginx.org/en/docs/ngx_google_perftools_module.html
# https://github.com/gperftools/gperftools/releases/
# https://www.cnblogs.com/91donkey/p/11513175.html
# 另外 the "--with-ipv6" option is deprecated,所以这个模块也移除掉
# 重新编译安装,加装 nginx-rtmp-module 模块
$ ./configure \
--prefix=/usr/share/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body \
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi \
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi \
--pid-path=/run/nginx.pid \
--lock-path=/run/lock/subsys/nginx \
--user=nginx \
--group=nginx \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-stream_ssl_preread_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-http_perl_module=dynamic \
--with-http_auth_request_module \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-pcre --with-pcre-jit \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-debug \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' \
--with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' \
--add-module=./nginx-rtmp-module-1.2.1
# 编译构建,注意不要执行 `make install`,这里采用平滑更新试
$ make -j2
# 备份、平滑升级
$ mv /usr/sbin/nginx /usr/sbin/nginx.old
$ cp objs/nginx /usr/sbin/
# 平滑升级实际是一组操作打包成一个命令,具体细节查看命令输出日志即可
$ make upgrade
/usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
kill -USR2 `cat /run/nginx.pid`
sleep 1
test -f /run/nginx.pid.oldbin
kill -QUIT `cat /run/nginx.pid.oldbin`
# 升级完成后可通过 `nginx -V` 再次查看是否安装成功
# 后面测试RTMP服务端口10000,所以提前加入防火墙
$ firewall-cmd --add-port=10000/tcp --permanent
配置
https://github.com/arut/nginx-rtmp-module
推流配置(直播流服务)
rtmp {
server {
listen 10000;
chunk_size 4000;
# 直播 | 播放 (不记录数据)
application live {
live on;
record off;
}
# 直播 | 播放 (记录数据,但实测并未真的记录,原因未知)
application rtmp {
# enable live streaming
live on;
# record first 1K of stream
record all;
record_path /data/live/rtmp;
record_max_size 1K;
# append current timestamp to each flv
record_unique on;
# publish only from localhost
# allow publish 127.0.0.1;
# deny publish all;
allow play all;
}
# 直播 | 播放,在http区段中的配置可以作为回放来观看
application hls {
live on;
hls on;
hls_path /data/live/hls;
hls_fragment 5s;
hls_playlist_length 15s;
hls_continuous on;
hls_cleanup on;
hls_nested on;
}
# 点播,目录下要有视频文件
application video {
play /data/live/video;
}
}
}
播放配置(hls)
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name dev.zlikun.com;
include /etc/nginx/default.d/*.conf;
# HLS协议直播播放地址,文件目录:/data/live/hls ,实际测试播放不了,原因未知
# https://dev.zlikun.com/hls/index.m3u8
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /data/live;
add_header Cache-Control no-cache;
}
# 模块 nginx-rtmp-module 源码提供了一些示例,可以这里配置使用
# https://dev.zlikun.com/stat
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/share/nginx/nginx-rtmp-module-1.2.1/;
}
}
参考
推流
# 测试推流地址:rtmp://dev.zlikun.com:10000/live
# 测试播放地址:rtmp://dev.zlikun.com:10000/live
鉴权
播放
分发
转码
转发
转发第三方RTMP直播流,比如推给七牛云的直播流,经过Nginx转发,从中添加自定义鉴权逻辑