Nginx+Tomcat实现负载均衡动静分离

目录

一、背景与环境

[1. 背景](#1. 背景)

[2. 环境图示](#2. 环境图示)

[3. 目标](#3. 目标)

二、操作过程

[1. 第一层](#1. 第一层)

[2. 第二层](#2. 第二层)


一、背景与环境

1. 背景

在一个Web应用程序中,通常会将动态内容(由Tomcat处理)与静态内容(如图片、CSS、JavaScript文件等)分开处理,以提高性能和扩展性。Nginx可以作为反向代理服务器,将动态请求转发给Tomcat服务器处理,同时直接提供静态内容,实现负载均衡和动静分离。下面将以相关案例进一步介绍。

2. 环境图示

3. 目标

① 7-0 基于反向代理,负载均衡可以调度到 7-1 和 7-2

② 7-1 和7-2 动静分离,自身处理静态资源,动态资源交给 7-3 和 7-4

③ 最终客户端访问代理服务器 7-0 实现负载均衡与动静分离

二、操作过程

1. 第一层

① 关闭所有机器防火墙

bash 复制代码
[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0

② 7-0,7-1,7-2 yum安装nginx

bash 复制代码
[root@localhost ~]# yum install epel-release.noarch -y
[root@localhost ~]# yum install nginx -y

③ 修改 7-0 代理服务器配置

bash 复制代码
[root@localhost ~]# vim /etc/nginx/nginx.conf
 18         upstream web {
 19         server 192.168.190.101;
 20         server 192.168.190.102;
 21         }

 49         location / {
 50         proxy_pass http://web;
 51         }
[root@localhost ~]# nginx -t
[root@localhost ~]# systemctl start nginx.service
[root@localhost ~]# nginx -s reload

④ 分别修改 7-1,7-2 nginx主目录web文件,并开启nginx服务

bash 复制代码
192.168.190.101:
[root@localhost ~]# vim /usr/share/nginx/html/index.html
7-1
[root@localhost ~]# systemctl start nginx.service

192.168.190.102:
[root@localhost ~]# vim /usr/share/nginx/html/index.html
7-2
[root@localhost ~]# systemctl start nginx.service

⑤ 访问代理服务器 7-0

bash 复制代码
[root@localhost ~]# curl 192.168.190.100
7-2
[root@localhost ~]# curl 192.168.190.100
7-1
[root@localhost ~]# curl 192.168.190.100
7-1
[root@localhost ~]# curl 192.168.190.100
7-2

2. 第二层

① 分别修改 7-1,7-2 web服务器配置文件

bash 复制代码
[root@localhost ~]# vim /etc/nginx/nginx.conf
 29         upstream tomcat {
 30         server 192.168.190.103:8080;
 31         server 192.168.190.104:8080;
 32         }

 48         location ~* \.jsp$ {
 49         proxy_pass http://tomcat;
 50         }
 51         
 52         location ~* \.html$ {
 53         root         /usr/share/nginx/html;
 54         }
[root@localhost ~]# nginx -t
[root@localhost ~]# nginx -s reload

② 7-3,7-4 分别安装tomcat

bash 复制代码
[root@localhost ~]# yum install tomcat -y

③ 7-3,7-4 分别新建tomcat服务项目文件

bash 复制代码
192.168.190.103,7-3:
[root@localhost ~]# cd /usr/share/tomcat/webapps
[root@localhost webapps]# mkdir test
[root@localhost webapps]# echo 7-3 > test/index.jsp
[root@localhost webapps]# systemctl start tomcat

192.168.190.104,7-4
[root@localhost ~]# cd /usr/share/tomcat/webapps
[root@localhost webapps]# mkdir test
[root@localhost webapps]# echo 7-4 > test/index.jsp
[root@localhost webapps]# systemctl start tomcat

④ 访问代理访问 7-0 查看负载均衡与动静分离

bash 复制代码
[root@localhost ~]# curl 192.168.190.100/test/index.jsp
7-4
[root@localhost ~]# curl 192.168.190.100/test/index.jsp
7-3
[root@localhost ~]# curl 192.168.190.100/test/index.jsp
7-3
[root@localhost ~]# curl 192.168.190.100/test/index.jsp
7-4
[root@localhost ~]# curl 192.168.190.100/test/index.j
相关推荐
hqxstudying5 小时前
mybatis过渡到mybatis-plus过程中需要注意的地方
java·tomcat·mybatis
云川之下9 小时前
【网络】使用 DNAT 进行负载均衡时,若未配置配套的 SNAT,回包失败
运维·网络·负载均衡
檀越剑指大厂10 小时前
【Nginx系列】查看 Nginx 的日志
运维·nginx
yuxb7315 小时前
集群与负载均衡:HAProxy 与 Nginx 实践
运维·nginx·负载均衡
做一位快乐的码农16 小时前
房屋装修设计管理系统的设计与实现/房屋装修管理系统
java·struts·spring·eclipse·tomcat·maven
努力买辣条1 天前
基于 Docker 的高可用 WordPress 集群部署:分布式 Nginx + Keepalived、MySQL 主从复制与 ProxySQL 读写分离
分布式·nginx·docker
川石课堂软件测试1 天前
技术干货|使用Prometheus+Grafana监控Tomcat实例详解
redis·功能测试·单元测试·tomcat·测试用例·grafana·prometheus
萌虎爱分享1 天前
Docker容器化部署实战:Tomcat与Nginx服务配置指南
nginx·docker·tomcat·容器部署
牛奶咖啡131 天前
云计算核心技术之云网络技术
云计算·负载均衡·云网络技术·专有/私有网络vpc·vpc系统架构·云网络关键技术·容器云网络技术
Fireworkitte2 天前
Ubuntu 系统 tar 包源码安装 Nginx
linux·nginx·ubuntu