1.下载nginx
wget http://nginx.org/download/nginx-1.14.0.tar.gz
2.安装依赖
// gcc、g++依赖库
sudo apt-get install build-essential
sudo apt-get install libtool
// pcre依赖库(http://www.pcre.org/)
sudo apt-get install libpcre3 libpcre3-dev
// zlib依赖库(http://www.zlib.net)
sudo apt-get install zlib1g-dev
// ssl依赖库
sudo apt-get install openssl libssl-dev
3.下载rtsp扩展
sudo mkdir module && cd module
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
unzip master.zip
4.解压编译
sudo tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0/
sudo ./configure --prefix=/usr/local/nginx/ --add-module=/etc/module/nginx-rtmp-module-master --with-http_ssl_module --with-http_secure_link_module
sudo make
sudo make install
5.创建系统服务文件
sudo vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
开启自启
systemctl enable nginx.service
6.配置nginx
sudo vim /usr/local/nginx/conf/nginx.conf
增加
user root
在http同级增加
rtmp{
server{
listen 1935;
application live{
live on;
}
application hls{
live on;
hls on;
hls_path /home/hls/test;
hls_fragment 3s;
record off;
}
}
}
防盗链在http的server里增加
location ~* .(png)$ {
secure_link $arg_md5,$arg_expires;
secure_link_md5 "$uri ggg";
# 空字符串,校验不通过
if ($secure_link = "") {
return 403;
}
# 时间过期
if ($secure_link = "0") {
return 410 "URL过期,请重新生成";
}
root /var/www/html;
}
说明:
用命令生成md5值
echo -n '/video/1.png ggg' | openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =
参数说明
/video/1.png 文件的url地址
ggg 加密验证字符
链接地址
http://127.0.0.1/video/1.png?md5=4EYn_a-LxAASjZoqa9z0NA&expires=1669097700
参数说明
md5 上一步生成的
expires 过期时间
如果是hls,在http的server里增加
location /hls{
add_header Access-Control-Allow-Origin *;
types{
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /home/hls/test/;
expires -1;
add_header Cache-Control no-cache;
}
7.重启服务
sudo systemctl restart nginx.service
8.安装ffmpeg
sudo apt-get install ffmpeg
9.ffmpeg推流
rtsp转rtmp
sudo ffmpeg -rtsp_transport tcp -i "rtsp://admin:[email protected]:554/h264/ch1/main/av_stream" -f flv -r 25 -s 1920*1080 -an "rtmp://10.10.3.180:1935/live/test"
rtsp转hls
sudo ffmpeg -rtsp_transport tcp -i "rtsp://admin:[email protected]:554/h264/ch9/main/av_stream" -c copy -f hls -hls_time 3.0 -hls_list_size 2 -hls_wrap 1200 /home/hls/test/test.m3u8
mp4转hls
sudo ffmpeg -i "http://10.10.3.71:8906/1.mp4" -c:v libx264 -c:a aac -strict -2 -hls_time 4 -hls_list_size 6 -hls_wrap 4 -f hls /home/hls/test/test.m3u8
10.网页访问
demo