NGiNX_HTTP_Push_Module 是一個 Nginx 的擴展模塊,它實現了 HTTP Push 和Comet server的功能。HTTP Push 被經常用在網頁上主動推的技術,例如一些聊天室啊,更新信息非常頻繁的應用場合。
Http Server Push是一種推送技術,服務器主動向瀏覽器發送數據。
可以參考:http://wiki.nginx.org/HttpPushStreamModule
1. 下載安裝
1) 下載源代碼包
Nginx:http://nginx.org/
Nginx Http Push Module:http://pushmodule.slact.net/(網站打不開)csdn上有一份http://download.csdn.net/download/javadxz/10046650。
2) 解壓縮
tar zxvf nginx-1.12.2.tar.gz
tar zxvf nginx_http_push_module-0.692.tar.gz
3) 編譯安裝
進入nginx根目錄
編譯Nginx,configure指定待添加模塊的路徑
./configure --add-module=/home/nginx/nginx_http_push_module-0.692
make && make install
錯誤提示: ./configure: error: the HTTP rewrite module requires the PCRE library. 安裝pcre-devel與openssl-devel解決問題 yum install gcc gcc-c++ openssl_devel -y yum -y install pcre-devel openssl openssl-devel ./configure --prefix=/usr/local/nginx
2. 配置
關於推送配置說明,可參考推送模塊源碼包內的README,
推送協議可參考protocol.txt,或訪問http://pushmodule.slact.net/protocol.html,
在Nginx配置文件中添加一個基本的推送配置,
可參考自帶的用於測試的配置文件nginx_http_push_module-0.692/tests/nginx.conf,將其中配置部分附加到Nginx配置文件的http結構中(安裝后nginx啟動默認讀取的文件是/usr/local/nginx/conf/nginx.conf)
location /chat { push_channel_group pushmodule_chat; location /chat/pub { set $push_channel_id $arg_id; #多個聊天室就相當於 ?id=markdream push_publisher; push_message_timeout 5m; push_message_buffer_length 10; } location /chat/sub { set $push_channel_id $arg_id; #多個聊天室就相當於 ?id=markdream push_subscriber; send_timeout 3600; } }
3. 調試
啟動
./nginx
./nginx -s reload
./nginx -s stop
啟動Nginx,使用curl進行推送測試
1) subscriber請求數據:
curl -X GET localhost:8082/chat/sub?id=1
向channel1請求數據,當前channel無數據,等待數據生成
2) publisher推送數據:
curl -X POST -d "china 2017-10-31" http://localhost:8082/chat/pub?id=1
向channel1推送數據,subscriber收到數據並返回
4. 總結
Http服務器需要使用一個或一組url指定發布服務和訂閱服務(publisher and subscriber locations,我翻譯的不太合理)。
所有發送到發布服務的請求被視為發布請求,所有發送到訂閱服務的請求被視為訂閱請求。
通道(channel)需要使用唯一id進行標識,推薦使用url指定具體通道,如 localhost:8082/sub?id=1。
發布請求(publisher request)通過POST向服務器傳輸數據,並通知其向某些通道某些用戶發送數據,
訂閱請求(subscriber request)通過GET向服務器請求數據,通知其該用戶想要接收數據。
配置信息詳解
變量:
$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實現,如果有任何問題,可以留言解答。