首页 > 代码库 > Nginx+Tomcat多实例负载均衡
Nginx+Tomcat多实例负载均衡
业务需求
1、在一台服务器上创建3个Tomcat实例
2、Nginx服务器负责调度Tomcat服务器
3、它的域名是:tomcat.zjzd.cn
1、IP地址规划
172.16.30.1 Nginx
172.16.30.2 Tomcat
172.16.30.254 Windows
172.16.30.2 Tomcat
2、安装JDK并配置环境
#/etc/init.d/iptables stop #setenforce 0 #wget http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz //也可以通过百度下载 #mv jdk-7u79-linux-x64.tar.gz jdk1.7.0_79.tar.gz #tar xf jdk1.7.0_79.tar.gz #mkdir /usr/java/ #mv jdk1.7.0_79 /usr/java/ #vim /etc/profile export JAVA_HOME=/usr/java/jdk1.7.0_79 export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOME/bin #source /etc/profile #echo $JAVA_HOME
3、搭建tomcat多实例
(1)搭建第一个tomcat实例
#wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-6/v6.0.48/bin/apache-tomcat-6.0.48.tar.gz #tar xf apache-tomcat-6.48.tar.gz #mv apache-tomcat-6.48 /usr/local/tomcat1 #cp /usr/local/tomcat1/conf/server.xml /usr/local/tomcat1/conf/server.xml.bak #vim /usr/local/tomcat1/conf/server.xml <Context path="/" docBase="/data/webapps/www1" reloadable="true"/> #mkdir -p /data/webapps/www1 #cd /data/webapss/www1/ #vim index.jsp <html> <body> <h1>Tomcat 1 jsp test page</h1> <%=new java.util.Date()%> </body> </html> #/usr/local/tomcat1/bin/startup.sh #ps -ef | grep tomcat
(2)搭建第二个tomcat实例
#cp -a /usr/local/tomcat1 /usr/local/tomcat2 #cp -a /data/webapps/www1 /data/webapps/www2 #vim /usr/local/tomcat2/conf/server.xml <Context path="/" docBase="/data/webapps/www2" reloadable="true"/> #vim /data/webapps/www2/index.jsp <html> <body> <h1>Tomcat 2 jsp test page</h1> <%=new java.util.Date()%> </body> </html> #sed -i ‘s/8005/8006/g;s/8009/8010/g;s/8080/8081/g‘ /usr/local/tomcat2/conf/server.xml #/usr/local/tomcat2/bin/startup.sh #ps -ef | grep tomcat
(3)搭建第三个tomcat实例
#cp -a /usr/local/tomcat1 /usr/local/tomcat3 #cp -a /data/webapps/www1 /data/webapps/www3 #vim /usr/local/tomcat3/conf/server.xml <Context path="/" docBase="/data/webapps/www3" reloadable="true"/> #vim /data/webapps/www3/index.jsp <html> <body> <h1>Tomcat 3 jsp test page</h1> <%=new java.util.Date()%> </body> </html> #sed -i ‘s/8005/8007/g;s/8009/8011/g;s/8080/8082/g‘ /usr/local/tomcat3/conf/server.xml #/usr/local/tomcat3/bin/startup.sh #ps -ef | grep tomcat
测试:http://172.16.30.2:8080 http://172.16.30.2:8081 http://172.16.30.2:8082
172.16.30.1 Nginx
4、配置Nginx负载均衡
(1)安装Nginx
#/etc/init.d/iptables stop #setenforce 0 #useradd www #yum install pcre pcre-devel openssl openssl-devel #wget http://nginx.org/download/nginx-1.10.2.tar.gz #tar -xf nginx-1.10.2.tar.gz #cd nginx-1.10.2.tar.gz #./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-pcre #make && make install #/usr/local/nginx/sbin/nginx -t #/usr/local/nginx/sbin/nginx #netstat -tunlp | grep 80
(2)配置主配置文件
#cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak #vim /usr/local/nginx/conf/nginx.conf worker_processes 1; error_log logs/error.log; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; include domains/*; //虚拟主机目录 }
(3)配置负载均衡虚拟主机配置文件
#mkdir /usr/local/nginx/conf/domains #vim /usr/local/nginx/conf/domains/tomcat.zjzd.cn upstream tomcat_web { server 172.16.30.2:8080 weight=1 max_fails=2 fail_timeout=30s; server 172.16.30.2:8081 weight=1 max_fails=2 fail_timeout=30s; server 172.16.30.2:8082 weight=1 max_fails=2 fail_timeout=30s; } server { listen 80; server_name tomcat.zjzd.cn; location / { proxy_pass http://tomcat_web; index index.jsp index.html; } } #/usr/local/nginx/sbin/nginx -t #/usr/local/nginx/sbin/nginx -s reload
(4)测试
172.16.30.254 Windows
首先找到C:\Windows\System32\drivers\etc\hosts文件,然后右击选择"Edit with notepad++"添加172.16.30.1 tomcat.zjzd.cn,并通过浏览器访问进行测试
http://tomcat.zjzd.cn //平均调度这三个实例
图1.1
图1.2
图1.3
5、配置Nginx动静分离
#vim /usr/local/nginx/conf/domains/tomcat.zjzd.cn location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root /data/webapps/www; #expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力 expires 3d; } #mkdir -p /data/webapps/www #vim /data/webapps/www/index.html this s a static page! #cd /data/webapps/www #wget http://www.baidu.com/img/bd_logo1.png #mv bd_logo1.png log.png #/usr/local/nginx/sbin/nginx -t #/usr/local/nginx/sbin/nginx -s reload
测试:http://tomcat.zjzd.cn/index.html http://tomcat.zjzd.cn/log.png
图2
图3.1
图3.2
注:需要对图3.1和图3.2说明的是,当浏览器第一次请求log.png图片时会返回200的状态码,代表是从远程服务器上请求的,第二次请求log.png时会返回304的状态码,代表请求的是从本地服务器上缓存的内容。在nginx配置文件中设置的静态内容过期时间为3天。当log.png文件失效时,例如替换log.png文件时。当再次刷新浏览器,会请求新的内容,但是新的内容不会正常显示。
图4
注:当停掉172.16.30.2所有的Tomcat服务时,静态内容会正常访问,但是动态内容则不会正常访问,因为做了动静分离,所以才会产生这种结果。
本文出自 “紫禁之巅” 博客,请务必保留此出处http://zjzd86.blog.51cto.com/6124664/1877789
Nginx+Tomcat多实例负载均衡