nginx+多个tomcat的负载均衡

网络架构
nginx主机:10.10.54.87
tomcat主机1:10.10.54.87
tomcat主机2:10.10.54.87

(1)安装nginx-1.4.5
[root@nan87 lnmp]# tar xvf nginx-1.4.5.tar.gz 
[root@nan87 lnmp]# cd nginx-1.4.5
[root@nan87 nginx-1.4.5]# ./configure --prefix=/usr/local/nginx --user=apache --group=apache --with-http_stub_status_module   --with-http_gzip_static_module --with-http_ssl_module
[root@nan87 nginx-1.4.5]# make && make install
[root@nan87 nginx-1.4.5]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
			--启动nginx
(2)配置DNS
[root@nan87 ~]# vim /etc/named.conf 
options {
        listen-on port 53 { any; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        allow-query     { any; };
        recursion yes;
};

logging {
        channel default_debug {
                file "data/named.run";
                severity dynamic;
        };
};

zone "." IN {
        type hint;
        file "named.ca";
};
zone "dogs.com" IN {
        type master;
        file "named.dogs.com";
};  

[root@nan87 ~]# vim /var/named/named.dogs.com 
$TTL    600
@       IN      SOA     dogs.com.        root (2014030401 1H 15M 1W 1D);
@       IN      NS      dogs.com.
dogs.com.        IN      A       10.10.54.87
www.dogs.com.    IN      A       10.10.54.87
bbs.dogs.com.    IN      A       10.10.54.87
hr.dogs.com.     IN      A       10.10.54.87

(3)安装JDK
[root@nan87 lnmp]# rpm -ivh jdk-7u51-linux-x64.rpm 
[root@nan87 lnmp]# rpm -ql jdk-1.7.0_51-fcs.x86_64
--查看JDK的安装路径
/usr/java/jdk1.7.0_51

(4)安装配置tomcat
[root@nan87 lnmp]# tar xvf apache-tomcat-7.0.52.tar.gz 
[root@nan87 lnmp]# mv apache-tomcat-7.0.52 /usr/local/tomcat

//给tomcat添加JAVA主目录,在96行添加
[root@nan87 lnmp]# vim /usr/local/tomcat/bin/catalina.sh 
JAVA_HOME=/usr/java/jdk1.7.0_51
CATALINA_HOME=/usr/local/tomcat

//做启动脚本
[root@nan87 lnmp]# cd /usr/local/tomcat/bin/
[root@nan87 bin]# cp catalina.sh /etc/init.d/tomcat 
[root@nan87 bin]# vim /etc/init.d/tomcat 
#chkconfig: 2375 74 76
#description:tomcat start stop
[root@nan87 bin]# chmod +x /etc/init.d/tomcat 

(5)第二台tomcat
[root@nan87 lnmp]# tar xvf apache-tomcat-7.0.52.tar.gz 
[root@nan87 lnmp]# mv apache-tomcat-7.0.52 /usr/local/tomcat2
//给tomcat添加JAVA主目录,在96行添加
[root@nan87 lnmp]# vim /usr/local/tomcat2/bin/catalina.sh 
JAVA_HOME=/usr/java/jdk1.7.0_51
CATALINA_HOME=/usr/local/tomcat2
//做启动脚本
[root@nan87 lnmp]# cd /usr/local/tomcat2/bin/
[root@nan87 bin]# cp catalina.sh /etc/init.d/tomcat2 
[root@nan87 bin]# vim /etc/init.d/tomcat2 
#chkconfig: 2355 73 75
#description:tomcat start stop
[root@nan87 bin]# chmod +x /etc/init.d/tomcat 

//更改配置
[root@nan87 lnmp]# cd /usr/local/tomcat2
//更改三个端口 
[root@nan87 tomcat2]# vim conf/server.xml
##将8005改为其他,如:9005
 <Server port="8005" shutdown="SHUTDOWN"> 
##将8080改为其他,如:9090
 <Connector port="8080" protocol="HTTP/1.1" 
##将8009改为其他,如:9009
 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> 

[root@nan87 ~]# mkdir -p /var/www/
[root@nan87 ~]# cd /var/www/
[root@nan87 www]# mkdir ROOT		--注意必须是ROOT
[root@nan87 www]# cd ROOT/
[root@nan87 html]# echo "Hello10.10.54.87" >index.jsp
[root@nan87 html]# vim /usr/local/tomcat2/conf/server.xml 
 <Host name="localhost"  appBase="/var/www"
		--appBase地址更改
(6)更改nginx配置
[root@nan87 html]# vim /usr/local/nginx/conf/nginx.conf
user apache apache;
worker_processes  2;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
			--添加下面内容
    upstream  www.dogs.com  {
               server   10.10.54.87:8080  weight=1;
               server   10.10.54.87:9090  weight=1;
       }
    server {
        listen       80;
        server_name  www.dogs.com;
        root /var/www/html;
        index index.html index.htm index.jsp;
        location / {
        proxy_pass        http://www.dogs.com;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
         location ~ .*.jsp$ {
        index index.jsp;
        proxy_pass http://localhost:8080;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires      30d;
        }
        location ~ .*\.(js|css)?$ {
        expires      1h;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

(7)测试
在浏览器中输入“http://www.dogs.com/”
刷新页面会有变化


你可能感兴趣的:(nginx+多个tomcat的负载均衡)