Nginx+tomcat 动静分离实战

为什么要做动静分离?

1、nginx的处理静态资源的能力非常强。如果tomcat的请求量为1000次,那么nginx的请求量为6000次,tomcat每秒的吞吐量为0.6M,nginx的每秒吞吐量为3.6M,所以,nginx处理静态资源的能力远远超于tomcat6倍。

2、动态资源和静态资源分开后,使得服务器结构更加清晰


静态资源有:图片,js,css等文件,静态资源一般会存放在代理服务器中一段时间,这样提高网站的响应速度。

动态资源:jsp、servlet等这些需要进行动态请求处理的。

部署:

上传 解压:

nginx+tomcat动静分离03_静态资源nginx+tomcat动静分离03_html_02

Jdk加入全局变量:

nginx+tomcat动静分离03_html_03


nginx+tomcat动静分离03_动静分离_04

export JAVA_HOME=/usr/local/jdk1.8

export PATH=$PATH:$JAVA_HOME/bin

生效:

nginx+tomcat动静分离03_动静分离_05

Tomcta:

nginx+tomcat动静分离03_tomcat_06

Tomcat网站根目录:

nginx+tomcat动静分离03_nginx_07



启动:

调试模式


nginx+tomcat动静分离03_html_08



写首页(动态)

nginx+tomcat动静分离03_tomcat_09


nginx+tomcat动静分离03_动静分离_10



<%@ page language=”java” contentType=”test/html; charset=GB18030”

pageEncoding=”GB183%”>

< HTML>

<head>

<meta http-equiv="Content-Type" content=”text/html; charset=GB18030”>

<title>Nginx动静分离测试</title>

</head>

<body>

<h1>您正在访问∶192.168.31.68</h1>

<img src=”/drp/img/girl.jpg" alt="女孩" />

</body>

</html>


上传图片:

nginx+tomcat动静分离03_html_11



测试:

nginx+tomcat动静分离03_tomcat_12



开始动静分离:

nginx+tomcat动静分离03_动静分离_13


nginx+tomcat动静分离03_nginx_14




server {

listen 81;

server_name localhost;


Location / {

proxy_pass http://192.168.31.68:8080;

}

}

检查、重新加载:

nginx+tomcat动静分离03_tomcat_15

[root@www conf]# pkill nginx

[root@www conf]# /usr/local/nginx/sbin/nginx

测试(反向代理)

nginx+tomcat动静分离03_nginx_16



动静分离:

nginx+tomcat动静分离03_html_17



server {

listen 81;

server_name localhost;


location / {

proxy_pass http://192.168.31.68:8080;

}

location ~ .*\.(htm|html|jpg|jpeg|png|bmp)$

{

root /usr/local/nginx/webapps;

expires 30d;

}

}

创建

mkdir /usr/local/nginx/webapps -p

检查、重新加载:

nginx+tomcat动静分离03_动静分离_18

[root@www conf]# pkill nginx

[root@www conf]# /usr/local/nginx/sbin/nginx


测试:

访问网页正常是没有图片的

复制图片:

nginx+tomcat动静分离03_html_19

访问网页有图片了

nginx+tomcat动静分离03_html_20



实现的动静分离!!!!!!