nginx_http_push_module模塊使用詳解

richmanxxx 8年前發布 | 12K 次閱讀 Web服務器

來自: http://blog.csdn.net//jiao_fuyou/article/details/38729829


關于

nginx_http_push_module模塊致力成為一個成熟的http推送和comet服務,它能夠處理好全部鏈接,并且僅通過http請求,可以完成廣播消息到所有客戶端,這讓你寫異步web應用程序時得心應手,并且在代碼中完全不必理會延時請求。該模塊完整的實現了Basic HTTP Push Relay Protocol

 

為什么選擇此模塊

當你要寫一個實時更新的模塊時,例如某些聊天室、多人在線flash游戲等。無論哪種方式,我們都要避免更新請求時刷新頁面或者每隔幾秒輪訓服務器,這樣的代碼丑陋無比(也許是我翻譯錯誤)。
 

如何使用 

你可以下載模塊代碼、安裝模塊、編寫nginx的conf文件,通過幾行的javascript,你就可以實現一個用戶客戶端。只需要通過http請求,你也可以發送實時消息給長輪訓用戶。
 
下載:
 
http://pushmodule.slact.net/downloads/nginx_http_push_module-0.692.tar.gz
如果需要跟更新版本,或者鏈接不可用,可訪問官方地址http://pushmodule.slact.net
 
安裝(包括安裝了一些其它模塊并不是必須的):
./configure --prefix=/usr/local/nginx     \
  --with-debug                            \
  --with-http_stub_status_module          \
  --with-http_ssl_module                  \
  --with-http_realip_module               \
  --with-http_image_filter_module         \
  --with-pcre=../pcre-8.21                \
  --add-module=../ngx_devel_kit-0.2.19    \
  --add-module=../lua-nginx-module-0.9.8  \
  --add-module=../echo-nginx-module       \
  --add-module=../redis2-nginx-module     \
  --add-module=../set-misc-nginx-module   \
  --add-module=../nginx_http_push_module-0.692
make
make install
我使用的nginx版本為nginx-1.7.2,官方要求最低版本為0.7,0.6和0.5版本都不支持。

編寫簡單nginx配置文件查看模塊如何使用:

1.打開nginx配置文件,寫入如下代碼:
...


location /pub {   
    set $push_channel_id $arg_id;   
    push_publisher;   
    push_store_messages on;   
    push_message_timeout 2h;   
    push_max_message_buffer_length 10;   
}   
location /sub {   
    push_subscriber;   
    set $push_channel_id $arg_id;   
    push_subscriber_concurrency broadcast;   
    default_type text/plain;   
}  


...

2.重啟nginx
./nginx –s stop
./nginx


3.消息訂閱
#curl --get http://172.16.18.114/sub?id=jfy
參數id=jfy即為push_channel_id
測試可以看到處于等待狀態。
           
4.消息發布
#curl -d "nginx_http_push_module" http://172.16.18.114/pub?id=jfy
這里的id=jfy,與上面sub的相同
發布后,上面的sub會返回消息


配置信息詳解

變量:
    $push_channel_id
    作為唯一標識,區別通信通道,要通信的pub和sub這個值必須相同。
    例如:
    set $push_channel_id $arg_id; 
    #channel id 就是當前url上的字符串變量"id"
    #(/pub?id=channel_id_string)


指令:

  Publisher / Subscriber
  
    push_subscriber[ long-poll | interval-poll ]
    默認值:long-poll
    位置:server,location

    定義一個server或者location作為subscriber,這個代表一個sub與信息管道進行通信,通過請求頭headers中設置If-Modified-Since和If-None-Match自動遍歷列表,處理隊列中保留時間最長的消息或是即將到來的新消息。具體可以參考protocol documentation

    當有sub請求數據并且數據未到達時,sub端長輪訓請求。如果是interval-poll方式,則會返回304碼,返回最近接受的數據。
    
    If-None-Match和If-Modified-Since說明:

    pub消息時,消息會帶一個唯一的時間戳message_time,如果同一時間內同時pub多條信息,則會啟用消息的message_tag屬性值從0開始遞增,否則message_tag=0;

    sub消息后隊列中的數據并不刪除

    sub消息返回的結果中,http header中有:
      Last-Modified: Thu, 21 Aug 2014 05:44:47 GMT    即為message_time
</span></span>

      Etag: 0                                         即為message_tag

    sub會從http的headers中取If-None-Match作為上次獲取到的消息的tag, If-Modified-Since作為上次獲取到的消息的time
</span>

    sub從后往前遍歷隊列,先比較這條消息的message_time與header中的time,如果這兩個相同則再比較tag,來決定獲取現有的哪一條消息或是等待將來的消息

    如果sub的header中沒有If-None-Match和If-Modified-Since,則當0處理,那就是將永遠一直獲取最舊的一條消息
    正確的用法是:
      當次sub請求中headers,If-None-Match賦值為上次sub返回headers中的Etag值
      當次sub請求中headers,If-Modified-Since賦值為上次sub返回headers中的Last-Modified值</span> </blockquote>     
    push_subscriber_concurrency[ last | first | broadcast ]
    默認值:broadcast
    使用環境:http,server,location
    多用戶請求時的控制處理,工作方式:
    broadcast:廣播形式接受,所有當前連接的用戶請求被保存;
    last:最近的用戶請求被保存,其他用戶全部409沖突;
    first:最開始的用戶請求被保存,其他全部409沖突;
 
    push_publisher
    默認值:none
    使用環境:server,location
    定義一個server或者location作為publisher,發給publisher的http請求被視為發送給subscribers的數據。
 

信息存儲:  
    push_store_messages[ on | off ]
    默認值:on
    使用環境:http,server,location

    當使用off時,僅表示push_message_buffer_length 0;

    當使用off時,pub消息時若沒有sub則消息會被丟棄

 
    push_max_reserved_memory[ size ]
    默認值:16M
    使用環境:http
    這個模塊的內存塊大小將會用于信息隊列和信息緩存。
 
    push_min_message_buffer_length[number ]
    默認值:1
    使用環境:http,server,location
    每個channel的最小信息存儲。
 
    push_max_message_buffer_length[number ]
    默認值:10
    使用環境:http,server,location
    每個channel的最大信息存儲數。
 
    push_message_buffer_length[ on |off ]
    默認值:off
    使用環境:http,server,location
    每個channel存儲的確切信息數
 
    push_delete_oldest_received_message[off ]
    默認值:0
    使用環境:http,server,location
    當啟動時,一旦channel中最老的數據被用戶接受后,它將被刪除。所有超過push_max_message_buffer_length的信息將會在channel的信息緩存中。原作者建議避免使用改指令,因為它違背了用戶get請求的冪等原則。
 
    push_message_timeout[ time ]
    默認值:1h
    使用環境:http,server,location
    作為消息在對立當中的過期時間,如果你不希望消息有過期時間,可以設置為0.
 

安全    
    push_authorized_channels_only[ on| off ]
    默認值:off
    使用環境:http,server,location
    subscriber是否可以通過請求來創建一個channel。如果設置on,publisher必須在subscriber請求數據前,發送一個post或者put請求。否則所有的subscriber請求不存在的channels時,將會得到403碼。
 
    push_channel_group[ string ]
    默認值:none
    使用環境:server,location
    作為一種約束,適用于發給指定channel,而其他channel將永遠不會收到。就好像是channel id的前綴字符串。
 
    push_max_channel_id_length[ number]
    默認值:512
    使用環境:main,server,location
    設置最大的channel id長度,長的部分將會被截斷

    push_max_channel_subscribers[number ]
    默認值:0(unlimited)
    使用環境:main,server,location
    最大并發subscriber數,你懂得!!!!

 本文由用戶 richmanxxx 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!