注:Nginx服务器默认安装完成,只添加nginx-rtmp-module模块和nginx-http-flv-module模块,如果没有安装nginx服务,请参考先安装nginx
下载nginx所需要的模块下载地址
所有准备工作做完之后开始安装
第一步:
将nginx模块解压缩后,上传到服务器,记住这个路径
第二步:
通过命令进入到nginx资源目录下,就是后缀为.tar.gz解压出来的文件
第三步:
执行命令
# =后面的地址就是nginx模块解压缩后上传到服务器的地址
./configure --add-module=/opt/rh/nginx-rtmp-module
make && make install
./configure --add-module=/opt/rh/nginx-http-flv-module
make && make install
# 注:这里我试过两个模块一起编译,但是在安装的时候出错,两个模块分开后就没有这个问题,具体情况我也没有深入研究。。将就用吧,麻烦一下子
执行完之后,会看到添加了rtmp和http-flv-live模块。
第四步:
启动nginx并修改文件
[root@home nginx-1.18.0]# whereis nginx
nginx: /usr/local/nginx
[root@home nginx-1.18.0]# /usr/local/nginx/sbin/nginx
编辑配置文件
[root@home nginx-1.18.0]# vi /usr/local/nginx/conf/nginx.conf
#添加下面内容
rtmp {
server {
listen 1935;
chunk_size 4000;
application live {
live on;
hls on;
hls_path /usr/local/nginx/html/live;
hls_fragment 10s; #每个TS文件包含10秒的视频内容
}
}
}
# 这里是支持http拉流的配置
server {
listen 82;
server_name rtmpserver;
location /test {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /usr/local/nginx/html/live; # 这里的地址与上面rtmp的hls_path一致
expires -1;
add_header 'Cache-Control' 'no-cache';
}
}
完整config内容
worker_processes 1;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
chunk_size 4000;
application live {
live on;
hls on;
hls_path /usr/local/nginx/html/live;
hls_fragment 10s; #每个TS文件包含10秒的视频内容
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 82;
server_name rtmpserver;
location /test {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /usr/local/nginx/html/live; # 这里的地址与上面rtmp的hls_path一致
expires -1;
add_header 'Cache-Control' 'no-cache';
}
}
server {
listen 8100;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
第五步:
重启nginx服务器
[root@home nginx-1.18.0]# /usr/local/nginx/sbin/nginx -s stop
[root@home nginx-1.18.0]# /usr/local/nginx/sbin/nginx
重启完成后,会在配置的地址下面看到多了一个文件夹
第六步:
视频推流
通过obs将视频推到服务器上面
推流成功之后会看到这样一些文件
第七步:
测试拉流
http拉流地址:http://192.168.0.123:82/test/mq.m3u8
rtmp拉流地址:rtmp://192.168.0.123:1935/live/mq
注:如果测试不成功,请查看一下防火墙状态,如果没有放行端口,请关闭防火墙在重试
systectl status firewalld
systectl stop firewalld
# 开启防火墙
systectl start firewalld