The article "Let your nginx rtmp live broadcast have the function of counting the number of online viewers of a channel" describes the online statistics module of nginx.
Our online live broadcast service uses the nginx rtmp module (see nginx rtmp module nginx-rtmp-Module Instructions for details). In general, this module provides excellent functional stability and performance. As long as you have no problem with the data supply of the live stream source, no problem will be available for nginx live broadcast.
For idle channels, it is a live video channel that no one is watching. To save system resources, we cleaned it regularly. How can we determine that a channel is idle? At first, we used the method described in "Let your nginx rtmp live broadcast have the function of counting the number of online viewers of a channel". Java uses Apache HTTP client to call nginx nclients, that is
Http: // Live Video Server IP/nclients? APP = app name & name = channel name. If the result is 0, the channel is idle.
However, in the actual environment, we found that this online statistics function is not reliable. Sometimes, when someone is playing a channel, it is counted as 0. In this way, statistical errors are caused, and the channels that should not be cleared are cleared, seriously affecting the stability of the live video effect.
At first, we thought we had not configured nginx, but the recheck and repeated deployment were still the case.
Once, we used http: // Live Video Server IP Address/STAT to directly track online statistics of live video channels on pages, and found that the probability of normal statistics is more than 80%. However, the 20% statistical result of the error is enough for our cleaner to make a fatal error. We can't help wondering whether nginx's online statistics function is reliable or not?
After verification by multiple parties, including authoritative confirmation by the author of the rtmp module, we finally come to the conclusion that this is indeed a bug in nginx.
It turns out that the nginx stat does not support concurrency in the Multi-worker mode well.
The official website provides two solutions: Enable nginx to work in single-worker mode, or configure a separate stat file for each nginx worker.
The first method has a significant impact on the performance provided by nginx, especially for multi-core servers. The second method does not officially describe how to configure it separately.
Based on the actual situation, we did not use these two methods, but managed to maintain the number of connections for each live video channel. Of course, this is not the best way, or we hope nginx can support stat in Multi-worker mode as soon as possible.
For more information, see https://github.com/arut/nginx-rtmp-module/issues/159.