tomcat---动静分离

访问静态和动态页面分开

实现动态的静态页面负载均衡

实验一

准备阶段:三台虚拟机

nginx代理服务器 :20.0.0.40

tomcat1 :20.0.0.50

tomcat2:20.0.0.51

配置关闭虚拟机防火墙和安全机制

systemctl stop firewalld

setenforce 0
配置nginx代理服务器 conf/nginx

upstream tomcat {
    server ip1:8080 weight=2;
    server ip2:8080 weight=3;
}

location ~* \.jsp$ {
   proxy_pass http://tomcat;
   proxy_set_header HOST $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
   #在nginx作为代理服务器时,会把所有经过的机器的IP,以及代理地址的IP全部记录下来
 }

nginx -t 检测配置文件是否有错

systemctl restart nginx 重启服务

切换到html目录下 cd /html

<html>
<body>
<h1>this is Nginx static test !!<h1>
<img src="图片">
</body>
</html>

配置后端tomcat

进入webapps目录下创建一个目录,在目录下创建index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>tomcat01</title>   
</head>
<body>
<% out.println("tomcat01 running");%>
</body>
</html>

进入主配置文件并做备份 conf/server.conf

#删除前面的 HOST 配置
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
	<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" />
</Host>

然后进入bin目录下启动tomcat

./shutdown.sh
./stratup.sh

查看8080端口是否启动

netstat -antp | grep 8080

同理tomcat也是上述操作

实验二

四层+7层+动静分离

五台服务器

1、代理服务器nginx1:20.0.0.40

2、静态页面和动态的请求转发服务器

20.0.0.41

20.0.0.42

3、后端服务器

tomcat1:20.0.0.50

tomcat2:20.0.0.51

配置代理服务器

stream {
  upstream static {
   server 20.0.0.50:80 weight=1;
   server 20.0.0.60:80 weight=1;
  }
  server {
     listen 80;
     proxy_pass static;
  }
}

转发服务器

upstream tomcat {
   server tomcatip1:8080 weight=1;
   server tomcatip2:8080 weight=2;
}

location ~* \.jsp$ {
   proxy_pass http://tomcat;
   proxy_set_header HOST $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
   #在nginx作为代理服务器时,会把所有经过的机器的IP,以及代理地址的IP全部记录下来
 }

配置静态页面的命令

<html>
<body>
<h1>this is Nginx static test !!<h1>
<img src="图片">
</body>
</html>

同理nginx2也是一样的

tomcat不需要动,实验一已经配置完毕

在浏览器上20.0.0.40可以查看静态

20.0.0.40/index.jsp查看动态

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值