用Nginx配置负载均衡服务器

暂时手头上没有Linux机器,只好拿windows来试验了。

所需工具:

  1. Nginx。下载地址:http://nginx.org/download/nginx-1.2.6.zip
  2. tomcat。这个下载地址就不提供了。

一、配置tomcat

  因为是在一台机器上做负载均衡,所以必须保证能有2台服务器,这就需要我们能在一台机器上同时启动2个tomcat。所以需要先设置一下tomcat。

解压tomcat之后,我们拷贝一份,分别为tomcat1和tomcat2。tomcat1里面的配置文件就不需要改了。仅仅只需要改tomcat2。

找到tomcat2的安装目录下的conf子目录中的server.xml文件:

<!-- 初始端口号为8005-->
<Server port="8055" shutdown="SHUTDOWN">
<!--  初始端口号为8080-->
<Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
 <!-- 初始端口号为8009-->
 <Connector port="8099" protocol="AJP/1.3" redirectPort="8443" />

经过上述三个步骤的修改就可以同时启动2个tomcat了。

可以在浏览器中输入如下网址测试下:

http://localhost:8080/

http://localhost:8081/

 

二、配置Nginx

  首先解压下载得到的Nginx,然后找到【...\nginx-1.2.6\conf】目录下的 nginx.conf文件,做如下修改: 

http {
    include       mime.types;
    default_type  application/octet-stream;

    .......

    #gzip  on;

    #从这里开始就是加入的了   ①
    upstream myhost {  
       server localhost:8080 weight=3;  #权重,我这里随便写的
       server localhost:8081 weight=5;  
    }  
    #到这里为止了   ①


    server {
        listen       8090; #这里本来是80,被我改成8090了
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

            #从这里开始就是加入的了   ②
            proxy_pass http://myhost;
            #到这里为止了   ②
        }

        ......
    }

到这里就修改完了。

 三、启动测试

 打开cmd,进入到Nginx目录,执行nginx.exe。

然后打开浏览器,输入地址:http://localhost:8090/

你看到了什么?tomcat的主页面,yes,你成功了。这样一个简单的负载均衡服务器就搭建好了。

 

 

 

posted @ 2012-12-28 23:03  yejg1212  阅读(311)  评论(0编辑  收藏  举报