If your Nginx already has RTMP live broadcast function, if you want to count the number of users currently watching a live channel, you can add the with-http_xslt_module module. The specific steps are as follows:
1. View the original parameters
/usr/local/nginx/sbin/nginx -V
output to get the parameters that were originally compiled with, for example, the author gets:
--user=nginx --group=nginx -- with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --with-http_flv_module --with-http_perl_module --with-mail
These parameters are still useful when we install new modules.
2. Download the nginx-rtmp-module installation package
nginx-rtmp-module-master.zip, the latest download address: https://github.com/arut/nginx-rtmp-module .
After downloading, unzip it to get the nginx-rtmp-module-master directory.
3. Download the nginx-1.3.8.tar.gz package and find the version you need
at http://nginx.org/download/ .
After downloading, unzip it to get the nginx-1.3.8 directory.
4. Close nginx
ps - ef | grep nginx
finds the master process in the process list, which is the master process number of nginx.
kill -TERM The main process number
nginx is shut down.
5. Install other dependencies
yum install pcre-devel
yum install openssl-devel
yum install perl-devel perl-ExtUtils-Embed
yum install gcc
yum install libxml2 libxml2-devel libxslt libxslt-devel
6. Compile the with-http_xslt_module module
to get it in step 1 Add the following parameters after a series of parameters:
--with-http_xslt_module --add-module=/home/defonds/nginx-rtmp-module-master
where /home/defonds/nginx-rtmp-module-master is obtained in step 2 content.
Switch to the nginx-1.3.8 directory obtained in step 3, and reconfigure using the parameter list obtained by the new combination:
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --with-http_flv_module --with-http_perl_module -- with-mail --with-http_xslt_module --add-module=/home/defonds/nginx-rtmp-module-master
Then compile:
make
Finally replace the original binary executable:
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp ./objs/nginx /usr/local/nginx/sbin/
7. Modify the nginx configuration file
to create a simple xls table file nclients.xsl for extracting the current channel The number of users, edit its content as follows:
- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- <xsl:output method="html"/>
- <xsl:param name="app"/>
- <xsl:param name="name"/>
- <xsl:template match="/">
- <xsl:value-of select="count(//application[name=$app]/live/stream[name=$name]/client[not(publishing) and flashver])"/>
- </xsl:template>
- </xsl:stylesheet>
Then put it in a directory, say /home/www.
Modify the nginx main configuration file nginx.conf and add the following:
- location /stat {
- rtmp_stat all;
- allow 127.0.0.1;
- }
- location /nclients {
- proxy_pass http://127.0.0.1/stat;
- xslt_stylesheet /home/www/nclients.xsl app='$arg_app' name='$arg_name';
- add_header Refresh "3; $request_uri";
- }
8. Restart nginx
/usr/local/nginx/sbin/nginx
No news is good news, there is no output from the terminal to prove that the startup was successful. Otherwise, check the main configuration file with reference to the abnormal information given by the terminal.
Access the following address according to the live channel:
http://live server IP/nclients?app=app application name&name=channel name
If the returned result indicates that the with-http_xslt_module module is successfully installed. The returned result is the number of viewers of the current channel.
Reference: Detailed explanation of the nginx-rtmp-module instruction of the Nginx RTMP module ;
https://github.com/arut/nginx-rtmp-module/wiki/Getting-number-of-subscribers .