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

news/2024/7/10 1:33:03 标签: nginx, tomcat, 负载均衡, 动静分离

目录

一、背景与环境

1. 背景

2. 环境图示

3. 目标

二、操作过程

1. 第一层

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. 第一层

① 关闭所有机器防火墙

[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0

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

[root@localhost ~]# yum install epel-release.noarch -y
[root@localhost ~]# yum install nginx -y

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

[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服务

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 

[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服务器配置文件

[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

[root@localhost ~]# yum install tomcat -y

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

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 查看负载均衡动静分离 

[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

http://www.niftyadmin.cn/n/5411365.html

相关文章

媒体邀约服务主要有哪些工作

传媒如春雨,润物细无声,大家好,我是51媒体网胡老师。 媒体邀约服务是一项繁琐的沟通工作,下面介绍一下51媒体网的主要工作流程。‘ 明确需求:根据客户的传播需求,制定拟邀请媒体名单供客户筛选。 媒体筛选…

Java基于微信小程序的高校讲座预约系统,附源码

博主介绍:✌程序员徐师兄、7年大厂程序员经历。全网粉丝12w、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ 🍅文末获取源码联系🍅 👇🏻 精彩专栏推荐订阅👇…

分类算法(Classification algorithms)

逻辑回归(logical regression): 逻辑回归这个名字听上去好像应该是回归算法的,但其实这个名字只是在历史上取名有点区别,但实际上它是一个完全属于是分类算法的。 我们为什么要学习它呢?在用我们的线性回归时会遇到一…

SOC设计:关于reset的细节

有如下几个信号 1、时钟:clk_top 2、总的reset信号:rstn_top 3、scan的reset信号:scan_rstn 4、软件复位信号:rstn_soft_sub 5、scan模式信号:scan_mode 6、reset bypass 信号:scan_rstn_sel 功能&a…

centos cat命令

cat命令主要有两个功能:显示文件内容和创建文件: 1. 显示文件内容cat /dev/opt/text.log 2. 在终端中查看多个文件的内容 cat test1.txt test2.txt 3. 在终端显示文件内容带行号 cat -n test1.txt 4. 将标准输出与重定向运算符一起使用 cat t…

gan, pixel2pixel, cyclegan, srgan图像超分辨率

文章目录 1.gan2.DCgan3.cgan4.pixel2pixel(Image-to-Image Translation with Conditional Adversarial Networks)5.CycleGAN6.Deep learning for in vivo near-infrared imaging11..Photo-Realistic Single Image Super-Resolution Using a Generative …

day36 贪心算法part5

435. 无重叠区间 中等 给定一个区间的集合 intervals ,其中 intervals[i] [starti, endi] 。返回 需要移除区间的最小数量,使剩余区间互不重叠 。 气球问题稍加改动就可ac 一个交叉区间里,最终只能保留一个,其他的全部要去掉。…

Oracle常用语句语法

1 第一章Oracle命令 转载至 Oracle常用语句语法汇总 https://www.modb.pro/db/1759752946170548224 a) 系统管理员连接 conn */* as sysdba b) 查询当前用户 show user c) 创建新用户 create user 用户名 identified by 密码(密码不能以数字开头)。例如…