问题
nginx-rtmp-modul是基于nginx实现的流媒体服务器,如果系统运行后需要修改配置参数,就需要修改nginx.conf后调用命令nginx -s reload
,这种方式对于输入HLS没有问题,但是如果是输入RTMP(push),就会导致reload前的播放可以继续播放,但是新的client无法播放RTMP流。
注意:输入如果是RTMP(pull)是否存在这种问题,没有测试
原因
nginx官网对relaod命令的解释:
Old worker processes, receiving a command to shut down, stop accepting new connections and continue to service current requests until all such requests are serviced. After that, the old worker processes exit.
reload以后nginx作为server继续为push client服务,等待push client主动关闭连接;push client也不会主动关闭连接,导致新的play client无法获取到RTMP流。
解决方案
参考:https://github.com/arut/nginx-rtmp-module/wiki/Control-module#drop
nginx-rtmp-modul存在Control modul,其中的Drop命令可以关闭连接:
This sub-module provides a simple way to drop client connection.
The first method drop/publisher drops publisher connection.
增加配置:
http {
...
server {
listen 8080;
server_name localhost;
....
location /control {
rtmp_control all;
}
}
}
reload命令修改为:
curl http://localhost/control/drop/publisher?app=myapp && nginx -s reload
以上方法从理论上分析,并不是百分百可靠:如果push client重连的速度足够快,还没有来的急执行reload命令,就会导致出现问题:drop命令再也无法关闭这个连接。
这个时候就需要直接关闭相应的worker进程;获取进行信息:
root@67fb50be9071:/usr/local/nginx# ps aux | grep nginx
root 7656 0.0 0.0 40644 5356 ? Ss 05:50 0:00 nginx: master process ./sbin/nginx
nobody 7660 0.0 0.0 42240 6324 ? S 05:52 0:03 nginx: worker process is shutting down
nobody 7683 0.0 0.0 41056 5784 ? S 07:49 0:00 nginx: worker process
nobody 7684 0.0 0.0 40844 5316 ? S 07:49 0:00 nginx: cache manager process
发现PID为7660的进程正在"shutting down",执行命令:
kill 7660
push client的连接被关闭了