Nginx+Tomcat动静分离&&负载均衡
文章目录
Nginx+Tomcat动静分离&&负载均衡
1. Tomcat简介2. Tomcat重要目录3. Nginx应用4. 反向代理原理5. 动静分离原理6. Nginx静态处理优势7. 实验要求8. 实验环境9. 实验步骤9.1 Nginx反向代理的配置 192.168.75.1669.2 Tomcat 1配置192.168.75.1349.3 Nginx配置9.4 Tomcat 1 192.168.75.134 配置9.5 Nginx配置 192.168.75.1669.6 验证 图片验证动静分离
10 . 实验二:负载均衡10.1 实验环境10.2 Tomcat 2 192.168.75.144 配置10.3 Nginx服务器配置192.168.75.16610.4 验证
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.6MNginx处理静态资源的能力是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
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 8月
13 09:53 bin
-rw
-r
----- 1 root root
19203 2月
5 2019 BUILDING
.txt
drwx
------ 2 root root
238 2月
5 2019 conf
-rw
-r
----- 1 root root
6095 2月
5 2019 CONTRIBUTING
.md
drwxr
-x
--- 2 root root
4096 8月
13 09:53 lib
-rw
-r
----- 1 root root
57092 2月
5 2019 LICENSE
drwxr
-x
--- 2 root root
6 2月
5 2019 logs
-rw
-r
----- 1 root root
2333 2月
5 2019 NOTICE
-rw
-r
----- 1 root root
3255 2月
5 2019 README
.md
-rw
-r
----- 1 root root
6854 2月
5 2019 RELEASE
-NOTES
-rw
-r
----- 1 root root
16262 2月
5 2019 RUNNING
.txt
drwxr
-x
--- 2 root root
30 8月
13 09:53 temp
drwxr
-x
--- 7 root root
81 2月
5 2019 webapps
drwxr
-x
--- 2 root root
6 2月
5 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
]#
9.3 Nginx配置
Nginx 处理静态图片,Tomcat处理动态图片 nginx 配置代码是动态的图片是静态的动态分离配置nginx 服务器的配置 192.168.75.166proxy_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
>
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
>
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 相对应
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
>
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.134Tomcat 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" />
-->
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
;
}
10.4 验证
192.168.75.166