nginx_http_push_module模块使用详解

关于

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

      Etag: 0                                         即为message_tag

    sub会从http的headers中取If-None-Match作为上次获取到的消息的tag, If-Modified-Since作为上次获取到的消息的time

    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值
    
    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数,你懂得!!!!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值