访问静态和动态页面分开
实现动态的静态页面负载均衡
准备阶段:三台虚拟机
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查看动态