Tomcat Nginx的动静分离

1.单机反向代理:

实验 :7-3 做客户机,7-2做代理服务器,7-1 tomcat 服务器 数据传输从7-3到7-2到7-1

配置:

7-1为之前的虚拟机配置

7-2做代理服务器配置:

7-3客户机配置:

测试:

2.反向代理多机:

静态资源nginx自己响应,动态资源转交给tomcat响应

实验:Nginx要开启upstream(负载均衡)、location(url链接)、proxy_pass(反向代理)
配置:7-1做nginx代理服务器;7-2和 7-3做Tomcat服务器

配置:

7-1安装nginx;7-3 和 7-2安装Tomcat;都开启服务!

配置7-1Nginx服务器:

bash 复制代码
 vim /etc/nginx/conf/nginx.conf
 
 17 http {
 18      upstream   tomcat {
 19      server     192.168.91.4:8080;
 20      server     192.168.91.5:8080;
 21 }
 
 48         location  ~* \.(jpg|png)$  {
 49         root      /data/html;
 50 }
 51 
 52         location  /  {
 53         proxy_pass  http://tomcat;
 54 }

建立静态资源存放在本机中

配置7-2Tomcat服务器:

bash 复制代码
[root@localhost ~]# cd /usr/local/tomcat/webapps/ROOT
[root@localhost ROOT]# 
[root@localhost ROOT]# ls
asf-logo-wide.svg  bg-upper.png       tomcat.css        tomcat.svg
bg-button.png      favicon.ico        tomcat.gif        WEB-INF
bg-middle.png      index.jsp          tomcat.png
bg-nav.png         RELEASE-NOTES.txt  tomcat-power.gif
[root@localhost ROOT]# 
[root@localhost ROOT]# vim test.jsp
[root@localhost ROOT]# cat test.jsp
7-2
[root@localhost ROOT]#

配置7-3Tomcat服务器 :

bash 复制代码
[root@centos1 ~]# cd /usr/local/tomcat/webapps/ROOT
[root@centos1 ROOT]# ls
asf-logo-wide.svg  bg-upper.png       tomcat.css        tomcat.svg
bg-button.png      favicon.ico        tomcat.gif        WEB-INF
bg-middle.png      index.jsp          tomcat.png
bg-nav.png         RELEASE-NOTES.txt  tomcat-power.gif
[root@centos1 ROOT]# vim test.jsp
[root@centos1 ROOT]# cat test.jsp
7-1
[root@centos1 ROOT]#

测试:

3.反向代理多机多级:

实验:7-3为Nginx代理服务器;7-4和7-5为Nginx服务器;7-1和7-2为Tomcat服务器
配置:

7-1:192.168.91.3

7-2:192.168.91.4

7-3:192.168.91.5

7-4:192.168.91.7

7-5:192.168.91.8

第一层:

7-3Nginx代理服务器传内容给7-4和7-5Nginx服务器

配置:

配置7-3Nginx代理服务器:
bash 复制代码
 vim /etc/nginx/nginx.conf
 17 http {
 18      upstream   web {
 19      server     192.168.91.7;
 20      server     192.168.91.8;
 21 }
 
 48         location  /  {
 49         proxy_pass  http://web;
 50 }
 51 
配置7-4Nginx服务器:
bash 复制代码
[root@centos4 ~]# vim /usr/share/nginx/html/index.html
[root@centos4 ~]# 
[root@centos4 ~]# cat /usr/share/nginx/html/index.html
7-4
[root@centos4 ~]#
配置7-5Nginx服务器:
bash 复制代码
[root@centos5 ~]# vim /usr/share/nginx/html/index.html
[root@centos5 ~]# cat /usr/share/nginx/html/index.html
7-5
[root@centos5 ~]#

测试:

bash 复制代码
[root@localhost ~]# curl 192.168.91.5
7-4
[root@localhost ~]# curl 192.168.91.5
7-5
[root@localhost ~]# curl 192.168.91.5
7-4
[root@localhost ~]#

第二层:

7-4和7-5Nginx服务器传内容给7-1和7-2Tomcat服务器

配置:

配置7-4Nginx服务器:
bash 复制代码
 vim /etc/nginx/nginx.conf
 
 29     upstream  tomcat  {
 30         server 192.168.91.3:8080;
 31         server 192.168.91.4:8080;
 32 }
 
 
 48         location ~*  \.jsp$  {
 49         proxy_pass  http://tomcat;
 50 }
 51 
 52         location ~*  \.html$  {
 53         root        /usr/share/nginx/html;
 54 }
配置7-5Nginx服务器:
bash 复制代码
 vim /etc/nginx/nginx.conf
 
 30     upstream  tomcat  {
 31         server  192.168.91.3:8080;
 32         server  192.168.91.4:8080;
 33 }
 
 49         location  ~* \.jsp$ {
 50         proxy_pass      http://tomcat;
 51 }
 52 
 53         location  ~* \.html$ {
 54         root            /usr/share/nginx/html;
 55 }

测试:

curl访问,验证第二层配置(7-3Nginx代理服务器 传内容给 7-4和7-5Nginx服务器;再给到7-1和7-2Tomcat服务器

当访问静态资源(.html)时:
bash 复制代码
[root@localhost ~]# curl 192.168.91.5/index.html
7-5
[root@localhost ~]# curl 192.168.91.5/index.html
7-4
[root@localhost ~]# curl 192.168.91.5/index.html
7-5
[root@localhost ~]# 
当访问动态资源(.jsp)时:
bash 复制代码
[root@localhost ~]# curl 192.168.91.5/test.jsp
7-1
[root@localhost ~]# curl 192.168.91.5/test.jsp
7-1
[root@localhost ~]# curl 192.168.91.5/test.jsp
7-2
[root@localhost ~]# curl 192.168.91.5/test.jsp
7-2
[root@localhost ~]#
相关推荐
TG:@yunlaoda360 云老大1 小时前
华为云国际站代理商CSBS主要有什么作用呢?
运维·服务器·数据库·华为云
像名字一样难取的昵称2 小时前
Linux学习笔记:十八、Linux文件的压缩,解压缩一站式学习
linux·运维·笔记·学习·ubuntu·ssh
Trouvaille ~3 小时前
【Linux】库制作与原理(二):ELF格式与静态链接原理
linux·运维·c语言·操作系统·动静态库·静态链接·elf文件
写代码的橘子n4 小时前
IPV6复习(基础入手版)
运维·服务器·网络
ICT技术最前线4 小时前
H3C双WAN口策略路由配置技术教程
运维·网络·h3c·策略路由
一分半心动4 小时前
windows docker desktop 安装VibeVoice
运维·docker·容器
向日葵.4 小时前
中间件交接文档
linux·运维·服务器
LucidX4 小时前
Docker核心操作实战
运维·docker·容器
隔壁阿布都4 小时前
Docker Compose中的网络管理
运维·docker·容器
云和数据.ChenGuang4 小时前
运维工程师技术教程之Pull Requests(PR)
运维·分布式·git·数据库运维工程师·运维教程