文章目录
一、Nginx动静分离实现原理1.1 动静分离实现原理1.2 Nginx静态处理优势
二、Nginx负载均衡实现原理2.1 Nginx实现负载均衡是通过反向代理实现2.2 Nginx配置反向代理的主要参数
三、Nginx+Tomcat群集搭建3.1 动静分离搭建3.1.1 搭建Nginx服务3.1.2 Tomcat服务搭建3.1.3 动静分离配置3.1.4 nginx处理静态图片,Tomcat处理动态页面配置
3.2 负载均衡配置3.2.1 配置Tomcat02,与01相同3.2.2 Nginx服务器配置3.2.3测试
一、Nginx动静分离实现原理
1.1 动静分离实现原理
服务端接收来自客户端的请求中,既有静态资源也有动态资源,静态资源由Nginx提供服务,动态资源Nginx转发至后端
1.2 Nginx静态处理优势
Nginx处理静态页面的效率远高于Tomcat的处理能力若Tomcat的请求量为1000次,则Nginx的请求量为6000次Tomcat每秒的吞吐量为0.6M,Nginx的每秒吞吐量为3.6MNginx处理静态资源的能力是Tomcat处理的6倍
二、Nginx负载均衡实现原理
2.1 Nginx实现负载均衡是通过反向代理实现
反向代理原理
2.2 Nginx配置反向代理的主要参数
upstream 服务池名 { }
配置后端服务器池,以提供响应数据 Proxy_pass http://服务池名
配置将访问请求转发给后端服务器池的服务器处理
三、Nginx+Tomcat群集搭建
3.1 动静分离搭建
3.1.1 搭建Nginx服务
[root
@nginx opt
]# yum
-y install gcc gcc
-c
++ make pcre
-devel zlib
-devel
[root
@nginx opt
]# useradd
-M
-s
/sbin
/nologin nginx
[root
@nginx opt
]# tar zxvf nginx
-1.12.2.tar
.gz
-C
/usr
/local
/
[root
@nginx opt
]# cd
/usr
/local
/nginx
-1.12.2/
[root
@nginx nginx
-1.12.2]#
./configure \
--prefix
=/usr
/local
/nginx \
--user
=nginx \
--group
=nginx \
--with
-http_stub_status_module
[root
@nginx nginx
-1.12.2]# make
&& make install
[root
@nginx nginx
-1.12.0]# ln
-s
/usr
/local
/nginx
/sbin
/nginx
/usr
/local
/sbin
[root
@nginx nginx
-1.12.2]# nginx
-t
nginx
: the configuration file
/usr
/local
/nginx
/conf
/nginx
.conf syntax is ok
nginx
: configuration file
/usr
/local
/nginx
/conf
/nginx
.conf test is successful
[root
@nginx nginx
-1.12.2]# vim
/etc
/init
.d
/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 nginx
-1.12.2]# chmod
+x
/etc
/init
.d
/nginx
[root
@nginx nginx
-1.12.2]# service nginx start
[root
@nginx nginx
-1.12.2]# netstat
-natp
| grep
80
tcp
0 0 0.0.0.0:80 0.0.0.0:* LISTEN
22832/nginx
: master
3.1.2 Tomcat服务搭建
[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
export CLASSPATH
=.:$
{JAVA_HOME
}/lib
:$
{JRE_HOME
}/lib
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
/
[root
@tomcat01 ~]# cd
/usr
/local
/
[root
@tomcat01 local
]# mv apache
-tomcat
-8.5.16/ tomcat
[root
@tomcat01 local
]# ln
-s
/usr
/local
/tomcat
/bin
/startup
.sh
/usr
/local
/bin
/
[root
@tomcat01 local
]# ln
-s
/usr
/local
/tomcat
/bin
/shutdown
.sh
/usr
/local
/bin
/
[root
@tomcat01 local
]# 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
/local
/jdk1
.8.0_91
/jre
Using CLASSPATH
: /usr
/local
/tomcat
/bin
/bootstrap
.jar
:/usr
/local
/tomcat
/bin
/tomcat
-juli
.jar
Tomcat started
.
3.1.3 动静分离配置
将动态处理请求转发到Tomcat处理
[root
@nginx nginx
-1.12.2]# vim
/usr
/local
/nginx
/conf
/nginx
.conf
server
{
listen
80;
server_name localhost
;
location
~.*
.jsp$
{
proxy_pass http
://192.168.179.124:8080; '指向Tomcat服务器地址'
proxy_set_header Host $host
;
}
[root
@nginx nginx
-1.12.2]# cd
/usr
/local
/nginx
/html
/
[root
@nginx html
]# vim index
.html
<!DOCTYPE html
>
<html>
<head>
<title>静态页面
</title
> '修改标题'
<meta http
-equiv
="content-type" content
="text/html;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
>
[root
@nginx html
]# service nginx stop
[root
@nginx html
]# service nginx start
Tomcat服务器添加动态网页
[root
@tomcat01 local
]# cd
/usr
/local
/tomcat
/webapps
/
[root
@tomcat01 webapps
]# mkdir test
[root
@tomcat01 webapps
]# vim 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 html4/loose.dtd">
<html>
<head>
<meta http
-equiv
="Content-Type" content
="text/html; charset=UTF-8">
<title>动态页面
</title
>
</head
>
<body>
<div>这是一个动态页面
</div
>
</body
>
</html
>
3.1.4 nginx处理静态图片,Tomcat处理动态页面配置
Tomcat配置
[root
@tomcat01 webapps
]# vim test
/index
.jsp
</head
>
<body>
<div>tomcat01动态页面
</div
>
<img src
='flower.jpg'> '添加图片路径'
</body
>
</html
>
nginx配置
[root
@nginx html
]# vim
/usr
/local
/nginx
/conf
/nginx
.conf
location
~.*\
.(gif
|jpg
|jpeg
|png
|bmp
|swf
|css
)$
{ '添加此段内容,匹配这些类型的文件'
root html
; 'html站点目录'
expires
30d; '缓存时间30天'
}
[root
@nginx html
]# mv flower
.jpg test
3.2 负载均衡配置
3.2.1 配置Tomcat02,与01相同
3.2.2 Nginx服务器配置
[root
@nginx ~]# vim
/usr
/local
/nginx
/conf
/nginx
.conf
...
#gzip on
;
upstream tomcatserver
{ '添加轮询方式'
server
192.168.179.124:8080 weight
=1;
server
192.168.179.125:8080 weight
=1;
}
...
location
~.*
.jsp$
{
proxy_pass http
://tomcatserver
; '将匹配到的动态处理交由tomcatserver内的服务器处理'
proxy_set_header Host $host
;
}
...
[root
@nginx ~]# service nginx stop
[root
@nginx ~]# service nginx start
3.2.3测试
再次访问此时两台Tomcat服务器轮询处理动态请求