RTMP(Real Time Messaging Protocol)
实时消息传送协议是 Adobe Systems公司为Flash播放器和服务器之间音频、视频和数据传输开发的开放协议。
1. 源码编译安装nginx
1.1 下载
nginx下载:
wget http://nginx.org/download/nginx-1.24.0.tar.gz
nginx-http-flv-module下载:
https://github.com/winshining/nginx-http-flv-module
1.2 解压
tar -xvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
unzip nginx-http-flv-module-master.zip
mv nginx-http-flv-module-master /usr/local/nginx/nginx-http-flv-module-master
1.3 编译和安装
mkdir /usr/local/nginx
./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx/nginx-http-flv-module-master
make -j8 && make install
1.4 配置 nginx.conf
cd /usr/local/nginx/conf
vim nginx.conf
可以参照nginx-http-flv-module-master/test/nginx.conf
添加以下内容:
rtmp {
server {
listen 1935;
chunk_size 4096;
application myapp {
live on;
}
}
}
1.5 启动运行
cd /usr/local/nginx
./nginx
1.6 测试
通过ffmpeg
工具推送test.MP4
视频为rtmp://10.201.53.1:1935/myapp/test
视频流。
ffmpeg -re -stream_loop -1 -i test.MP4 -f flv -b:v 1000k -vcodec h264 rtmp://10.201.53.1:1935/myapp/test
2. 开机自启动
2.1 安装在物理机时采用 service 自启动方式
cd /usr/lib/systemd/system
vim nginx.service
nginx.service
内添加如下内容
# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /usr/local/nginx/logs/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
配置service
自启动
systemctl reload
service nginx start
service nginx status
2.2 安装在docker容器时的自启动方式
若在docker内编译的nginx,可将该容器重新生成镜像nginx-rtmp:v1.0
,后续用此镜像来部署环境。
docker run \
--name nginx-rtmp \
--user root \
--network host \
--privileged=true \
-v /etc/localtime:/etc/localtime \
-v /sys/fs/cgroup:/sys/fs/cgroup:ro \
-w /usr/local/nginx/sbin \
--ipc host \
--restart always \
-dit nginx-rtmp:v1.0 \
./nginx -g 'daemon off;'