Nginx的nginx_http_push_module模块测试
2011-03-03 18:10
316 查看
Nginx的nginx_http_push_module模块,可以使nginx服务器成为一个comet服务器
可以接受Client端的一个长连接请求,当server端有消息push时,则返回消息给Client
1. 编译很简单:
–add-module=../slact-nginx_http_push_module
2.配置如下:
折叠展开复制代码
push_max_reserved_memory 20M;
push_authorized_channels_only on;
server{
listen 80;
server_name localhost test.ysz.com;
access_log /opt/work/log/nginx_access_ysz.log main;
error_log /opt/work/log/nginx_error_ysz.log error;
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;
}
}
3.测试:
浏览器端访问http://localhost/activity?id=10000,则请求会被堵塞
用python写一个简单的post程序,向http://localhost//publish?id=10000上post数据,则浏览器端会马上得到该消息
注意:请求publish时,如果要发布消息,则必须得是POST请求,而且对应的Content-Type会被转发给长连接的Client。Get请求会返回当前是否有存在的channel_id。具体的Http Push协议细节,可参考http://pushmodule.slact.net/protocol.html
4.一些问题:
如果push模块放在后端的nginx上,而前段nginx反响代理到后端,当Client发送长连接请求时,
默认1分钟会直接断掉连接,返回504,不过可以设置proxy_read_timeout时间足够长来解决
设置push_subscriber_concurrency broadcast,可以将push消息同时发送给同一个用户开启的多个浏览器窗口,但我在一个浏览器里开多个tab也来请求,却只有一个能得到消息,另外一个请求仍旧没有返回。当设置成first时,对应同一个channel_id,如果有多个长连接请求,则只有第一个会等待返回,其它的都返回409。设置成last时,我的测试结果是所有请求都会断掉,似乎是一个Bug?
设置push_store_messages on,当没有对应的长连接请求时,可以暂时把msg缓存起来
设置push_authorized_channels_only on可以打开认证功能,只有当publisher给对应的
channel_id发送过消息后,Client端才可以发起一个长连接请求,否则会返回403 Forbidden错误
当client发起一个长连接请求时,accesslog和后端应用是无法知道其发送请求。不过,通过向publish发送一个Get请求来判断是否有对应channel_id的长连接请求.
5.部分协议细节:
订阅者:
The server MUST accept all valid HTTP
Subscriber requests are considered notifications of intent to receive some message. Subscribers may request existing messages, messages that are not yet available, and messages that are no longer available. The requested message is identified using the
There are several common mechanisms for performing an HTTP server push. The rest of the behavior of the server in response to a subscriber request SHOULD be configurable and MUST be selected from the following list of mechanisms:
Long-Polling Requests for existing messages will be responded to immediately; responses to requests for messages not yet available MUST be delayed until the message becomes available. Delayed responses MUST satisfy all of the following conditions:
A
. The entire response must be indistinguishable from a response to a request for an existing message.
If the channel the subscriber is waiting on is deleted or for some reason becomes unavailable, the server MUST immediately send a
If another subscriber has conflicted with this request, the server MUST immediately send a
Interval-Polling All requests will be responded to immediately. Requests for messages not yet available MUST produce a
In addition, when the server receives more than one concurrent subscriber request on the same channel, it MUST do one of the following:
Broadcast No additional actions are performed Last-in, first-out All but the most recent long-held subscriber request on the channel are sent a
The server SHOULD make this selection configurable, and MUST default to
发送者:
Theserver MUST accept all valid HTTP requests to the publisher location. The server, when sent a publisher request, MUST satisfy all of the following conditions:
GET requests receive a
PUT requests receive a
DELETE requests receive a
POST requests are used to sendmessages. The request MAY contain a body in any encoding representing a message to be sent over the channel. Themessage MUST be immediately delivered to all currently long-held subscriber requests. Additionally, the message MAY be stored for future retrieval and the oldest message stored for the channel MAY be deleted.
A POST request MUST be replied to with a
The Content-Type header of the request MUST be forwarded with the message.
可以接受Client端的一个长连接请求,当server端有消息push时,则返回消息给Client
1. 编译很简单:
–add-module=../slact-nginx_http_push_module
2.配置如下:
折叠展开复制代码
push_max_reserved_memory 20M;
push_authorized_channels_only on;
server{
listen 80;
server_name localhost test.ysz.com;
access_log /opt/work/log/nginx_access_ysz.log main;
error_log /opt/work/log/nginx_error_ysz.log error;
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;
}
}
3.测试:
浏览器端访问http://localhost/activity?id=10000,则请求会被堵塞
用python写一个简单的post程序,向http://localhost//publish?id=10000上post数据,则浏览器端会马上得到该消息
注意:请求publish时,如果要发布消息,则必须得是POST请求,而且对应的Content-Type会被转发给长连接的Client。Get请求会返回当前是否有存在的channel_id。具体的Http Push协议细节,可参考http://pushmodule.slact.net/protocol.html
4.一些问题:
如果push模块放在后端的nginx上,而前段nginx反响代理到后端,当Client发送长连接请求时,
默认1分钟会直接断掉连接,返回504,不过可以设置proxy_read_timeout时间足够长来解决
设置push_subscriber_concurrency broadcast,可以将push消息同时发送给同一个用户开启的多个浏览器窗口,但我在一个浏览器里开多个tab也来请求,却只有一个能得到消息,另外一个请求仍旧没有返回。当设置成first时,对应同一个channel_id,如果有多个长连接请求,则只有第一个会等待返回,其它的都返回409。设置成last时,我的测试结果是所有请求都会断掉,似乎是一个Bug?
设置push_store_messages on,当没有对应的长连接请求时,可以暂时把msg缓存起来
设置push_authorized_channels_only on可以打开认证功能,只有当publisher给对应的
channel_id发送过消息后,Client端才可以发起一个长连接请求,否则会返回403 Forbidden错误
当client发起一个长连接请求时,accesslog和后端应用是无法知道其发送请求。不过,通过向publish发送一个Get请求来判断是否有对应channel_id的长连接请求.
5.部分协议细节:
订阅者:
The server MUST accept all valid HTTP
GETrequests to the subscriber location. All other request methods SHOULD be responded to with a
405 Method Not Allowedstatus code.
Subscriber requests are considered notifications of intent to receive some message. Subscribers may request existing messages, messages that are not yet available, and messages that are no longer available. The requested message is identified using the
If-Modified-Sinceand
If-None-Matchrequest headers. A request with no
If-Modified-Sinceheader MUST be assumed to be requesting the oldest available message in a channel. Each
200 OKresponse containing a message MUST have its
Last-Modifiedand
Etagheaders set so that a request using those headers will be interpreted as a request for the next available message. Additionally, said
200 OKMUST contain the
Content-Typeheader of the message publisher request, unless no
Content-Typeheader had been provided or it is explicitly overridden by server configuration.
There are several common mechanisms for performing an HTTP server push. The rest of the behavior of the server in response to a subscriber request SHOULD be configurable and MUST be selected from the following list of mechanisms:
Long-Polling Requests for existing messages will be responded to immediately; responses to requests for messages not yet available MUST be delayed until the message becomes available. Delayed responses MUST satisfy all of the following conditions:
A
200 OKresponse containing the message (and its
Content-Type) MUST be sent immediately after the message becomes available
. The entire response must be indistinguishable from a response to a request for an existing message.
If the channel the subscriber is waiting on is deleted or for some reason becomes unavailable, the server MUST immediately send a
410 Goneresponse.
If another subscriber has conflicted with this request, the server MUST immediately send a
409 Conflictresponse.
Interval-Polling All requests will be responded to immediately. Requests for messages not yet available MUST produce a
304 Not Modifiedresponse code.
In addition, when the server receives more than one concurrent subscriber request on the same channel, it MUST do one of the following:
Broadcast No additional actions are performed Last-in, first-out All but the most recent long-held subscriber request on the channel are sent a
409 Conflictresponse. First-in, last-out All but the oldest request will be sent a
409 Conflict
The server SHOULD make this selection configurable, and MUST default to
broadcastbehavior.
发送者:
Theserver MUST accept all valid HTTP requests to the publisher location. The server, when sent a publisher request, MUST satisfy all of the following conditions:
GET requests receive a
200 OKresponse for existing channels and a
404 Not Foundotherwise.
PUT requests receive a
200 OKresponse. The request creates a channel if no channel with the given channel id exists.
DELETE requests receive a
200 OKif thechannel identified by the channel id exists and has been completely deleted. All subscribers MUST have been sent a
410 Goneresponse. Requests for nonexistent channels MUST be responded to with a
404 Not Found.
POST requests are used to sendmessages. The request MAY contain a body in any encoding representing a message to be sent over the channel. Themessage MUST be immediately delivered to all currently long-held subscriber requests. Additionally, the message MAY be stored for future retrieval and the oldest message stored for the channel MAY be deleted.
A POST request MUST be replied to with a
201 Createdif there were any long-held subscribers that have been sent this message, and with a
202 Acceptedotherwise.
The Content-Type header of the request MUST be forwarded with the message.
相关文章推荐
- Nginx的nginx_http_push_module模块测试
- nginx模块学习——nginx_http_push_module模块深入讲解和聊天室实现
- Nginx + FastCgi/ php5-fpm + nginx_http_push_module安装
- nginx模块学习——nginx_http_push_module模块深入讲解和聊天室实现
- 基于nginx_http_push_module模块让nginx变成Comet Server
- 使用NGiNX_HTTP_Push_Module实现简单的服务器推送
- 使用NGiNX_HTTP_Push_Module实现简单的服务器推送
- nginx_http_push_module
- nginx模块学习——nginx_http_push_module模块深入讲解和聊天室实现
- Nginx Http Push Module 消息推送
- 使用NGiNX_HTTP_Push_Module实现简单的服务器推送
- nginx数据推送模块 - nginx http push module
- 使用NGiNX_HTTP_Push_Module实现简单的服务器推送
- 【 nginx模块学习】nginx_http_push_module模块深入讲解和聊天室实现
- nginx_http_push_module模块使用详解
- 安装成功的nginx,如何添加未编译安装模块(非覆盖安装http_image_filter_module)
- nginx强制使用https访问(http跳转到https
- 高性能Web服务器Nginx的配置与部署研究(10)核心模块之HTTP模块Location相关指令
- nginx搭建支持http和rtmp协议的流媒体服务器之三
- nginx配置http和https