关于:
nginx_http_push_module模块致力成为一个成熟的http推送和comet服务,它能够处理好全部链接,并且仅通过http请求,可以完成广播消息到所有客户端,这让你写异步web应用程序时得心应手,并且在代码中完全不必理会延时请求。
为什么选择此模块:
当你要写一个实时更新的模块时,例如某些聊天室、多人在线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配置文件,写入如下代码:
- <span style="font-size:16px;">location /publish {
- set $push_channel_id $arg_id;
- push_publisher;
- push_store_messages on;
- push_message_timeout 2h;
- push_max_message_buffer_length 10;
- }
- location /activity {
- push_subscriber;
- set $push_channel_id $arg_id;
- push_subscriber_concurrency broadcast;
- default_type text/plain;
- }
- </span>
2.重启nginx
./nginx –s stop
./nginx
3.访问站点
http://192.168.0.237:2033/activity?id=lengzijian
注意后面变量的key为id、value为你自定义的例子中为lengzijian。
测试可以看到页面处于等待状态。
4.写脚本发送信息
用perl 语言写一个脚本:
- <span style="font-size:16px;">use LWP::UserAgent;
- use HTTP::Request::Common;
- my $ua = new LWP::UserAgent;
- my $response = $ua->request(
- POST 'http://192.168.0.237:2033//publish?id=lengzijian',
- Content_Type => 'text/html',
- Content => '服务端推送信息'
- );
- my $content = $response->content;
- print $content;
- </span>
注意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实现,如果有任何问题,可以留言解答。
本文为翻译文章,有部分原创转载请注明地址:http://blog.youkuaiyun.com/lengzijian/article/details/7638088
【关于我们】
才淇(微信公众号:caiqicehua),专注于国内各大互联网公司社会招聘内推。每天更新最新互联网名企(包括但不限于今日头条、网易游戏、BAT、网易互联网、小米、京东、乐视、携程等名企)内推信息,有技术岗、有产品岗、有运营岗、有设计岗、有交互岗、有销售岗,更有其他N多相关岗位!更多内推信息请扫描以下二维码关注查阅。