你的 Nginx 已經有了 RTMP 直播功能的話,若是你還想統計某直播頻道當前觀看用戶量的話,能夠加入 with-http_xslt_module 模塊。具體步驟以下:
1.查看原來的參數
/usr/local/nginx/sbin/nginx -V
輸出中能夠獲得原來編譯時帶有的參數,好比做者獲得:
--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
這些參數在咱們安裝新模塊時仍然有用。
2.下載 nginx-rtmp-module 安裝包
nginx-rtmp-module-master.zip,最新下載地址:
https://github.com/arut/nginx-rtmp-module。
下載後將其解壓縮獲得 nginx-rtmp-module-master 目錄。
3.下載 nginx-1.3.8.tar.gz 包
能夠在
http://nginx.org/download/ 找你須要的版本。
下載後解壓縮獲得 nginx-1.3.8 目錄。
4.關閉 nginx
ps - ef | grep nginx
在進程列表裏找到 master 進程,這是 nginx 的主進程號。
kill -TERM 主進程號
nginx 被關閉。
5.安裝其餘依賴包
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.編譯 with-http_xslt_module 模塊
在步驟一獲得的一系列參數後增長如下參數:
--with-http_xslt_module --add-module=/home/defonds/nginx-rtmp-module-master
其中 /home/defonds/nginx-rtmp-module-master 是步驟二獲得的目錄。
切換進入步驟三獲得的 nginx-1.3.8 目錄,使用新組合獲得的參數列表從新配置:
./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
而後編譯:
make
最後替換掉原來的二進制執行文件:
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp ./objs/nginx /usr/local/nginx/sbin/
7.修改 nginx 配置文件
建立一個簡單地 xls 表格文件 nclients.xsl 用於提取觀看當前頻道的用戶數量,編輯其內容以下:
- <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>
而後將其放在一個目錄中,好比 /home/www。
修改 nginx 主配置文件 nginx.conf,添加如下內容:
- 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.重啓 nginx
/usr/local/nginx/sbin/nginx
No news is good news,終端沒有輸出證實啓動成功。不然參照終端給的異常信息檢查主配置文件。
根據直播頻道訪問如下地址:
http://直播服務器IP/nclients?app=app應用名&name=頻道名
有返回結果表示 with-http_xslt_module 模塊安裝成功。返回結果就是當前頻道的觀看人數。
參考資料:Nginx RTMP 模塊 nginx-rtmp-module 指令詳解;
https://github.com/arut/nginx-rtmp-module/wiki/Getting-number-of-subscribers。 html