013.Nginx动静分离

一 动静分离概述

1.1 动静分离介绍

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

1.2 动静分离原理

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

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

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			#重载配置文件
 
手动访问后端静态站点及资源://staticrs.linuxds.com/及//staticrs.linuxds.com/nginx.jpg。
clipboard
clipboard

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
手动访问后端动态站点及资源://dynamic.linuxds.com:8080/javatest.jsp
clipboard

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 //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 //static_server;
 30         expires 5d;
 31     }
 32     location ~ .*\.jsp$ {
 33         proxy_pass //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 访问测试

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

三 动静分离实现–根据文件路径

3.1 环境准备

参考2.1.

3.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 /static {
  8         alias   /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			#重载配置文件
 
手动访问后端静态站点及资源://staticrs.linuxds.com/static/及//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>
 
  1 [root@nginx03 ~]# systemctl start tomcat.service	#启动tomcat
手动访问后端动态站点及资源://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 //static_server;
 24     }
 25     location ~  .*\.(css)$  {
 26         root   /usr/share/nginx/dss;
 27     }
 28     location /static/ {
 29         proxy_pass //static_server;
 30         expires 5d;
 31     }
 32     location /dynamic/ {
 33         proxy_pass //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			#重载配置文件
 

3.5 访问测试

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