About
The nginx_http_push_module is designed to become a mature HTTP push and comet service. It can handle all links and broadcast messages to all clients only through HTTP requests, this makes it easy for you to write asynchronous web applications, and you do not have to worry about delayed requests in the code. This module fully implements basic HTTP push relay Protocol
Why choose this module?
When you want to write a real-time update module, such as some chat rooms and multiplayer online Flash games. Either way, we should avoid refreshing the page when updating requests or training the server every few seconds. Such code is ugly (maybe my translation error ).
How to Use
You can download the module code, install the module, and compile the nginx CONF file. with several lines of JavaScript, You can implement a user client. You can send Real-Time Messages to long-round training users only through HTTP requests.
Download:
Http://pushmodule.slact.net/downloads/nginx_http_push_module-0.692.tar.gz
If you need to update the version or the link is unavailable, visit the official address http://pushmodule.slact.net
Installation (including installation of some other modules is not necessary ):
./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
The nginx version I use is nginx-1.7.2, and the official minimum version is 0.7, 0.6 and 0.5 are not supported.
Compile a simple nginx configuration file to view how to use the module:
1. Open the nginx configuration file and write the following code:
...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. Restart nginx
./Nginx-s stop
./Nginx
3. Message subscription
# Curl -- get http: // 172.16.18.114/sub? Id = jfy
The parameter id = jfy is push_channel_id.
The test shows that the instance is in the waiting state.
4. Message Publishing
# Curl-d "nginx_http_push_module" http: // 172.16.18.114/pub? Id = jfy
The id = jfy here is the same as the sub above.
After the sub is released, the above sub will return a message
Configuration details
Variable:
$ Push_channel_id
As a unique identifier, the pub and sub values to be communicated must be the same for different communication channels.
For example:
Set $ push_channel_id $ arg_id;
# Channel ID is the string variable "ID" on the current URL"
# (/Pub? Id = channel_id_string)
Command:
Publisher/subscriber
Push_subscriber [long-poll | interval-poll]
Default Value: Long-poll.
Location: Server, location
Define a server or location as the subscriber. This indicates that a sub communicates with the information pipeline and sets if-modified-since and if-None-match in the Request Header headers to automatically traverse the list, process the messages that have been retained for the longest time in the queue or the upcoming new messages. For more information, see Protocol documentation.
When a sub request data fails to arrive, the sub side sends a long round training request. If it is in the interval-poll method, the system returns the 304 code and the most recent accepted data.
If-None-match and if-modified-since:
When a pub message is sent, the message carries a unique timestamp message_time. If multiple pub messages are sent at the same time, the message_tag attribute value of the message is enabled to increase from 0, otherwise, message_tag = 0;
Data in the queue after a sub message is not deleted
In the result returned by the sub message, the HTTP header contains:
Last-modified: Thu, 21 Aug 2014 05:44:47 GMT is message_time
Etag: 0 is message_tag
Sub retrieves if-None-match from the HTTP headers as the tag of the last acquired message, and if-modified-since as the time of the last acquired message
Sub traverses the queue from the back to the front and compares the message_time of the message with the time in the header. If the two are the same, the sub compares the tag to determine which message is available or waiting for future messages.
If the sub header does not contain if-None-match and if-modified-since, when 0 is processed, the old message will always be obtained.
The correct usage is:
In the next sub request headers, if-None-match is assigned the value of etag in the previous sub returned headers.
When headers in the sub request, if-modified-since is assigned the value of last-modified in the headers returned by the previous sub request.
Push_subscriber_concurrency [last | first | broadcast]
Default Value: broadcast.
Environment: HTTP, server, location
Multi-user request control processing, working method:
Broadcast: receives broadcast requests. All user requests for the current connection are saved;
Last: the most recent user requests are saved. All other users have 409 conflict;
First: the initial user request is saved, and all others conflict with 409;
Push_publisher
Default Value: None
Environment: Server, location
Define a server or location as publisher. The HTTP request sent to publisher is treated as the data sent to subscribers.
Information Storage:
Push_store_messages [ON | Off]
Default Value: On
Environment: HTTP, server, location
When off is used, it only indicates push_message_buffer_length 0;
When off is used, the message is discarded if no sub is found in pub messages.
Push_max_reserved_memory [size]
Default Value: 16 MB
Environment: HTTP
The memory block size of this module will be used for information queue and information cache.
Push_min_message_buffer_length [number]
Default Value: 1
Environment: HTTP, server, location
Minimum Information Storage for each channel.
Push_max_message_buffer_length [number]
Default Value: 10
Environment: HTTP, server, location
The maximum number of information storage for each channel.
Push_message_buffer_length [ON | Off]
Default Value: Off
Environment: HTTP, server, location
The exact number of information stored in each channel
Push_delete_oldest_received_message [off]
Default Value: 0
Environment: HTTP, server, location
When started, the old data in the channel will be deleted once it is accepted by the user. All information that exceeds push_max_message_buffer_length will be cached in the channel information. The original author suggested that you avoid using the change command because it violates the idempotence principle of the user GET request.
Push_message_timeout [time]
Default Value: 1 h
Environment: HTTP, server, location
As the expiration time of the message in the opposition, if you do not want the message to have an expiration time, you can set it to 0.
Security
Push_authorized_channels_only [ON | Off]
Default Value: Off
Environment: HTTP, server, location
Whether subscriber can create a channel through a request. If on is set, publisher must send a post or put request before subscriber requests data. Otherwise, all the subscriber requests that do not have channels will receive a 403 code.
Push_channel_group [String]
Default Value: None
Environment: Server, location
As a constraint, it is suitable for sending to a specified channel, and other channels will never receive it. It is like a channel ID prefix string.
Push_max_channel_id_length [number]
Default Value: 512
Environment: Main, server, location
Sets the maximum length of the channel ID. The long part is truncated.
Push_max_channel_subscribers [number]
Default Value: 0 (unlimited)
Environment: Main, server, location
You can understand the maximum number of concurrent subscriber !!!!