基于nginx_HTTP_Push_Module的服务器推送技术实战


“服务器推送”很久很久以前就注意到这个名词了,无奈技术不行,没有怎么去研究它,又加上自己是搞PHP的,看到java的DWR框架也实现了诸如服务器推送的功能,看了代码是无比的吃力,最后还是使用谷歌搜了一下有关服务器推送的资料,一个国外的牛人也写了一个采用nginx来实现服务器推送的模块“”。官方上面就有例子了,你会发现右上角的聊天窗口是在进行的……

好,现在我们就来使用这个模块来本地化搭建一个服务器推送的网页聊天。该实验我都是在VMware中进行的哦。

首先请准备的工具有:

1、Linux操作系统(FreeBSD 9.0 i386

2、nginx最新版(下载

3、NGiNX_HTTP_Push_Module(下载

好了,关于操作系统怎么安装,VMware怎么安装这些不是本次讲的,请自行谷歌,没有上面这几个条件我也奉劝你也别尝试了,否则打击很大。

我们先来讲讲nginx安装,其实nginx安装是十分简单的啦,我在这里分两种情况,一种是你已经安装nginx了,还有一种就是全新安装nginx,像我们在实验的是属于第二种

先来说说全新安装nginx,如果你是新安装的FreeBSD是需要先安装pcre和wget的

 

#安装wget
cd /usr/ports/ftp/wget
make install clean
rehash

#安装Nginx所以来的pcre
cd /usr/ports/devel/pcre
make install clean

 

下面开始下载我们需要的软件

 

cd /usr/local/src/

#先下载nginx_http_push_module
wget http://pushmodule.slact.net/downloads/nginx_http_push_module-0.692.tar.gz

tar -zxvf nginx_http_push_module-0.692.tar.gz

#下载nginx
wget http://nginx.org/download/nginx-1.2.7.tar.gz

tar -zxvf nginx-1.2.7.tar.gz

cd nginx-1.2.7

 ./configure --with-http_ssl_module --user=www --group=www --add-module=/usr/local/src/nginx_http_push_module-0.692

make && make install clean

 

然后就是一个漫长的安装过程啦,全新安装nginx+nginx_http_push_module模块就已经安装好了。下面这个是你已经安装好nginx了,并且要添加nginx模块的操作(全新安装的可以忽略不看)

 

 

#因为你的nginx已经安装了先查看安装的配置参数,默认的nginx安装在/usr/local/nginx

/usr/local/nginx/sbin/nginx -V

#将后面的参数都复制起来

cd /usr/local/src/nginx-1.2.7

./configure --with-http_ssl_module --user=www --group=www --add-module=/usr/local/src/nginx_http_push_module-0.692

#开始编译记住不是安装
make

make 完之后,我们将nginx的二进制文件拷贝一份(这是良好的习惯)

 

 

#先停止nginx
pkill -9 nginx

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

#将新编译的nginx二进制文件覆盖原来已经存在的
cp  /usr/local/src/nginx-1.2.7/objs/nginx /usr/local/nginx/sbin/

好了,至此你已经成功将nginx_http_push_module添加到nginx中了

#查看nginx安装参数
/usr/local/nginx/sbin/nginx -V

 

 

20130315105300

 

===========================到这里你已经解决了上面的安装问题了=========================

现在我们再谈nginx_http_push_module的配置了(push-demo下载)

我们修改nginx的配置文件

 

cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
vi /usr/local/nginx/conf/nginx.conf

我的nginx配置文件,没有问题的话放到你的服务器上也成功运行的(因为都是默认的)

 

 

worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

		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; 
			}
		}

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

然后nginx重新载入配置文件

 

/usr/local/nginx/sbin/nginx -s reload

OK, 你将我的代码放在网站根目录,默认就应该在/usr/local/nginx/html/中即可

 

然后激动人心时刻到啦,打开浏览器输入:https://i.markdream.com/push/demo.html (建议打开两个不同类型的浏览器进行测试,嘎嘎)

这个是聊天室2:https://i.markdream.com/push/demo2.html

OK,到这里一个基本的聊天基于服务器推送已经完成,关于怎么扩展聊天这个看你自己的想象力了(有空我会整理一个)。


《“基于nginx_HTTP_Push_Module的服务器推送技术实战”》 有 2 条评论

  1. 请问,nginx从B发送消息到A,会不会A的长连接每次会断开从新连接啊?
    还有就是能不能做到A在接收到消息后能不能发送一个返回值,告诉B我已经收到消息?
    A和B与nginx之间的长连接,能不能实现接收和发送消息?
    我目前的阶段是A和B与nginx之间的长连接不能实现接收数据,只能发送数据。请问有没有解决方法?

回复 刘东 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

评论审核已启用。您的评论可能需要一段时间后才能被显示。