nginx+Tomcat(反向代理、动静分离、负载均衡)

目录

前言

一、nginx和tomcat组合的架构

二、案例操作


前言

tomcat服务既可以处理动态页面,也可以处理静态页面;但其处理静态页面的速度远远不如nginx和apache服务,但ngingx和apache服务无法直接处理动态页面,下文就讲述了nginx和tomcat组合使用,实现动静分离和负载均衡;目前很多大型网站都应用Nginx服务器作为后端网站程序的反向代理及负载均衡器,提升整个站点的负载并发能力

一、nginx和tomcat组合的架构

  • standalone模式:Tomcat单独运行,直接接受用户的请求,不推荐。
  • 单机反向代理模式:单机运行,提供了一个Nginx作为反向代理,可以做到静态由nginx提供响应,动态jsp代理给Tomcat

LNMT:Linux + Nginx + MySQL + Tomcat

LAMT:Linux + Apache(Httpd)+ MySQL + Tomcat

  • 反向代理多级:前置一台Nginx,给多台Tomcat实例做反向代理和负载均衡调度,Tomcat上部署的纯动态页面更适合

LNMT:Linux + Nginx + MySQL + Tomcat

  • 反向代理多机多级

LNNMT:Linux + Nginx + Nginx + MySQL + Tomcat

动态服务器的问题,往往就是并发能力太弱,往往需要多台动态服务器一起提供服务。如何把并发的压力分摊,这就需要调度,采用一定的调度策略,将请求分发给不同的服务器,这就是Load Balance负载均衡。当单机Tomcat,演化出多机多级部署的时候,一个问题便凸显出来,这就是Session。而这个问题的由来,都是由于HTTP协议在设计之初没有想到未来的发展

二、案例操作

前提:关闭所有设备的防火墙和核心防护

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

安装设备对应的服务软件,如nginx反向代理服务器、nginx服务器1和2都需安装nginx软件,Tomcat服务器1和2需安装tomcat软件

bash 复制代码
#nginx反向代理服务器、nginx服务器1和2
[root@localhost ~]#yum install epel-release.noarch -y
[root@localhost ~]#yum install -y nginx
[root@localhost ~]#systemctl start nginx
bash 复制代码
#Tomcat服务器1和2需安装tomcat软件
参考https://blog.csdn.net/m0_71815887/article/details/136382688?spm=1001.2014.3001.5502

注:

  • 当客户端访问的是静态资源,nginx反向代理服务器会根据负载均衡的轮询调度算法,分别调度到nginx服务器1或nginx服务器2处理静态资源(比例1:1)。如果客户端访问的是动态资源,nginx服务器1和nginx服务器2也会根据负载均衡的轮询调度算法,分别调度到tomcat服务器1或tomcat服务器2处理动态资源(比例1:1)
  • 在现实环境中,nginx反向代理服务器反向代理的nginx服务器1和nginx服务器2的网页内容是一样的,但这里,为了方便展示效果,就使用不同的网页,方便区分。tomcat服务器1和tomcat服务器2同理

客户端访问:http://172.16.12.10/index.html ---------> nginx服务器1 或 nginx服务器2

http://172.16.12.10/index.jsp ---------> tomcat服务器1 或 tomcat服务器2

nginx反向代理服务器配置:

bash 复制代码
[root@localhost ~]#vim /etc/nginx/nginx.conf
upstream web {
    server 172.16.12.11;
    server 172.16.12.12;
    }
   server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        root         /usr/share/nginx/html;
        location / {
        proxy_pass http://web/;
        }
[root@localhost ~]#nginx -t
[root@localhost ~]#nginx -s reload

nginx服务器1配置:

bash 复制代码
[root@localhost ~]#vim /etc/nginx/nginx.conf
upstream web {
    server 172.16.12.11;
    server 172.16.12.12;
    }
   server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        root         /usr/share/nginx/html;
        location / {
        proxy_pass http://web/;
        }
[root@localhost ~]#nginx -t
[root@localhost ~]#nginx -s reload
[root@localhost ~]#scp /etc/nginx/nginx.conf 172.16.12.12:/opt   #将配置文件拷贝到nginx服务器2的opt目录下
[root@localhost ~]#echo "welcome to static_server nginx 1 " > /usr/share/nginx/html/index.html

nginx服务器2配置:

bash 复制代码
[root@localhost ~]#cd /opt
[root@localhost opt]#cp nginx.conf /etc/nginx/nginx.conf
[root@localhost opt]#nginx -t
[root@localhost opt]#nginx -s reload
[root@localhost opt]#echo "welcome to static_server nginx 2 " > /usr/share/nginx/html/index.html

tomcat服务器1配置:

bash 复制代码
[root@localhost ~]#mkdir /usr/local/tomcat/webapps/dynamic
[root@localhost ~]#echo "welcome to dynamic_server tomcat 1 " > /usr/local/tomcat/webapps/dynamic/index.jsp
[root@localhost ~]#vim /usr/local/tomcat/conf/server.xml
<Context docBase="/usr/local/tomcat/webapps/dynamic"
               path="" reloadable="true"  />
[root@localhost ~]#systemctl restart tomcat

tomcat服务器2配置:

bash 复制代码
[root@localhost ~]#mkdir /usr/local/tomcat/webapps/dynamic
[root@localhost ~]#echo "welcome to dynamic_server tomcat 2 " > /usr/local/tomcat/webapps/dynamic/index.jsp
[root@localhost ~]#vim /usr/local/tomcat/conf/server.xml
<Context docBase="/usr/local/tomcat/webapps/dynamic"
               path="" reloadable="true"  />
[root@localhost ~]#systemctl restart tomcat

客户端测试:

bash 复制代码
#访问静态资源
[root@localhost ~]#curl 172.16.12.10/index.html
bash 复制代码
#访问动态资源
[root@localhost ~]#curl 172.16.12.10/index.jsp
相关推荐
三坛海会大神5551 天前
LVS与Keepalived详解(二)LVS负载均衡实现实操
linux·负载均衡·lvs
qq_264220891 天前
LVS负载均衡群集和LVS+Keepalived群集
运维·负载均衡·lvs
雨落Liy1 天前
Nginx 从入门到进阶:反向代理、负载均衡与高性能实战指南
运维·nginx·负载均衡
沐矢羽1 天前
Tomcat PUT方法任意写文件漏洞学习
学习·tomcat
Yyyy4821 天前
Nginx负载均衡集群实验步骤
运维·nginx·负载均衡
qq_264220891 天前
Nginx优化与 SSL/TLS配置
运维·nginx
matlab的学徒2 天前
Web与Nginx网站服务(改)
linux·运维·前端·nginx·tomcat
云动雨颤2 天前
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
数据库·spring boot·tomcat
✎﹏赤子·墨筱晗♪2 天前
深入浅出LVS负载均衡群集:原理、分类与NAT模式实战部署
分类·负载均衡·lvs
邂逅星河浪漫2 天前
【Docker+Nginx+Ollama】前后端分离式项目部署(传统打包方式)
java·nginx·docker·部署