NGINX_HTTP_Push_Module是一个Nginx服务器模块,可以实现服务器端的长连接和推送(但是推送一次后会结束进程,尚未实现持续的推送)。
http://pushmodule.slact.net可以下载该模块。
如果已安装Nginx,需要重新编译安装Nginx,要用命令"./configure--add-module=/home/hfahe/ngnix_http_push_module"来配置,然后执行make和makeinstall来安装Nginx。
安装完成后,在Nginx配置文件的Server段里添加两个路径配置:
- 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;
- }
重启Nginx后,请求http://localhost/activity?id=10000的地址,可以看到浏览器处于请求堵塞阶段。
使用一段Perl脚本来Post数据到publish地址:
- use LWP::UserAgent;
- use HTTP::Request::Common;
- my $ua = new LWP::UserAgent;
- my $response = $ua->request(
- POST 'http://localhost//publish?id=10000',
- Content_Type => 'text/html',
- Content => 'Server Push'
- );
- my $content = $response->content;
- print $content;
运行后,可以看到浏览器已经输出了推送的内容。
如果要完成一个大的消息系统,还需要实现持续的推送机制,另外还需要实现Nginx和后台服务器的交互(或者考虑AgentZh推的Nginx编程,呵呵)。