centos使用nginx搭建mp4、flv服务器
centos使用nginx搭建mp4、flv服务器
本文的主要目的是,在centos下,使用yum工具来安装nginx,并搭配安装相关的模块,使其能在线播放视频。(提供https、视频预览)等。
nginx实现rtmp,flv,mp4流媒体服务器 主要参考这篇。
https://www.cnblogs.com/maidongdong/p/8631258.html 这一篇带关键帧的讲解。或者 https://www.cnblogs.com/welcomer/p/3766815.html
安装过程
安装一些依赖的库
yum install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel
下载并解压
wget http://nginx.org/download/nginx-1.17.6.tar.gz
tar zxf nginx-1.17.6.tar.gz
cd nginx-1.17.6
执行安装命令
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_mp4_module
make && make install
备注:之前不小心,\写成了 && \ ,导致命令不识别。
另外,有一个模块--with-http_stub_status_module 并没有安装,这是一个nginx的监控模块。用法如下:
在nginx.conf的server块中添加如下代码
location /nginx_status {
# Turn on nginx stats
stub_status on;
# I do not need logs for stats
access_log off;
# Security: Only allow access from 192.168.1.100 IP #
#allow 192.168.1.100;
# Send rest of the world to /dev/null #
#deny all;
}
这段代码是加在默认的server里的,假设默认server的配置为
listen 127.0.0.1:80;
server_name 127.0.0.1;
那么访问nginx的状态,就可以通过 curl 127.0.0.1/nginx_status访问了,返回结果类似于:
Active connections: 1
server accepts handled requests
655 655 1985
Reading: 0 Writing: 1 Waiting: 0
以上,参考编译安装 nginx的http_stub_status_module监控其运行状态
SSL
配置,参考这篇文章:Nginx下配置Https证书详细过程
如下,执行,然后输出对应的参数,即可。当然,正规的都是要申请的,用自己生成的,会提示不安全。
openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /usr/local/nginx/ssl/nginx.key -out /usr/local/nginx/ssl/nginx.crt
配置
以下为一个参照配置,将mp4目录直接由nginx处理,其他的请求,交给nodejs来响应。
server {
listen 443 ssl;
#server_name www.yourdomain.com;
ssl_certificate /usr/local/nginx/ssl/nginx.crt;
ssl_certificate_key /usr/local/nginx/ssl/nginx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
#禁止在header中出现服务器版本,防止黑客利用版本漏洞攻击
server_tokens off;
#如果是全站 HTTPS 并且不考虑 HTTP 的话,可以加入 HSTS 告诉你的浏览器本网站全站加密,并且强制用 HTTPS 访问
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
access_log /usr/local/nginx/logs/httpsaccess.log;
# /root
root /home/movie/;
location /mp4 {
index index.html index.htm;
}
location /{
proxy_pass http://127.0.0.1:55055;
}
}
强制https访问,配置如下:
server {
listen 80;
server_name www.yourdomain.com;
rewrite ^(.*) https://$server_name$1 permanent; #http 跳转 https
}
问题
1、设置好后,执行nginx -t说配置没有问题,但是就是无法请求到对应的资源。后发现,/root/movie/没有目录权限,而nginx执行的用户组为nobody。另外,如果顶级目录,没有权限,那么子目录,就算有权限也无法访问。