一 动静分离概述

1.1 动静分离介绍

为了提高网站的响应速度,减轻程序服务器(Tomcat,Jboss等)的负载,对于静态资源,如图片、js、css等文件,可以在反向代理服务器中进行缓存,这样浏览器在请求一个静态资源时,代理服务器就可以直接处理,而不用将请求转发给后端服务器。对于用户请求的动态文件,如servlet、jsp,则转发给Tomcat,Jboss服务器处理,这就是动静分离。即动态文件与静态文件的分离。

1.2 动静分离原理

动静分离可通过location对请求url进行匹配,将网站静态资源(HTML,JavaScript,CSS,img等文件)与后台应用分开部署,提高用户访问静态代码的速度,降低对后台应用访问。通常将静态资源放到nginx中,动态资源转发到tomcat服务器中。

二 动静分离实现--根据文件后缀

2.1 环境准备



主机

IP

角色

备注

nginx01

172.24.10.21

Nginx Proxy主机

接受请求,并代理至后端css存储点

nginx02

172.24.10.22

Nginx 静态服务器

处理静态请求

nginx03

172.24.10.23

Nginx 动态服务器

处理动态请求

本实验动静分离主要是通过nginx+tomcat来实现,其中nginx01进行前端代理,同时本地处理css静态文件,nginx02处理图片、html、JS等静态文件,tomcat处理jsp、servlet等动态请求。

2.2 创建静态站点

  1 [root@nginx02 ~]# mkdir /usr/share/nginx/staticrs/
2 [root@nginx02 ~]# echo '<h1>Static_Web</h1>' > /usr/share/nginx/staticrs/index.html
3 [root@nginx02 ~]# ll /usr/share/nginx/staticrs/ #上传示例图片静态资源
4 total 16K
5 -rw-r--r-- 1 root root 20 Jun 20 14:32 index.html
6 -rw-r--r-- 1 root root 11K Jun 20 14:35 nginx.jpg
7 [root@nginx02 ~]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
  1 [root@nginx02 ~]# vi /etc/nginx/conf.d/staticrs.conf
2 server {
3 listen 80;
4 server_name staticrs.linuxds.com;
5 access_log /var/log/nginx/staticrs.access.log main;
6 error_log /var/log/nginx/staticrs.error.log warn;
7 location / {
8 root /usr/share/nginx/staticrs;
9 index index.html;
10 }
11 }
  1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件
2 [root@nginx02 ~]# nginx -s reload #重载配置文件
手动访问后端静态站点及资源:http://staticrs.linuxds.com/及http://staticrs.linuxds.com/nginx.jpg。

2.3 创建动态站点

  1 [root@nginx03 ~]# yum install -y tomcat
2 [root@nginx03 ~]# mkdir -p /usr/share/tomcat/webapps/ROOT
  1 [root@nginx03 ~]# vi /usr/share/tomcat/webapps/ROOT/javatest.jsp	#构建动态测试页面
2 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
3 <HTML>
4 <HEAD>
5 <TITLE>JSP Test Page</TITLE>
6 </HEAD>
7
8 <BODY>
9 <%
10 Random rand = new Random();
11 out.println("<h1>随机数:<h1>");
12 out.println(rand.nextInt(99)+100);
13 %>
14 </BODY>
15 </HTML>
  1 [root@nginx03 ~]# systemctl start tomcat.service	#启动tomcat
手动访问后端动态站点及资源:http://dynamic.linuxds.com:8080/javatest.jsp

2.4 配置前端动静分离

  1 [root@nginx01 ~]# mkdir -p /usr/share/nginx/dss
2 [root@nginx01 ~]# ll /usr/share/nginx/dss/
3 total 4.0K
4 -rw-r--r-- 1 root root 1.9K Jun 20 18:10 test.css #模拟css
  1 [root@nginx01 ~]# vi /etc/nginx/conf.d/dss.conf		#配置Dynamic-Static Separation
2 upstream static_server {
3 server 172.24.10.22;
4 }
5 upstream tomcat_server {
6 server 172.24.10.23:8080;
7 }
8
9 server {
10 listen 80;
11 server_name dss.linuxds.com;
12 access_log /var/log/nginx/dss.access.log main;
13 error_log /var/log/nginx/dss.error.log warn;
14 proxy_set_header X-Real-IP $remote_addr;
15 proxy_set_header Host $host;
16 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
17 proxy_set_header X-Forwarded-Proto $scheme;
18 # location / {
19 # root html;
20 # index index.html;
21 # }
22 location / {
23 proxy_pass http://static_server;
24 }
25 location ~ .*\.(css)$ {
26 root /usr/share/nginx/dss;
27 }
28 location ~ .*\.(htm|html|gif|jpg|jpeg|png|gif|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma) {
29 proxy_pass http://static_server;
30 expires 5d;
31 }
32 location ~ .*\.jsp$ {
33 proxy_pass http://tomcat_server;
34 expires 1h;
35 }
36 error_page 500 502 503 504 /50x.html;
37 location = /50x.html {
38 root html;
39 }
40 }
  1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件
2 [root@nginx01 ~]# nginx -s reload #重载配置文件

2.5 访问测试

浏览器分别访问:http://dss.linuxds.com/,http://dss.linuxds.com/javatest.jsp,http://staticrs.linuxds.com/nginx.jpg,http://dss.linuxds.com/test.css。

三 动静分离实现--根据文件后缀

3.1 环境准备

参考2.1.

3.2 创建静态站点

  1 [root@nginx02 ~]# vi /etc/nginx/conf.d/staticrs.conf
2 server {
3 listen 80;
4 server_name staticrs.linuxds.com;
5 access_log /var/log/nginx/staticrs.access.log main;
6 error_log /var/log/nginx/staticrs.error.log warn;
7 location /static {
8 alias /usr/share/nginx/staticrs;
9 index index.html;
10 }
11 }
手动访问后端静态站点及资源:http://staticrs.linuxds.com/static/及http://staticrs.linuxds.com/static/nginx.jpg。

3.3 创建动态站点

  1 [root@nginx03 ~]# yum install -y tomcat
2 [root@nginx03 ~]# mkdir -p /usr/share/tomcat/webapps/ROOT/dynamic
  1 [root@nginx03 ~]# vi /usr/share/tomcat/webapps/ROOT/dynamic/javatest.jsp
2 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
3 <HTML>
4 <HEAD>
5 <TITLE>JSP Test Page</TITLE>
6 </HEAD>
7
8 <BODY>
9 <%
10 Random rand = new Random();
11 out.println("<h1>随机数:<h1>");
12 out.println(rand.nextInt(99)+100);
13 %>
14 </BODY>
15 </HTML>
手动访问后端动态站点及资源:http://dynamic.linuxds.com:8080/dynamic/javatest.jsp

3.4 配置前端动静分离

  1 [root@nginx01 ~]# mkdir -p /usr/share/nginx/dss
2 [root@nginx01 ~]# ll /usr/share/nginx/dss/
3 total 4.0K
  1 [root@nginx01 ~]# vi /etc/nginx/conf.d/dss.conf		#配置Dynamic-Static Separation
2 upstream static_server {
3 server 172.24.10.22;
4 }
5 upstream tomcat_server {
6 server 172.24.10.23:8080;
7 }
8
9 server {
10 listen 80;
11 server_name dss.linuxds.com;
12 access_log /var/log/nginx/dss.access.log main;
13 error_log /var/log/nginx/dss.error.log warn;
14 proxy_set_header X-Real-IP $remote_addr;
15 proxy_set_header Host $host;
16 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
17 proxy_set_header X-Forwarded-Proto $scheme;
18 # location / {
19 # root html;
20 # index index.html;
21 # }
22 location / {
23 proxy_pass http://static_server;
24 }
25 location ~ .*\.(css)$ {
26 root /usr/share/nginx/dss;
27 }
28 location /static/ {
29 proxy_pass http://static_server;
30 expires 5d;
31 }
32 location /dynamic/ {
33 proxy_pass http://tomcat_server;
34 expires 1h;
35 }
36 error_page 500 502 503 504 /50x.html;
37 location = /50x.html {
38 root html;
39 }
40 }

3.5 访问测试

浏览器分别访问:http://dss.linuxds.com/,http://dss.linuxds.com/dynamic/javatest.jsp,http://staticrs.linuxds.com/static/nginx.jpg,http://dss.linuxds.com/test.css。
参考:https://klionsec.github.io/2017/12/21/nginx-static-dynamic/。

最新文章

  1. IIS 发布添加网站错误:HTTP 错误 500.21 - Internal Server Error 解决方案
  2. Node debug
  3. C语言深度剖析学习错误点记录
  4. Linux内核源码分析方法
  5. [cc150] check palindrome of a singly linked list
  6. poj1151Atlantis(离散化+扫描线)
  7. mysql的账户失效,之前的密码无法登录
  8. js中调用mangeto的js翻译
  9. xzzx
  10. c++内存优化:二级间接索引模式内存池
  11. [刷题]算法竞赛入门经典 3-10/UVa1587 3-11/UVa1588
  12. C# 使用NPOI 导出Excel
  13. vuejs小白入门
  14. canal demo搭建全记录
  15. docker hub切换国内镜像
  16. RuntimeError: Working outside of application context.
  17. MDK5使用技巧
  18. except but
  19. 探索式软件测试—Exploratory Software Testing
  20. python 面向对象 【进阶】

热门文章

  1. Linux 开放指定端口号
  2. 微信小程序scroll-view
  3. LeetCode 80,不使用外部空间的情况下对有序数组去重
  4. Scala创建SparkStreaming获取Kafka数据代码过程
  5. 4. union-find算法
  6. openstack迁移计算节点所有云主机
  7. DOM-BOM-EVENT(1)
  8. Apache POI 操作Excel(2)-- POI包引入项目
  9. 比Minikube更快,使用Kind快速创建K8S学习环境
  10. 逻辑式编程语言极简实现(使用C#) - 3. 运行原理