說明:
已經安裝好的nginx,需要添加一個未被編譯安裝的模塊,需要怎么弄呢?
具體:這里以安裝第三方nginx-rtmp-module和nginx-accesskey-2.0.3模塊為例,nginx的模塊是需要重新編譯nginx,而不是像apache一樣配置文件引用.so
1. 下載第三方擴展模塊nginx-rtmp-module
sudo git clone https://github.com/arut/nginx-rtmp-module.git
2. 下載第三方擴展模塊nginx-accesskey-2.0.3
http://www.filewatcher.com/m/nginx-accesskey-2.0.3.tar.gz.2632-0.html 下載后解壓即可
tar.gz 這種格式是我使用得最多的壓縮格式。它在壓縮時不會占用太多CPU的,而且可以得到一個非常理想的壓縮率。使用下面這種格式去壓縮一個目錄: # tar -zcvf archive_name.tar.gz directory_to_compress 解壓縮: # tar -zxvf archive_name.tar.gz
3、最后下載的結果如下所示,git上下載的是不需用解壓的,nginx-accesskey-2.0.3 下載下來需要解壓的

4.cp nginx-accesskey-2.0.3 到Nginx 安裝目錄下去 nginx-1.8.1

修改 nginx-accesskey-2.0.3 文件下面的配置文件
vim nginx-accesskey-2.0.3/config
#修改$HTTP_ACCESSKEY_MODULE為ngx_http_accesskey_module
USE_MD5=YES
USE_SHA1=YES
ngx_addon_name=ngx_http_accesskey_module
HTTP_MODULES="$HTTP_MODULES ngx_http_accesskey_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_accesskey_module.c"
5、提示安裝錯誤:
./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option.
大概意思是:HTTP重寫模塊需要PCRE庫。您可以使用--without-http_rewrite_module禁用模塊選項,或將PCRE庫安裝到系統中,或構建PCRE庫通過使用--with-pcre = <path>選項從源與nginx靜態。
解決辦法:
需要安裝一下兩個庫文件:(nginx pcre 安裝)
sudo apt-get install libpcre3 libpcre3-dev
apt-get install openssl libssl-dev
apt-get install git
6、在Nginx安裝目錄下面配置文件
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --add-module=./nginx-accesskey-2.0.3 --add-module=../nginx-rtmp-module/

注意:安裝時候的兩個模塊是在不同的目錄下面的,所以在配置文件的時候一定要記住這個文件路徑必須的合適,否則安裝不成功,提示一下錯誤:

7、編譯:
make
#不要make install,否則就是覆蓋安裝
8、 替換nginx二進制文件:
root@iZ231gvwxe7Z:/home/www/nginx-1.8.1# cd objs/
root@iZ231gvwxe7Z:/home/www/nginx-1.8.1/objs# ls
addon autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
root@iZ231gvwxe7Z:/home/www/nginx-1.8.1/objs# cp nginx /usr/local/nginx/sbin/nginx
9、 查看已經安裝好的模塊和重啟Nginx:
www@iZ231gvwxe7Z:/tmp/hls$ /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.8.1 built by gcc 4.8.2 (Ubuntu 4.8.2-19ubuntu1) built with OpenSSL 1.0.1f 6 Jan 2014 TLS SNI support enabled configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module
--with-http_ssl_module --with-http_realip_module
--add-module=./nginx-accesskey-2.0.3
--add-module=../nginx-rtmp-module/
配置文件:
user www www; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; error_log logs/warn.log warn; #pid logs/nginx.pid; events { worker_connections 65525; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } rtmp { server { listen 1935; chunk_size 4096; application live { live on; record off; exec /home/www/bin/deviceToUrlStreamName.sh $name; exec_kill_signal term; } application live360p { live on; record off; } } }
10、 重啟Nginx:
service nginx restart
11.增加一個RTMP在線統計人數模塊:
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --with-http_flv_module --with-http_perl_module --with-http_xslt_module --add-module=../nginx-rtmp-module/
配置 --with-http_xslt_module 時提示 the HTTP XSLT module requires the libxml2/libxslt libraries
提示錯誤:./configure: error: the HTTP XSLT module requires the libxml2/libxslt
sudo apt-get install libxml2 libxml2-dev libxslt-dev sudo apt-get install libgd2-xpm libgd2-xpm-dev
修改 nginx 配置文件,增加以下兩個代碼:
location /stat { rtmp_stat all; allow 127.0.0.1; } location /nclients { proxy_pass http://127.0.0.1/stat; xslt_stylesheet /www/nclients.xsl app='$arg_app' name='$arg_name'; add_header Refresh "3; $request_uri"; }
創建一個簡單地 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>
使用HTTP請求http://myserver.com/nclients?app=myapp&name=mystream獲取流訂閱者的數量。 在瀏覽器或iframe中打開時,此編號將每3秒自動刷新一次。
重啟Nginx服務:
root@iZ231gvwxe7Z:/home/www/nginx-1.8.1/objs# service nginx restart
* Stopping Nginx Server... [fail]
* Starting Nginx Server... [ OK ]
root@iZ231gvwxe7Z:/home/www/nginx-1.8.1/objs#
使用OBS推流該Nginx服務器的RTMP模塊既可以
例如我測試的結果:

使用VLC播放該流
測試在線觀看人數結果(只有我一個人播放的,就是一個人哦!):

參考文獻:
https://github.com/arut/nginx-rtmp-module/wiki/Getting-number-of-subscribers
http://blog.csdn.net/defonds/article/details/9065591
http://www.cnblogs.com/terrysun/archive/2012/11/22/2782472.html
如何升級Nginx到最新穩定版:http://www.cnblogs.com/terrysun/archive/2012/11/22/2782472.html
