[toc]
动静分离概述 什么是动静分离 前后端分离,将前端代码和后端代码区分开;
前端代码由前端的开发人员来写,后端代码由后端的开发人员去写;
前后端建立连接使用AJAX
什么是静态资源
类似.jpg .png .css .js…不需要访问 数据库的资源,属于静态资源 
 
 
什么是动态资源
 
什么是静态请求
 
什么是动态请求
用户发起的请求是访问后端资源,访问数据库(不是页面会动,就一定是动态请求) 
 
 
 
部署动静分离和代理整合资源 环境准备 
主机名 
WanIP 
LanIP 
角色 
应用 
 
 
web01 
10.0.0.7 
172.16.1.7 
代理 
nginx 
 
web02 
10.0.0.8 
172.16.1.8 
静态服务器 
nginx 
 
web03 
10.0.0.9 
172.16.1.9 
动态服务器 
tomcat 
 
部署前端静态页面(web02) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever /$basearch / gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true  yum install -y nginx vim /etc/nginx/conf.d/static.conf server{ 	listen 80; 	server_name pic.xxx.com; 	root /code; 	index index.html; 	charset utf-8; 	location ~* .*\.(jpg|png|gif)$ { 		root /code/images; 	} } nginx -t mkdir  -p /code/imagesecho  '这是静态资源页面'  > /code/index.htmlsystemctl restart nginx 10.0.0.8 pic.xxx.com pic.xxx.com 
1 2 3 4 5 6 7   0 ✓ 14:31:00 root@web02,172.16.1.8:/code/images  total 12 -rw-r--r-- 1 root root 11138 Jan  9 10:00 皮卡丘.jpg pic.xxx.com/皮卡丘.jpg 
部署后端动态资源(web03) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 yum install -y tomcat systemctl start tomcat netstat -lntup rpm -ql tomcat /usr/share/tomcat/webapps mkdir  /usr/share/tomcat/webapps/ROOTvim /usr/share/tomcat/webapps/ROOT/test.jsp <%@ page language="java"  import="java.util.*"  pageEncoding="utf-8" %> <HTML>     <HEAD>         <TITLE>HCLNB Page</TITLE>     </HEAD>     <BODY>         <%             Random rand = new Random();             out.println("<h1>随机数:<h1>" );             out.println(rand.nextInt(99)+100);         %>     </BODY> </HTML> 
整合前后端动静资源(web01) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 vim /etc/nginx/conf.d/proxy.conf upstream static{ 	server 172.16.1.8:80; } upstream java{ 	server 172.16.1.9:8080; } server { 	listen 80; 	server_name pic.xxx.com; 	location ~* \.(jpg|png|gif)$ { 		proxy_pass http://static; 		proxy_set_header HOST $http_host ; 	} 	location ~ \.jsp{ 		proxy_pass http://java; 		proxy_set_header HOST $http_host ; 	} } nginx -t systemctl start nginx 10.0.0.7 pic.xxx.com pic.xxx.com/test.jsp pic.xxx.com/皮卡丘.jpg 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 vim /etc/nginx/conf.d/proxy.conf upstream static{ 	server 172.16.1.8:80; } upstream java{ 	server 172.16.1.9:8080; } server { 	listen 80; 	server_name pic.xxx.com; 	location / { 		root /code/; 		index index.html; 	} 	 	location ~* \.(jpg|png|gif)$ { 		proxy_pass http://static; 		proxy_set_header HOST $http_host ; 	} 	 	location ~ \.jsp{ 		proxy_pass http://java; 		proxy_set_header HOST $http_host ; 	} } mkdir  /codevim /code/index.html <html lang="en" > <head >         <meta charset="UTF-8"  />         <title>测试ajax和跨域访问</title>         <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js" ></script> </head> <script type ="text/javascript" > $(document).ready(function          $.ajax({         type : "GET" ,         url: "http://pic.xxx.com/test.jsp" ,         success: function (data){                 $("#get_data" ).html(data)         },         error: function                  alert("失败了,回去检查服务" );         }         }); }); </script>         <body>                 <h1>测试动静分离</h1>                 <img src="http://pic.xxx.com/1.jpg" >                 <div id ="get_data" ></div>         </body> </html> systemctl restart nginx pic.xxx.com 
nginx实现资源分离 搭建环境 
主机名 
WanIP 
LanIP 
角色 
应用 
 
 
web01 
10.0.0.7 
172.16.1.7 
PC端 
nginx pc端代码 
 
web02 
10.0.0.8 
172.16.1.8 
安卓端 
nginx 安卓端代码 
 
web03 
10.0.0.9 
172.16.1.9 
IOS端 
tomcat ios端代码 
 
lb01 
10.0.0.5 
172.16.1.5 
代理 
nginx 
 
部署PC端(web01) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 vim /etc/nginx/conf.d/pc.conf server{ 	listen 9090; 	server_name pc.xxx.com; 	charset utf-8; 	location / { 		root /code/pc; 		index index.html; 	} } mkdir  /code/pcecho  '这里是PC端'  > /code/pc/index.htmlsystemctl restart nginx 10.0.0.7 pc.xxx.com 
部署安卓端(web02) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 vim /etc/nginx/conf.d/android.conf server{ 	listen 9091; 	server_name android.xxx.com; 	charset utf-8; 	location / { 		root /code/android; 		index index.html; 	} } mkdir  /code/androidecho  '这里是android'  > /code/android/index.htmlsystemctl restart nginx 10.0.0.8 android.xxx.com 10.0.0.8:9091 
部署ios端(web03) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 vim /etc/nginx/conf.d/ios.conf server{ 	listen 9092; 	server_name ios.xxx.com; 	charset utf-8; 	location / { 		root /code/ios; 		index index.html; 	} } mkdir  -p /code/iosecho  '这里是ios端'  > /code/ios/index.htmlsystemctl restart nginx 10.0.0.9 ios.xxx.com 10.0.0.9:9092 
资源分离配置(lb01) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 vim /etc/nginx/conf.d/xxx.conf upstream pc { 	server 172.16.1.7:9090; } upstream android { 	server 172.16.1.8:9091; } upstream ios { 	server 172.16.1.9:9092; } server { 	listen 80; 	server_name www.xxx.com; 	charset utf-8; 	location / { 		if  ($http_user_agent  ~* "Android" ) { 			proxy_pass http://android; 		} 		if  ($http_user_agent  ~* "Iphone" ){ 			proxy_pass http://ios; 		} 		proxy_pass http://pc; 	} } systemctl restart nginx 10.0.0.5 www.xxx.com www.xxx.com