Nginx+tomcat负载均衡配置

a、配置2个tomcat。直接复制一份tomcat在同级目录下,看起来是这样:
D:\apache-tomcat-6.0.26
D:\apache-tomcat-6.0.26_2

b、配置“环境变量”,新建CATALINA_HOME,值是D:\apache-tomcat-6.0.26,再新建CATALINA2_HOME,值是D:\apache-tomcat-6.0.26_2;检查两个tomcat是否都能分别启动;

c、修改D:\apache-tomcat-6.0.26\conf\server.xml 3个端口号,分别是:
<Server port="8005" shutdown="SHUTDOWN">
改成
<Server port="18005" shutdown="SHUTDOWN">(端口号随便)


<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
改成
<Connector port="18080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
               
               
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
改成
<Connector port="18009" protocol="AJP/1.3" redirectPort="8443" />


d、同样修改apache-tomcat-6.0.26_2\conf\server.xml 3个端口号,不同的是端口号要与18080不同,所以修改好之后应该是这个样子:
<Server port="18006" shutdown="SHUTDOWN">(端口号随便)

...........

<Connector port="18081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
        
...........        
               
<Connector port="18010" protocol="AJP/1.3" redirectPort="8443" />


apache-tomcat-6.0.26_2还要改个地方,apache-tomcat-6.0.26_2\bin下所有*.bat中的CATALINA_HOME改成CATALINA2_HOME。

至此2个tomcat可以同时运行了,分别打开各自的首页localhost:18080、localhost:18081


e、配置nginx就简单多了,修改D:\nginx-0.8.55\conf\nginx.conf文件2个地方:
1、在
   
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout 65;
   
    后面增加
    
upstream localhost {
        server localhost:18080;
        server localhost:18081;
        #ip_hash;
    }

    
2、在
  
location / {
            root   html;
            index  index.html index.htm;
   
    里面增加
     
    proxy_pass   http://localhost;
    proxy_redirect    off;
    proxy_set_header   Host $host;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size   10m;
    client_body_buffer_size   128k;
    proxy_connect_timeout   15;
    proxy_send_timeout   15;
    proxy_read_timeout   15;
    proxy_buffer_size   8k;
    proxy_buffers   4 64k;
    proxy_busy_buffers_size   128k;
    proxy_temp_file_write_size  128k;

    
    整个看起来应该是:
    
   
location / {
          root   html;
          index  index.html index.htm;
          proxy_pass   http://localhost;
          proxy_redirect    off;
          proxy_set_header   Host $host;
          proxy_set_header   X-Real-IP $remote_addr;
          proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
          client_max_body_size   10m;
          client_body_buffer_size   128k;
          proxy_connect_timeout   15;
          proxy_send_timeout   15;
          proxy_read_timeout   15;
          proxy_buffer_size   8k;
          proxy_buffers   4 64k;
          proxy_busy_buffers_size   128k;
          proxy_temp_file_write_size  128k;       
      }

    
    好了,全部搞定了,开启2个tomcat,在开启nginx,访问http://localhost,你会看到tomcat的首页,等等这样还没完,你随便关掉其中一个tomcat,再访问http://localhost也是可以看到tomcat的首页的,

这样,nginx+tomcat负载均衡就全部搞定了。   

     我的博客其他文章列表   
http://my.oschina.net/helu 


你可能感兴趣的:(Nginx+tomcat负载均衡配置)