nginx模块学习——nginx_http_push_module模块深入讲解和聊天室实现

关于:

 

nginx_http_push_module模块致力成为一个成熟的http推送和comet服务,它能够处理好全部链接,并且仅通过http请求,可以完成广播消息到所有客户端,这让你写异步web应用程序时得心应手,并且在代码中完全不必理会延时请求。

 

本文为翻译文章,有部分原创转载请注明地址:http://blog.csdn.net/lengzijian/article/details/7638088

 

为什么选择此模块:

 

当你要写一个实时更新的模块时,例如某些聊天室、多人在线flash游戏等。无论哪种方式,我们都要避免更新请求时刷新页面或者每隔几秒轮训服务器,这样的代码丑陋无比(也许是我翻译错误)

 

如何使用:

 

你可以下载模块代码、安装模块、编写nginxconf文件,通过几行的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.70.60.5版本都不支持。

编写简单nginx配置文件查看模块如何使用:

1.打开nginx配置文件,写入如下代码:

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; 
}


2.重启nginx

./nginx –s stop

./nginx

3.访问站点

http://192.168.0.237:2033/activity?id=lengzijian

注意后面变量的keyidvalue为你自定义的例子中为lengzijian

测试可以看到页面处于等待状态。

4.写脚本发送信息

perl 语言写一个脚本:

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;


注意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实现,如果有任何问题,可以留言解答。

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值