Nginx+Tomcat负载均衡,动静分离群集 原理加实验

Tomcat概述

Tomcat简介:

●最初是由Sun的软件构架师詹姆斯.邓肯.戴维森开发

●安装Tomcat后,安装路径下面的目录和文件,是使用或者配置Tomcat的重要文件

Tomcat重要目录

  • Bin:存放启动和关闭Tomcat脚本
  • Conf:存放Tomcat不同的配置文件
  • Doc:存放Tomcat文档
  • Lib:存放Tomcat运行需要的库文件
  • Logs:存放Tomcat执行时的LOG文件
  • Src:存放Tomcat的源代码
  • Webapps:Tomcat的主要Web发布目录
  • Work:存放jsp编译后产生的class文件

Nginx应用

●Nginx是一款非常优秀的HTTP服务器软件

  • 支持高达50000个并发连接数的响应
  • 拥有强大的静态资源处理能力
  • 运行稳定
  • 内存,CPU等系统资源消耗非常低

●目前很多大型网站都应用Nginx服务器作为后端网站程序的反向代理及负载均衡器,提升整个站点的负载并发能力

Nginx负载均衡实现原理

●Nginx实现负载均衡是通过反向代理实现

●反向代理原理
在这里插入图片描述
Nginx配置反向代理的主要参数
●Upstream 服务池名{ }

  • 配置后端服务器池,以提高响应数据

●Proxy_pass http://服务池名

  • 配置将访问请求转发给后端服务器池的服务器处理

Nginx动静分离实现原理

●动静分离原理

  • 服务端接收来自客户端的请求中,既有静态资源也有动态资源,静态资源由Nginx提供服务,动态资源Nginx转发至后端
    在这里插入图片描述

●Nginx静态处理优势

  • Nginx处理静态页面的效率远高于Tomcat的处理能力
  • 若Tomcat的请求量为1000次,则Nginx的请求量为6000次
  • Tomcat每秒的吞吐量为0.6M,Nginx的每秒吞吐量为3.6M
  • Nginx处理静态资源的能力是Tomcat处理的6倍

实验

实验环境

  • nginx虚拟机 192.168.148.135 nginx服务
  • tomcat虚拟机 192.168.148.136 tomcat服务
  • tomcat虚拟机 192.168.148.137 tomcat服务

Nginx+Tomcat动静分离实验

推荐步骤

这边做动静分离只需要其中两台虚拟机,一台ngxin虚拟机和一台tomcat虚拟机
●一.先配置nginx虚拟机
1.将nginx软件包放到xhell连接器中,安装nginx

[root@localhost ~]# hostnamectl set-hostname nginx   ##修改主机名方便区分
[root@nginx ~]# setenforce 0    ##关闭防护系统
[root@nginx ~]# iptables -F      ##清空防火墙规则
[root@nginx ~]# useradd -M -s /sbin/nologin nginx        ##建立管理用户
[root@nginx ~]# tar zxvf nginx-1.12.2.tar.gz              ##解压文件
[root@nginx ~]# cd nginx-1.12.2/
[root@nginx ~]# ./configure \                   
--prefix=/usr/local/nginx \   ##指定路径
--user=nginx \           ##管理用户
--group=nginx          ##管理属组
[root@web1 nginx-1.12.2]# make&&make install          ##编译安装
[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin/    ####建立软链接方便管理
[root@nginx nginx-1.12.2]# cd /etc/init.d/      ##建立启动脚本添加使用service工具进行管理
[root@nginx init.d]# vim nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
  start)
   $PROG
   ;;
  stop)
   kill -s QUIT $(cat $PIDF)
   ;;
  restart)
   $0 stop
   $0 start
   ;;
  reload)
   kill -s HUP $(cat $PIDF)
   ;;
  *)
    echo "Usage: $0 {start|stop|restart|reload}"
    exit 1
esac
exit 0 
[root@nginx init.d]# chmod +x nginx     ##增加执行权限
[root@nginx init.d]# chkconfig --add nginx      
[root@nginx init.d]# service nginx start 

在这里插入图片描述
2.nginx配置完成后开始配置tomcat

[root@localhost ~]# hostnamectl set-hostname tomcat01
[root@localhost ~]# su
[root@tomcat01 ~]# setenforce 0
[root@tomcat01 ~]# iptables -F
[root@tomcat01 ~]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
[root@tomcat01 ~]# vim /etc/profile     ##修改环境变量
....在底行加入
export JAVA_HOME=/usr/local/jdk1.8.0_91     ##家目录
export JRE_HOME=${JAVA_HOME}/jre                ##JREhome
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib      ##class文件
export PATH=${JAVA_HOME}/bin:$PATH       ##系统环境变量
[root@tomcat01 ~]# source /etc/profile
[root@tomcat01 ~]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/   ##解压tomcat软件包
[root@tomcat01 ~]# cd /usr/local/
[root@tomcat01 local]# mv apache-tomcat-8.5.16/ tomcat    ##重命名Tomcat,方便管理
[root@tomcat01 local]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin   ##创建脚本连接方便管理
[root@tomcat01 local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin
[root@tomcat01 local]# startup.sh   ##开启服务

在这里插入图片描述
3.在nginx的虚拟机上配置动静分离,修改配置文件

[root@nginx init.d]# vim /usr/local/nginx/conf/nginx.conf    
#access_log  logs/host.access.log  main;
        location ~.*.jsp$ {
          proxy_pass http://192.168.148.136:8080;     ##动态页面交给tomcat处理
          proxy_set_header Host $host;
        }
[root@nginx init.d]# nginx -t     ##检查内容是否正确

4.在nginx中创建静态页面

[root@nginx html]# cd /usr/local/nginx/html/
[root@nginx html]# vim index.html    ##写入静态页面信息
<!DOCTYPE html>
<html>
<head>
<title>静态页面</title> 
<meta http-equiv="content-type" content="textml;charset=utf-8"> 
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>静态网站</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>这是一个静态网页.</em></p>
</body>
</html>

5.在tomcat中创建动态页面

[root@tomcat01 webapps]# cd /usr/local/tomcat/webapps
[root@tomcat01 webapps]# mkdir test    ##创建一个test站点
[root@tomcat01 webapps]# cd test/
[root@tomcat01 test]# vim index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
 <head>
    <title>JSP test1 page</title>
 </head>
 <body>
    <% out.println("Welcome dongtai Web");%>
 </body>
</html>
[root@tomcat01 test]# shutdown.sh 
[root@tomcat01 test]# startup.sh 

6.打开win10虚拟机做动静分离访问测试
在这里插入图片描述
在这里插入图片描述
Nginx处理静态图片,Tomcat处理动态页面

推荐步骤
1.在tomcat的虚拟机上配置

[root@tomcat01 test]# vim index.jsp
 <body>
    <% out.println("Welcome dongtai Web");%>
    <img src="test.jpg"/>     ##创建图片信息,这边用的是相对路径,所以图片要放在nginx的test文件夹中
 </body>

2.nginx虚拟机上配置,修改配置文件

[root@nginx conf]# vim /usr/local/nginx/conf/nginx.conf
#access_log  logs/host.access.log  main;
        location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {     ##支持的图片格式
          root html;
          expires 30d;            ##支持缓存30}

3.在nginx中也要创建一个test文件夹和tomcat的站点位置对应起来

[root@nginx conf]# cd ..
[root@nginx nginx]# cd html/      ##切换到nginx的站点
[root@nginx html]# mkdir test   ##创建和tomcat相同的文件夹test用于存放图片
[root@nginx test]# ls
12382c7b85ff754752d97dbdd2d23fe9.jpg
[root@nginx test]# mv 12382c7b85ff754752d97dbdd2d23fe9.jpg test.jpg    ##修改图片名称
[root@nginx test]# ls
test.jpg
[root@nginx test]# service nginx restart    ##重启服务

4.打开win10浏览器输入网址http://192.168.148.135/test/index.jsp测试

在这里插入图片描述
实验结果:发现nginx在处理静态图片,tomcat在处理动态页面

Nginx+tomcat负载均衡实验
推荐步骤:
1.先配置第三台虚拟机,相同的步骤安装tomcat,这边配置参考tomcat01
在这里插入图片描述

[root@tomcat02 local]# startup.sh        ##启动服务
[root@tomcat02 tomcat]# cd /usr/local/tomcat/
[root@tomcat02 tomcat]# mkdir -pv /web/webapp1    ##创建站点
mkdir: 已创建目录 "/web"
mkdir: 已创建目录 "/web/webapp1"
[root@tomcat02 tomcat]# vim /web/webapp1/index.jsp       ##写入页面信息
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html> 
 <head>
    <title>JSP test1 page</title>
 </head>
 <body>
    <% out.println("Welcome tomcat02 Web");%>     ##写入信息
 </body>
</html>
[root@tomcat02 tomcat]# vim /usr/local/tomcat/conf/server.xml    ##修改配置文件让其识别
......148行处修改
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
      <Context docBase="/web/webapp1" path="" reloadable="false">   ####添加此段站点信息
      </Context>
[root@tomcat02 tomcat]# shutdown.sh    ##关闭服务
[root@tomcat02 tomcat]# startup.sh     ##开启服务

2.配置tomcat01,其他的都一样,就是页面信息需要区分开来

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html> 
 <head>
    <title>JSP test1 page</title>
 </head>
 <body>
    <% out.println("Welcome tomcat01 Web");%>     ##页面信息区分开来  
 </body>
</html>
[root@tomcat01 tomcat]# shutdown.sh    ##关闭服务
[root@tomcat01 tomcat]# startup.sh     ##开启服务

3.配置nginx负载均衡

[root@nginx test]# vim /usr/local/nginx/conf/nginx.conf    ##修改配置文件
.......省略,添加
#keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    upstream tomcat_server {     ##添加节点服务器地址
        server 192.168.148.136:8080 weight=1;
        server 192.168.148.137:8080 weight=1;
        }
......省略
location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://tomcat_server;     
        }
[root@nginx test]# service nginx restart    ##修改完后,重启服务

4.打开win10做访问测试
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值