nginx之流式服务器
nginx安装回顾
安装编译工具及库文件(zlib、pcre、openssl…)
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
安装 PCRE(废话有点多)
- 下载 PCRE 安装包
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
- 解压安装包
tar zxvf pcre-8.35.tar.gz
- 进入安装包目录,编译安装
cd pcre-8.35
./configure
make && make install
注:
pcre
https://ftp.pcre.org/pub/pcre/pcre-8.35.tar.gz
https://ftp.pcre.org/pub/pcre/pcre2-10.34.tar.gz
OpenSSL(编译./config --prefix=/usr/local/openssl 不使用./configure)
https://www.openssl.org/source/openssl-1.1.1d.tar.gz
nginx-rtmp-module安装
# https://codeload.github.com/arut/nginx-rtmp-module/zip/master
unzip unzip master
# 解压保存源码包即可不需要其它操作,后编译nginx时需要用到
nginx编译注意
./configure --prefix=/path/to/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/path/to/pcre-8.35 --add-module=/path/to/nginx-rtmp-module-master
nginx配置(重点在这)
# rtmp配置
rtmp {
server {
listen 1935; # 一般监听1935
chunk_size 4096; # 看着来
# rtmp 直播流配置
# application rtmplive { # rtmplive -> live
# live on;
# max_connections 1024 # rtmp 引擎最大连接数 默认off
# }
# hls 直播流配置(这里的配置联系到m3u8知识,详情可以百度,此处不赘述)
application hls{
live on;
hls on;
hls_path /path/to/nginx/html/hls; # 自己配就行,随意
hls_fragment 5s;
hls_playlist_length 15s; # m3u8文件中指明的ts文件分段时长
hls_continuous on; # 连续模式
hls_cleanup on; #对多余的切片进行删除
hls_nested on; #嵌套模式
}
}
}
# http/server中添加配置
location /hls { #添加视频流存放地址。
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
#访问权限开启,否则访问这个地址会报403
autoindex on;
alias /path/to/nginx/html/hls; #视频流存放地址,与上面的hls_path相对应,root和alias区别百度(可以都试试)
expires -1;
add_header Cache-Control no-cache;
# 跨域配置(到目前为止博主尚未解决flv.js的跨域问题,但是RN、Android可以)
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
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';
if ($request_method = 'OPTIONS') {
return 204;
}
}
好了,可以启动你的nginx了
附(关于ffmpeg我当做读者已经会用了,不会的来这儿->ffmpeg来了)
ffmpeg推拉流测试
推流:ffmpeg -re -i /path/to/file.mp4 -vcodec copy - acodec copy -f flv rtmp://192.168.31.50:1935/hls
拉流:
rtmp://192.168.31.50:1935/live/test
查看流信息:
http://192.168.31.50/stat
写在最后
欢迎留言私信讨论;
文章有知识性错误请立马联系博主,博主将非常感谢;
无需经过允许即可随意使用转载,知识本来就是被广泛用来学习的;
非常感谢您能看到此处,本文为博主学习笔记,如有不同见解,请不吝赐教。