社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  NGINX

Nginx+Tomcat动静分离&&负载均衡

weixin_42099301 • 4 年前 • 369 次点击  

Nginx+Tomcat动静分离&&负载均衡

1. Tomcat简介

  • 最初是由Sun的软件构架师詹姆斯·邓肯·戴维森开发
  • 安装Tomcat后,安装路径下面的目录和文件,是使用或者配置Tomcat的重要文件

2. Tomcat重要目录

  • bin :存放启动和关闭Tomcat脚本
  • conf :存放Tomcat不同的配置文件
  • doc:存放Tomcat文档
  • lib:存放Tomcat运行需要的库文件
  • logs:存放Tomcat执行时的LOG文件
  • src:存放Tomcat的源代码
  • webapps:Tomcat的主要Web发布目录
  • work:存放jsp编译后产生的class文件

3. Nginx应用

  • Nginx是一款非常优秀的HTTP服务器软件
  • 支持高达50 000个并发连接数的响应
  • 拥有强大的静态资源处理能力
  • 运行稳定
  • 内存、CPU等系统资源消耗非常低
  • 目前很多大型网站都应用Nginx服务器作为后端网站程序的反向代理及负载均衡器,提升整个站点的负载并发能力
  • 只能静态页面处理。
  • Apache可以处理动态和静态页面的处理。
  • Tomcat 支持动态页面处理。
  • Nginx负载均衡实现原理
  • Nginx实现负载均衡是通过反向代理实现

4. 反向代理原理

  • Nginx负载均衡实现原理
  • Nginx配置反向代理的主要参数
  • upstream 服务池名 { }
  • 配置后端服务器池,以提供响应数据
  • proxy_pass http://服务池名
  • 配置将访问请求转发给后端服务器池的服务器处理

5. 动静分离原理

  • 服务端接收来自客户端的请求中,既有静态资源也有动态资源,静态资源由Nginx提供服务,动态资源Nginx转发至后端

6. Nginx静态处理优势

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

7. 实验要求

  • 要求部署两台后端Tomcat服务器
  • 为了进行测试,搭建两个内容不同的网站
  • Tomcat部署与网站搭建步骤
  • 关闭firewall防火墙
  • 安装JDK,配置JAVA环境
  • 安装配置Tomcat
  • 创建/web/webapp1目录,修改server.xml,将网站文件目录更改到/web/webapp1/路径下
    /web/webapp1/下建立测试页面index.jsp,并进行测试

8. 实验环境

  • 一台:Nginx 作为反向代理动静分离的静态页面 192.168.75.166
  • 一台:tomcat 作为动态页面处理的 192.168.75.134
  • 一台:tomcat 作为动态页面处理的 192.168.75.144

在这里插入图片描述

9. 实验步骤

9.1 Nginx反向代理的配置 192.168.75.166

  • Nginx服务基础
  • Nginx:稳定性高
  • 系统资源消耗低
  • 对HTTP并发连接的处理能力高(能处理高并发 活到现在的核心)
  • 单台物理服务器可支持30000~50000个并发请求
yum -y install pcre-devel zlib-devel gcc gcc-c++  pcre  make

pcre-devel : 支持正则表达式
pcre   正则表达式

zlib-devel : 压缩功能

tar zxvf nginx-1.12.2.tar.gz 
cd  nginx-1.12.2
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx

./configure  --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/


    
sbin/


设置Nginx系统引导启动   systemctl

在/etc/init.d 目录下新建Nginx文件为启动脚本

[root@localhost 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@localhost init.d]# chmod +x nginx   //添加权限

[root@localhost init.d]# chkconfig --add nginx   //添加到systemctl启动文件设置中

[root@localhost init.d]# service nginx stop   


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

9.2 Tomcat 1配置192.168.75.134


iptbales -F    
setenforce 0
准备JDK安装包

tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local

 vim /etc/profile

export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH


source /etc/profile

 

java -version   //查看版本号


部署Tomcat


[root@promote opt]# tar zxvf apache-tomcat-9.0.16.tar.gz  -C /usr/local/
[root@promote opt]# cd /usr/local/
[root@promote local]# ls
apache-tomcat-9.0.16  etc    include  lib64    sbin   src
bin                   games  lib      libexec  share
[root@promote local]# mv apache-tomcat-9.0.16/ tomcat
[root@promote local]# cd tomcat/
[root@promote tomcat]# ll
总用量 124
drwxr-x--- 2 root root  4096 813 09:53 bin
-rw-r----- 1 root root 19203


    
 25 2019 BUILDING.txt
drwx------ 2 root root   238 25 2019 conf
-rw-r----- 1 root root  6095 25 2019 CONTRIBUTING.md
drwxr-x--- 2 root root  4096 813 09:53 lib
-rw-r----- 1 root root 57092 25 2019 LICENSE
drwxr-x--- 2 root root     6 25 2019 logs
-rw-r----- 1 root root  2333 25 2019 NOTICE
-rw-r----- 1 root root  3255 25 2019 README.md
-rw-r----- 1 root root  6854 25 2019 RELEASE-NOTES
-rw-r----- 1 root root 16262 25 2019 RUNNING.txt
drwxr-x--- 2 root root    30 813 09:53 temp
drwxr-x--- 7 root root    81 25 2019 webapps
drwxr-x--- 2 root root     6 25 2019 work
[root@promote tomcat]# cd bin/
[root@promote bin]# ln -s /usr/local/tomcat/bin/startup.sh /usr/bin/
[root@promote bin]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/bin/

[root@promote bin]# startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/jdk1.8.0_201-amd64
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@promote bin]# netstat -natp | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      13752/java          
[root@promote bin]#     

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

9.3 Nginx配置

  • Nginx 处理静态图片,Tomcat处理动态图片 nginx 配置
  • 代码是动态的
  • 图片是静态的
  • 动态分离配置
  • nginx 服务器的配置 192.168.75.166
  • proxy_pass 模块功能跳转到另一个服务器

vim /usr/local/nginx/conf/nginx.conf


         location ~.*.jsp$ {
         proxy_pass http://192.168.75.134:8080;
        proxy_set_header Host $host;
}



 vim /usr/local/nginx/html/index.html 

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>静态页面</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family:Tahoma,Verdana,Arial,sans-serif;
}
</style>
</head>
<body>
<h1>静态页面</h1>
<p>这是个静态页面</p >
</body>
</html>


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

9.4 Tomcat 1 192.168.75.134 配置

  • Tomcat的test /indexJSP 和Nginx存放的图片路径要相同不然无法显示图片

[root@localhost bin]# mkdir /usr/local/tomcat/webapps/test

[root@localhost bin]# vim /usr/local/tomcat/webapps/test/index.jsp

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/ html14/loose.dtd"


    
>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>动态页面</title>
</head>
<body>
<div>动态页面</div>
</body>
</html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

9.5 Nginx配置 192.168.75.166

  • 图片属于静态资源,因为Nginx处理的是静态页面。所以图片应该放在Nginx目录中,但是要与Tomcat的JSP文件的路径相对应
vim /usr/local/nginx/conf/nginx.conf

location ~.*\.(gif|jpg|jpeg|png|bmg|swf|css)$ {
        root html;
        expires 30d;
        }

cd /usr/local/nginx/html

mkdir test
将图片放入 /usr/local/nginx/html/test 目录下因为需要和tomcat 1 相对应
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • Tomcat 1 192.168.75.134 配置
vim /usr/local/tomcat/webapps/test/index.jsp


<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/ html14/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>动态页面</title>
</head>
<body>
<div>动态页面</div>
<img src="22.jpg"width=250>   //添加
</body>
</html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

9.6 验证 图片验证动静分离

  • 访问静态页面
  • http://192.168.75.166
  • 访问动态页面
  • http://192.168.75.166/test/index.jsp

在这里插入图片描述

在这里插入图片描述

  • 图片验证动静分离

在这里插入图片描述

10 . 实验二:负载均衡

10.1 实验环境

  • Nginx 192.168.75.166 作为负载均衡服务器
  • Tomcat1 192.168.75.134
  • Tomcat 2 192.168.75.144 作为后端web服务器
  • 以Nginx作为负载均衡器,Tomcat作为应用服务器

10.2 Tomcat 2 192.168.75.144 配置

  • 安装Tomcat 服务
  • 像之前Tomcat1 一样的步骤,详情看Tomcat1
  • 因为之前Tomcat1已经部署Tomcat,所以只需要部署Tomcat2 服务器内容详情如下

mkdir -pv /web/webapp1
cd /web/webapp1
vim index.jsp

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/ html14/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>web   web   web   web   web  webb   !!!!</title>
</head>
<body>
<div>web   web   web   web   web  webb   !!!!</div>
</body>
</html>

************修改配置文件***************

 vim /usr/local/tomcat/conf/server.xml 


<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Context docBase="/web/webapp1" path="" reloadable="false">  //添加
        </Context>  //添加
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

10.3 Nginx服务器配置192.168.75.166

  • 部署Nginx 详情看目录9.1

vim /usr/local/nginx/conf/nginx.conf

******在http的全局添加*******

 upstream tomcat_server {
    server 192.168.75.134:8080 weight=1;
    server 192.168.75.144:8080 weight=5;  //权重越高越优先
   }

*****在server中添加*****
 location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://tomcat_server;  //添加
        }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

10.4 验证

  • 192.168.75.166

在这里插入图片描述
在这里插入图片描述

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/73164
 
369 次点击