1.下载nginx (/etc 下)
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依赖库(zlib Home Site)
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.解压编译 (/etc 下)
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
需先创建目录
项目保存到 /var/www/html
视频流保存到 /usr/local/sbin/www/live
6.配置nginx
sudo vim /usr/local/nginx/conf/nginx.conf
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
rtmp {
server{
listen 1938;
application live{
live on;
}
application hls{
live on;
hls on;
hls_path /usr/local/sbin/www/live;
hls_fragment 3s;
record off;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
root /var/www/html;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /hls{
add_header Access-Control-Allow-Origin *;
types{
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /usr/local/sbin/www/live;
expires -1;
add_header Cache-Control no-cache;
location ~* .(m3u8)$ {
secure_link $arg_md5,$arg_expires;
secure_link_md5 '$secure_link_expires fchsoft';
# 空字符串,校验不通过
if ($secure_link = "") {
return 403;
}
# 时间过期
if ($secure_link = "0") {
return 410 "URL过期,请重新生成";
}
}
}
location /play/ {
proxy_pass http://127.0.0.1:8008/index/;
}
#location ~*\.(png){
#valid_referers none blocked 10.10.3.151;
#if ($invalid_referer){
# return 403;
# }
#root /var/www/html;
#}
location ~* .(mp4)$ {
secure_link $arg_md5,$arg_expires;
secure_link_md5 '$secure_link_expires fchsoft';
# 空字符串,校验不通过
if ($secure_link = "") {
return 403;
}
# 时间过期
if ($secure_link = "0") {
return 410 "URL过期,请重新生成";
}
#alias /home/zfjia/ZLMediaKit/release/linux/Debug/www/live;
root /var/www/html/video;
}
location /ws {
proxy_pass http://127.0.0.1:8088;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
}
设置nginx 开机自启 systemctl enable nginx.service
7.重启服务
sudo systemctl restart nginx.service
8.mqtt测试
mosquitto_sub -h localhost -p 1883 -u test -P 123456 -t /#