Nginx module learning-nginx_http_push_module

Source: Internet
Author: User
Tags perl script
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 article for translation articles, some original reprint please note address: http://blog.csdn.net/lengzijian/article/details/7638088

 

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:

./Configure -- prefix = <your nginx path>/nginx -- add-module = <your download module>/nginx_http_push_module/

Make

Make install

The nginx version I use is nginx-1.0.13, 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 /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. Restart nginx

./Nginx-s stop

./Nginx

3. Access the site

Http: // 192.168.0.237: 2033/activity? Id = lengzijian

Note that the key of the variable is ID and the value is lengzijian in your custom example.

The test shows that the page is in the waiting status.

4. Write a script to send information

Write a script in 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 => 'server push information'); my $ content = $ response-> content; print $ content;


Note that the connection belt parameter of the POST request should be the same as the previous ID = lengzijian

Execution statement

Perl XXX. pl

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"
#(/foo/bar?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 subscriber. This indicates that a sub communicates with the information pipeline and uses the entity-caching Request Header (if-modified-since and if-None-match) automatically traverses the list to process the information that is retained for the longest time in the queue.

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.

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;

 

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

Set the maximum length of the channel ID. The long part is truncated. For example:

1. Set conf:

Push_max_channel_id_length5;

2. Enter the connection:

Http: // 192.168.0.237: 2033/activity? Id = lengzijian

3. Change the Perl script

4. view the page

Push_max_channel_subscribers [number]


Default Value: 0 (unlimited)


Environment: Main, server, location


You can understand the maximum number of concurrent subscriber !!!!

Summary

 

The following is an official chat room implemented using nginx_http_push_module:

Chat Room: http://www.2zct.com/nginx/chat.zip

Nginx configurations: http://www.2zct.com/nginx/nginx_conf.zip

Slightly changed. js + nginx is implemented. If you have any questions, leave a message to answer them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.