Use nginx_push_stream_module for server push messaging

NGiNX_HTTP_Push_Module  is a Nginx extension module, which implements the HTTP Push and Comet server functions. HTTP Push is often proactively pushed in web technology, such as chat rooms ah, very frequently update applications.

Http Server Push is a push technology, the server sends data to the browser. 

You can refer to: http://wiki.nginx.org/HttpPushStreamModule

 

1. Download and install

1) Download the source code package
Nginx: http://nginx.org/
Nginx Http the Push Module: http://pushmodule.slact.net/ (sites not open) have a on CSDN http://download.csdn.net / download / javadxz / 10.04665 million .

2) 解压缩
tar zxvf nginx-1.12.2.tar.gz
tar zxvf nginx_http_push_module-0.692.tar.gz

3) compile and install

Nginx into the root directory

Compiling Nginx, configure the module to be added to specify the path

./configure --add-module=/home/nginx/nginx_http_push_module-0.692
 make && make install

Copy the code
错误提示:
./configure: error: the HTTP rewrite module requires the PCRE library.

安装pcre-devel与openssl-devel解决问题
yum install gcc gcc-c++ openssl_devel -y
yum -y install pcre-devel openssl openssl-devel 
./configure --prefix=/usr/local/nginx
Copy the code

 

2. Configure

About push configuration, reference is made to push in the module README source package,

Push protocol can refer protocol.txt, or access http://pushmodule.slact.net/protocol.html ,

Add a basic push configuration Nginx configuration file,

Refer own profile for testing nginx_http_push_module-0.692 / tests / nginx.conf, wherein the configuration portion attached to the Nginx http configuration file structure (after installation nginx default startup file is read / usr / local / nginx / conf / nginx.conf)

Copy the code
        LOCATION / Chat { 
            push_channel_group pushmodule_chat; 
            LOCATION / Chat / Pub { 
                 SET $ $ push_channel_id arg_id; # is equivalent to a plurality of chat rooms markdream = ID? 
                 push_publisher; 
                 push_message_timeout 5m; 
                 push_message_buffer_length 10; 
            } 
            LOCATION / Chat / Sub { 
                 SET $ $ push_channel_id arg_id; # is equivalent to a plurality of chat rooms markdream = ID? 
                 push_subscriber; 
                 send_timeout 3600; 
            } 
        }
Copy the code

3. Debugging

start up

./nginx

./nginx -s reload

./nginx -s stop

Start Nginx, using curl Push-test

1) subscriber请求数据:
curl -X GET localhost:8082/chat/sub?id=1
向channel1请求数据,当前channel无数据,等待数据生成

2) publisher推送数据:
curl -X POST -d "china 2017-10-31" http://localhost:8082/chat/pub?id=1
向channel1推送数据,subscriber收到数据并返回

 

 

4. 总结

Http服务器需要使用一个或一组url指定发布服务和订阅服务(publisher and subscriber locations,我翻译的不太合理)。

所有发送到发布服务的请求被视为发布请求,所有发送到订阅服务的请求被视为订阅请求。

通道(channel)需要使用唯一id进行标识,推荐使用url指定具体通道,如 localhost:8082/sub?id=1。

发布请求(publisher request)通过POST向服务器传输数据,并通知其向某些通道某些用户发送数据,

订阅请求(subscriber request)通过GET向服务器请求数据,通知其该用户想要接收数据。

 

配置信息详解

变量:

$push_channel_id

作为唯一标识,区别通信通道,要通信的pub和sub这个值必须相同。

例如:

set $push_channel_id $arg_id; 
#channel id 就是当前url上的字符串变量"id"
#(/foo/bar?id=channel_id_string)

 

 

指令:

     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长度,长的部分将会被截断,例如:

1.设置conf:

push_max_channel_id_length5;

2.输入连接:

http://192.168.0.237:2033/activity?id=lengzijian

3.更改perl脚本

        4.查看页面

         push_max_channel_subscribers[number ]

         默认值:0(unlimited)

         使用环境:main,server,location

         最大并发subscriber数,你懂得!!!!

 

总结

 

最后附上用nginx_http_push_module实现的官方的聊天室:

聊天室下载地址:http://www.2zct.com/nginx/chat.zip

 

nginx配置信息下载地址:http://www.2zct.com/nginx/nginx_conf.zip

 

稍微做了改动,js+nginx实现,如果有任何问题,可以留言解答。

 
分类:  网络通信message-queuenginx
好文要顶  关注我  收藏该文   

NGiNX_HTTP_Push_Module 是一个 Nginx 的扩展模块,它实现了 HTTP Push 和Comet server的功能。HTTP Push 被经常用在网页上主动推的技术,例如一些聊天室啊,更新信息非常频繁的应用场合。

Http Server Push是一种推送技术,服务器主动向浏览器发送数据。 

可以参考:http://wiki.nginx.org/HttpPushStreamModule

 

1. 下载安装

1) 下载源代码包
Nginx:http://nginx.org/
Nginx Http Push Module:http://pushmodule.slact.net/(网站打不开)csdn上有一份http://download.csdn.net/download/javadxz/10046650

2) 解压缩
tar zxvf nginx-1.12.2.tar.gz
tar zxvf nginx_http_push_module-0.692.tar.gz

3) 编译安装

进入nginx根目录

编译Nginx,configure指定待添加模块的路径

./configure --add-module=/home/nginx/nginx_http_push_module-0.692
 make && make install

Copy the code
错误提示:
./configure: error: the HTTP rewrite module requires the PCRE library.

安装pcre-devel与openssl-devel解决问题
yum install gcc gcc-c++ openssl_devel -y
yum -y install pcre-devel openssl openssl-devel 
./configure --prefix=/usr/local/nginx
Copy the code

 

2. 配置

关于推送配置说明,可参考推送模块源码包内的README,

推送协议可参考protocol.txt,或访问http://pushmodule.slact.net/protocol.html

在Nginx配置文件中添加一个基本的推送配置,

可参考自带的用于测试的配置文件nginx_http_push_module-0.692/tests/nginx.conf,将其中配置部分附加到Nginx配置文件的http结构中(安装后nginx启动默认读取的文件是/usr/local/nginx/conf/nginx.conf)

Copy the code
        location /chat {
            push_channel_group pushmodule_chat;
            location /chat/pub {
                 set $push_channel_id $arg_id; #多个聊天室就相当于 ?id=markdream
                 push_publisher;
                 push_message_timeout 5m;
                 push_message_buffer_length 10;
            }
            location /chat/sub {
                 set $push_channel_id $arg_id; #多个聊天室就相当于 ?id=markdream
                 push_subscriber;
                 send_timeout 3600; 
            }
        }
Copy the code

3. 调试

启动

./nginx

./nginx -s reload

./nginx -s stop

启动Nginx,使用curl进行推送测试

1) subscriber请求数据:
curl -X GET localhost:8082/chat/sub?id=1
向channel1请求数据,当前channel无数据,等待数据生成

2) publisher推送数据:
curl -X POST -d "china 2017-10-31" http://localhost:8082/chat/pub?id=1
向channel1推送数据,subscriber收到数据并返回

 

 

4. 总结

Http服务器需要使用一个或一组url指定发布服务和订阅服务(publisher and subscriber locations,我翻译的不太合理)。

所有发送到发布服务的请求被视为发布请求,所有发送到订阅服务的请求被视为订阅请求。

通道(channel)需要使用唯一id进行标识,推荐使用url指定具体通道,如 localhost:8082/sub?id=1。

发布请求(publisher request)通过POST向服务器传输数据,并通知其向某些通道某些用户发送数据,

订阅请求(subscriber request)通过GET向服务器请求数据,通知其该用户想要接收数据。

 

配置信息详解

变量:

$push_channel_id

作为唯一标识,区别通信通道,要通信的pub和sub这个值必须相同。

例如:

set $push_channel_id $arg_id; 
#channel id 就是当前url上的字符串变量"id"
#(/foo/bar?id=channel_id_string)

 

 

指令:

     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长度,长的部分将会被截断,例如:

1.设置conf:

push_max_channel_id_length5;

2.输入连接:

http://192.168.0.237:2033/activity?id=lengzijian

3. Change the perl script

        4. View page

         push_max_channel_subscribers[number ]

         Default value: 0 (unlimited)

         Use of the environment: main, server, location

         The maximum number of concurrent subscriber, you know! ! ! !

 

to sum up

 

Finally, attach the official chat room with nginx_http_push_module implementation:

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

 

nginx configuration information Download: http://www.2zct.com/nginx/nginx_conf.zip

 

Do a little change, js + nginx be achieved if you have any questions, you can shout answer.

Guess you like

Origin www.cnblogs.com/wntd/p/11428699.html