Nginx服务器 nginx http push module模块

   =关于http push module=

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

模块安装[编辑]

1、下载地址:http://pushmodule.slact.net/downloads/nginx_http_push_module-0.692.tar.gz

2、Nginx重新编译添加模块
查看ngixn版本及编译参数

/usr/local/nginx/sbin/nginx -V

进入nginx源码目录

cd nginx

重新编译安装nginx

./configure 之前安装Nginx的参数 --add-module=push_module解压目录/nginx_http_push_module
make

make完之后在objs目录下就多了个nginx,这个就是新版本的程序 备份旧的nginx程序

cp /usr/local/nginx/sbin/nginx/usr/local/nginx/sbin/nginx.bak

用新的nginx程序覆盖旧的

cp objs/nginx /usr/local/nginx/sbin/nginx

测试新的nginx程序是否正确

/usr/local/nginx/sbin/nginx -t

nginx: theconfiguration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx:configuration file /usr/local/nginx/conf/nginx.conf test issuccessful

平滑重启nginx

/usr/local/nginx/sbin/nginx -s reload

官方要求Nginx最低版本为0.70.60.5版本都不支持。

3、打开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; 
}

配置信息详解[编辑]

变量:

$push_channel_id
作为唯一标识,区别通信通道,要通信的pub和sub这个值必须相同。
例如:
set $push_channel_id $arg_id; 
#channel id 就是当前url上的字符串变量"id"


指令:

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长度,长的部分将会被截断,例如:
push_max_channel_subscribers[number ]
默认值:0(unlimited)
使用环境:main,server,location