關於:
nginx_http_push_module模塊致力成為一個成熟的http推送和comet服務,它能夠處理好全部鏈接,並且僅通過http請求,可以完成廣播消息到所有客戶端,這讓你寫異步web應用程序時得心應手,並且在代碼中完全不必理會延時請求。
本文為翻譯文章,有部分原創轉載請注明地址:http://blog.csdn.net/lengzijian/article/details/7638088
為什么選擇此模塊:
當你要寫一個實時更新的模塊時,例如某些聊天室、多人在線flash游戲等。無論哪種方式,我們都要避免更新請求時刷新頁面或者每隔幾秒輪訓服務器,這樣的代碼丑陋無比(也許是我翻譯錯誤)。
如何使用:
你可以下載模塊代碼、安裝模塊、編寫nginx的conf文件,通過幾行的javascript,你就可以實現一個用戶客戶端。只需要通過http請求,你也可以發送實時消息給長輪訓用戶。
下載:
http://pushmodule.slact.net/downloads/nginx_http_push_module-0.692.tar.gz
如果需要跟更新版本,或者鏈接不可用,可訪問官方地址http://pushmodule.slact.net
安裝:
./configure --prefix=<your nginx path>/nginx --add-module=<your download module>/nginx_http_push_module/
make
make install
我使用的nginx版本為nginx-1.0.13,官方要求最低版本為0.7,0.6和0.5版本都不支持。
編寫簡單nginx配置文件查看模塊如何使用:
1.打開nginx配置文件,寫入如下代碼:
2.重啟nginx
./nginx –s stop
./nginx
3.訪問站點
http://192.168.0.237:2033/activity?id=lengzijian
注意后面變量的key為id、value為你自定義的例子中為lengzijian。
測試可以看到頁面處於等待狀態。
4.寫腳本發送信息
用perl 語言寫一個腳本:
注意post請求的連接帶的參數應該與之前的相同id=lengzijian
執行語句
perl xxx.pl
配置信息詳解
變量:
$push_channel_id
作為唯一標識,區別通信通道,要通信的pub和sub這個值必須相同。
例如:
set $push_channel_id $arg_id;
#channel id 就是當前url上的字符串變量"id"
#(/foo/bar?id=channel_id_string)
指令:
Publisher/Subscriber
push_subscriber[ long-poll | interval-poll ]
默認值:long-poll
位置:server,location
定義一個server或者location作為subscriber,這個代表一個sub與信息管道進行通信,通過entity-caching請求頭(If-Modified-Since and If-None-Match)自動遍歷列表,處理隊列中保留時間最長的信息。
當有sub請求數據並且數據未到達時,sub端長輪訓請求。如果是interval-poll方式,則會返回304碼,返回最近接受的數據。
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;
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長度,長的部分將會被截斷,例如:
1.設置conf:
push_max_channel_id_length5;
2.輸入連接:
http://192.168.0.237:2033/activity?id=lengzijian
3.更改perl腳本
4.查看頁面
push_max_channel_subscribers[number ]
默認值:0(unlimited)
使用環境:main,server,location
最大並發subscriber數,你懂得!!!!
總結
最后附上用nginx_http_push_module實現的官方的聊天室:
聊天室下載地址:http://www.2zct.com/nginx/chat.zip
nginx配置信息下載地址:http://www.2zct.com/nginx/nginx_conf.zip
稍微做了改動,js+nginx實現,如果有任何問題,可以留言解答。