讓你的 Nginx 的 RTMP 直播具備統計某頻道在線觀看用戶數量的功能

  你的 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 用於提取觀看當前頻道的用戶數量,編輯其內容以下:
  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
  2.   
  3. <xsl:output method="html"/>  
  4.   
  5. <xsl:param name="app"/>  
  6. <xsl:param name="name"/>  
  7.   
  8. <xsl:template match="/">  
  9.     <xsl:value-of select="count(//application[name=$app]/live/stream[name=$name]/client[not(publishing) and flashver])"/>  
  10. </xsl:template>  
  11.   
  12. </xsl:stylesheet>  

        而後將其放在一個目錄中,好比 /home/www。
        修改 nginx 主配置文件 nginx.conf,添加如下內容:
  1. location /stat {  
  2.     rtmp_stat all;  
  3.     allow 127.0.0.1;  
  4. }  
  5. location /nclients {  
  6.     proxy_pass http://127.0.0.1/stat;  
  7.     xslt_stylesheet /home/www/nclients.xsl app='$arg_app' name='$arg_name';  
  8.     add_header Refresh "3; $request_uri";  
  9. }  

        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-subscribershtml

相關文章
相關標籤/搜索