本章目录
1. Tomcat 多实例
1.1 实验步骤
1.2 实验流程
2. Nginx + Tomcat 负载均衡与动静分离
2.1 实验步骤
2.2 实验流程
(1)放入安装包,关闭防火墙
(2)安装 JDK 并设置环境变量
(3)安装 Tomcat
1 cd /opt 2 tar zxvf apache-tomcat-9.0.16.tar.gz 3 mkdir /usr/local/tomcat 4 mv apache-tomcat-9.0.16 /usr/local/tomcat/tomcat1 5 cp -a /usr/local/tomcat/tomcat1 /usr/local/tomcat/tomcat2
(4)配置 Tomcat 环境变量
1 vim /etc/profile.d/tomcat.sh 2 3 #tomcat1 4 export CATALINA_HOME1=/usr/local/tomcat/tomcat1 5 export CATALINA_BASE1=/usr/local/tomcat/tomcat1 6 export TOMCAT_HOME1=/usr/local/tomcat/tomcat1 7 8 #tomcat2 9 export CATALINA_HOME2=/usr/local/tomcat/tomcat2 10 export CATALINA_BASE2=/usr/local/tomcat/tomcat2 11 export TOMCAT_HOME2=/usr/local/tomcat/tomcat2
1 source /etc/profile.d/tomcat.sh
(5)修改 tomcat 中的 server.xml 文件,要求各 tomcat 示例配置不能再有重复的端口号
1 vim /usr/local/tomcat/tomcat2/conf/server.xml 2 3 <Server port="8006" shutdown="SHUTDOWN"> #22行,修改Server prot,默认为8005 -> 修改为8006 4 <Connector port="8081" protocol="HTTP/1.1" #69行,修改Connector port,HTTP/1.1 默认为8080 -> 修改为8081 5 <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" /> #116行,修改Connector port AJP/1.3,默认为8009 -> 修改为8010
(6)修改各 tomcat 实例中的 startup.sh 和 shutdown.sh 文件,添加 tomcat 环境变量
1 vim /usr/local/tomcat/tomcat1/bin/startup.sh 2 3 # ----------------------------------------------------------------------------- 4 # Start Script for the CATALINA Server 5 # ----------------------------------------------------------------------------- 6 ##添加以下内容 7 export CATALINA_BASE=$CATALINA_BASE1 8 export CATALINA_HOME=$CATALINA_HOME1 9 export TOMCAT_HOME=$TOMCAT_HOME1
1 vim /usr/local/tomcat/tomcat1/bin/shutdown.sh 2 3 # ----------------------------------------------------------------------------- 4 # Start Script for the CATALINA Server 5 # ----------------------------------------------------------------------------- 6 export CATALINA_BASE=$CATALINA_BASE2 7 export CATALINA_HOME=$CATALINA_HOME2 8 export TOMCAT_HOME=$TOMCAT_HOME2
1 vim /usr/local/tomcat/tomcat2/bin/startup.sh 2 3 # ----------------------------------------------------------------------------- 4 # Start Script for the CATALINA Server 5 # ----------------------------------------------------------------------------- 6 export CATALINA_BASE=$CATALINA_BASE2 7 export CATALINA_HOME=$CATALINA_HOME2 8 export TOMCAT_HOME=$TOMCAT_HOME2
1 vim /usr/local/tomcat/tomcat2/bin/shutdown.sh 2 3 # ----------------------------------------------------------------------------- 4 # Stop script for the CATALINA Server 5 # ----------------------------------------------------------------------------- 6 export CATALINA_BASE=$CATALINA_BASE2 7 export CATALINA_HOME=$CATALINA_HOME2 8 export TOMCAT_HOME=$TOMCAT_HOME2
1 /usr/local/tomcat/tomcat1/bin/startup.sh 2 /usr/local/tomcat/tomcat2/bin/startup.sh 3 4 netstat -natp | grep java
(1)放入安装包,关闭防火墙
(2)安装 JDK 并设置环境变量
(3)安装 Tomcat
(4)配置 Tomcat 环境变量
(5)修改 tomcat 中的 server.xml 文件,要求各 tomcat 示例配置不能再有重复的端口号
(6)修改各 tomcat 实例中的 startup.sh 和 shutdown.sh 文件,添加 tomcat 环境变量
(7)访问测试
2. Nginx + Tomcat 负载均衡与动静分离
2.1 实验步骤
(1)动静分离:tomcat server 配置
1 mkdir /usr/local/tomcat/webapps/test 2 vim /usr/local/tomcat/webapps/test/index.jsp 3 4 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 5 <html> 6 <head> 7 <title>JSP test1 page</title> #指定为 test1 页面 8 </head> 9 <body> 10 <% out.println("动态页面 1,http://www.test1.com");%> 11 </body> 12 </html>
1 vim /usr/local/tomcat/conf/server.xml 2 3 <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> 4 <Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true"> 5 </Context> 6 </Host> 7 8 /usr/local/tomcat/bin/shutdown.sh 9 /usr/local/tomcat/bin/startup.sh
1 mkdir /usr/local/tomcat/tomcat1/webapps/test /usr/local/tomcat/tomcat2/webapps/test 2 3 vim /usr/local/tomcat/tomcat1/webapps/test/index.jsp 4 5 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 6 <html> 7 <head> 8 <title>JSP test2 page</title> #指定为 test2 页面 9 </head> 10 <body> 11 <% out.println("动态页面 2,http://www.test2.com");%> 12 </body> 13 </html> 14 15 16 vim /usr/local/tomcat/tomcat1/conf/server.xml 17 18 <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> 19 <Context docBase="/usr/local/tomcat/tomcat1/webapps/test" path="" reloadable="true" /> 20 </Host> 21 22 /usr/local/tomcat/tomcat1/bin/shutdown.sh 23 /usr/local/tomcat/tomcat1/bin/startup.sh
1 vim /usr/local/tomcat/tomcat2/webapps/test/index.jsp 2 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 4 <html> 5 <head> 6 <title>JSP test3 page</title> #指定为 test3 页面 7 </head> 8 <body> 9 <% out.println("动态页面 3,http://www.test3.com");%> 10 </body> 11 </html> 12 13 vim /usr/local/tomcat/tomcat2/conf/server.xml 14 15 <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> 16 <Context docBase="/usr/local/tomcat/tomcat2/webapps/test" path="" reloadable="true" /> 17 </Host> 18 19 /usr/local/tomcat/tomcat2/bin/shutdown.sh 20 /usr/local/tomcat/tomcat2/bin/startup.sh
(2)Nginx server 配置
1 echo '<html><body><h1>这是静态页面</h1></body></html>' > /usr/local/nginx/html/index.html 2 mkdir /usr/local/nginx/html/img 3 cp /root/game.jpg /usr/local/nginx/html/img 4 5 vim /usr/local/nginx/conf/nginx.conf 6 7 ...... 8 http { 9 ...... 10 #gzip on; 11 12 #配置负载均衡的服务器列表,weight参数表示权重,权重越高,被分配到的概率越大 13 upstream tomcat_server { 14 server 192.168.80.100:8080 weight=1; 15 server 192.168.80.101:8080 weight=1; 16 server 192.168.80.101:8081 weight=1; 17 } 18 19 server { 20 listen 80; 21 server_name www.kgc.com; 22 23 charset utf-8; 24 25 #access_log logs/host.access.log main; 26 27 #配置Nginx处理动态页面请求,将 .jsp文件请求转发到Tomcat 服务器处理 28 location ~ .*\.jsp$ { 29 proxy_pass http://tomcat_server; 30 #设置后端的Web服务器可以获取远程客户端的真实IP 31 ##设定后端的Web服务器接收到的请求访问的主机名(域名或IP、端口),默认HOST的值为proxy_pass指令设置的主机名。如果反向代理服务器不重写该请求头的话,那么后端真实服务器在处理时会认为所有的请求都来在反向代理服务器,如果后端有防攻击策略的话,那么机器就被封掉了。 32 proxy_set_header HOST $host; 33 ##把$remote_addr赋值给X-Real-IP,来获取源IP 34 proxy_set_header X-Real-IP $remote_addr; 35 ##在nginx 作为代理服务器时,设置的IP列表,会把经过的机器ip,代理机器ip都记录下来 36 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 37 } 38 39 #配置Nginx处理静态图片请求 40 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ { 41 root /usr/local/nginx/html/img; 42 expires 10d; 43 } 44 45 location / { 46 root html; 47 index index.html index.htm; 48 } 49 ...... 50 } 51 ...... 52 }
(1)动静分离:tomcat server 配置
(2)Nginx server 配置
(3)访问测试
-