Nginx动静分离和资源分离
动静分离
什么是静态资源
类似于 .jpg、.png、.css、.js....不需要访问数据库的资源,属于静态资源
什么是动态资源
需要访问数据库的代码文件,.php、.jsp、.py....
什么是静态请求
用户发起的请求只访问前端资源,不访问数据库
什么是动态请求
用户发起的请求访问后端资源,访问数据库
注意:不是页面会动,就一定是动态请求
什么是动静分离
又叫做前后端分离,将前端代码和后端代码区分开,前端代码,前端开发人员来写,后端代码,后端的开发人员来写前端和后端建立连接使用AJAX
实践动静分离
环境准备
主机名 | 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 |
部署前端(静态页面)
# 1.安装nginx服务
[root@web01 ~]# 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
[root@web01 ~]# yum install -y nginx
[root@web02 ~]# vim /etc/yum.repos.d/ngx.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
[root@web02 ~]# yum install -y nginx
# 2.配置nginx静态资源配置文件
[root@web02 ~]# vim /etc/nginx/conf.d/static.conf
server {
listen 80;
server_name www.hg.com;
root /code;
index index.html;
charset utf-8;
location ~* .*\.(jpg|png|gif)$ {
root /code/images;
}
}
# 3.检查语法,启动nginx
[root@web02 ~]# nginx -t
[root@web02 ~]# systemctl restart nginx
# 4.创建站点目录
[root@web02 ~]# mkdir -p /code/images
# 5.部署前端代码
[root@web02 ~]# echo '这是静态资源页面' > /code/index.html
# 6.域名解析
10.0.0.8 www.hg.com
# 7.浏览器访问页面
# 1.安装nginx服务
[root@web01 ~]# 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
[root@web01 ~]# yum install -y nginx
[root@web02 ~]# vim /etc/yum.repos.d/ngx.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
[root@web02 ~]# yum install -y nginx
# 2.配置nginx静态资源配置文件
[root@web02 ~]# vim /etc/nginx/conf.d/static.conf
server {
listen 80;
server_name www.hg.com;
root /code;
index index.html;
charset utf-8;
location ~* .*\.(jpg|png|gif)$ {
root /code/images;
}
}
# 3.检查语法,启动nginx
[root@web02 ~]# nginx -t
[root@web02 ~]# systemctl restart nginx
# 4.创建站点目录
[root@web02 ~]# mkdir -p /code/images
# 5.部署前端代码
[root@web02 ~]# echo '这是静态资源页面' > /code/index.html
# 6.域名解析
10.0.0.8 www.hg.com
# 7.浏览器访问页面
# 8.上传图片到/code/images
[root@web02 images]# ll
total 6292
-rw-rw-rw- 1 root root 6441553 Jul 7 11:05 wallhaven-2y322g.jpg
# 9.浏览器加上图片名访问
部署后端(动态页面)
# 1.安装JAVA环境
[root@web03 ~]# yum install -y tomcat
# 2.启动tomcat
[root@web03 ~]# systemctl start tomcat
# 3.查看端口
[root@web03 ~]# netstat -lntup|grep java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 1871/java
tcp6 0 0 :::8009 :::* LISTEN 1871/java
tcp6 0 0 :::8080 :::* LISTEN 1871/java
# 4.tomcat站点目录
[root@web03 ~]# rpm -ql tomcat
/usr/share/tomcat/webapps
# 5.部署后端代码,需要在站点目录下创建一个ROOT目录,将代码部署在ROOT目录下
[root@web03 webapps]# mkdir /usr/share/tomcat/webapps/ROOT
## 这是一个随机数代码 假装是动态页面
[root@web03 webapps]# vim /usr/share/tomcat/webapps/ROOT/test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
<HEAD>
<TITLE>测试</TITLE>
</HEAD>
<BODY>
<%
Random rand = new Random();
out.println("<h1>随机数:<h1>");
out.println(rand.nextInt(99)+100);
%>
</BODY>
</HTML>
# 6.打开浏览器访问:http://10.0.0.9:8080/test.jsp
# 1.安装JAVA环境
[root@web03 ~]# yum install -y tomcat
# 2.启动tomcat
[root@web03 ~]# systemctl start tomcat
# 3.查看端口
[root@web03 ~]# netstat -lntup|grep java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 1871/java
tcp6 0 0 :::8009 :::* LISTEN 1871/java
tcp6 0 0 :::8080 :::* LISTEN 1871/java
# 4.tomcat站点目录
[root@web03 ~]# rpm -ql tomcat
/usr/share/tomcat/webapps
# 5.部署后端代码,需要在站点目录下创建一个ROOT目录,将代码部署在ROOT目录下
[root@web03 webapps]# mkdir /usr/share/tomcat/webapps/ROOT
## 这是一个随机数代码 假装是动态页面
[root@web03 webapps]# vim /usr/share/tomcat/webapps/ROOT/test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
<HEAD>
<TITLE>测试</TITLE>
</HEAD>
<BODY>
<%
Random rand = new Random();
out.println("<h1>随机数:<h1>");
out.println(rand.nextInt(99)+100);
%>
</BODY>
</HTML>
# 6.打开浏览器访问:http://10.0.0.9:8080/test.jsp
在代理上整合资源
# 1.修改nginx代理配置文件 加 location /
[root@web01 ~]# 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 www.gouwo.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;
}
}
# 2.创建站点目录
[root@web01 ~]# mkdir /code
# 3.编写资源整合代码
[root@web01 ~]# vim /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://www.gouwo.com/test.jsp",
success: function(data){
$("#get_data").html(data)
},
error: function() {
alert("啧啧啧,失败了,回去检查你服务去~");
}
});
});
</script>
<body>
<h1>测试动静分离</h1>
<img src="http://www.hg.com/wallhaven-2y322g.jpg">
<div id="get_data"></div>
</body>
</html>
# 4.重启nginx
[root@web01 ~]# nginx -t
[root@web01 ~]# systemctl restart nginx
# 5.域名解析10.0.0.7 www.gouwo.com 然后打开浏览器访问:http://www.gouwo.com
nginx实现资源分离
环境准备
主机名 | WANIP | LANIP | 角色 | 应用 |
---|---|---|---|---|
lb01 | 10.0.0.5 | 172.16.1.5 | 代理 | nginx |
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端页面 | nginx、IOS端的代码 |
部署PC端
# 1.编写PC端nginx配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/pc.conf
server {
listen 9090;
server_name pc.hg.com;
charset utf-8;
location / {
root /code/pc;
index index.html;
}
}
# 2.创建站点目录
[root@web01 ~]# mkdir /code/pc
# 3.部署pc端代码
[root@web01 ~]# echo '这里是PC端页面' > /code/pc/index.html
# 4.重新启动nginx
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl restart nginx
# 5.域名解析
10.0.0.7 pc.hg.com
# 6.浏览器访问:http://pc.hg.com:9090/
# 1.编写PC端nginx配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/pc.conf
server {
listen 9090;
server_name pc.hg.com;
charset utf-8;
location / {
root /code/pc;
index index.html;
}
}
# 2.创建站点目录
[root@web01 ~]# mkdir /code/pc
# 3.部署pc端代码
[root@web01 ~]# echo '这里是PC端页面' > /code/pc/index.html
# 4.重新启动nginx
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl restart nginx
# 5.域名解析
10.0.0.7 pc.hg.com
# 6.浏览器访问:http://pc.hg.com:9090/
部署安卓端代码
# 1.编写安卓端nginx配置文件
[root@web02 ~]# vim /etc/nginx/conf.d/android.conf
server {
listen 9091;
server_name android.hg.com;
charset utf-8;
location / {
root /code/android;
index index.html;
}
}
# 2.创建站点目录
[root@web02 ~]# mkdir /code/android
# 3.部署安卓端代码
[root@web02 ~]# echo '这里是andorid页面' > /code/android/index.html
# 4.重启nginx
[root@web02 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 ~]# systemctl restart nginx
# 5.域名解析
10.0.0.8 andorid.hg.com
# 6.浏览器访问:http://andorid.hg.com:9091/
# 1.编写安卓端nginx配置文件
[root@web02 ~]# vim /etc/nginx/conf.d/android.conf
server {
listen 9091;
server_name android.hg.com;
charset utf-8;
location / {
root /code/android;
index index.html;
}
}
# 2.创建站点目录
[root@web02 ~]# mkdir /code/android
# 3.部署安卓端代码
[root@web02 ~]# echo '这里是andorid页面' > /code/android/index.html
# 4.重启nginx
[root@web02 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 ~]# systemctl restart nginx
# 5.域名解析
10.0.0.8 andorid.hg.com
# 6.浏览器访问:http://andorid.hg.com:9091/
部署IOS页面
# 1.编写ios页面nginx配置文件
[root@web03 ~]# yum install -y nginx
[root@web03 ~]# vim /etc/nginx/conf.d/ios.conf
server {
listen 9092;
server_name ios.hg.com;
charset utf-8;
location / {
root /code/ios;
index index.html;
}
}
# 2.创建站点目录
[root@web03 ~]# mkdir /code/ios -p
# 3.部署IOS代码
[root@web03 ~]# echo '这里是IOS页面' > /code/ios/index.html
# 4.重启nginx
[root@web03 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web03 ~]# systemctl start nginx
# 5.域名解析
10.0.0.9 ios.hg.com
# 6.浏览器访问:http://ios.hg.com:9092
# 1.编写ios页面nginx配置文件
[root@web03 ~]# yum install -y nginx
[root@web03 ~]# vim /etc/nginx/conf.d/ios.conf
server {
listen 9092;
server_name ios.hg.com;
charset utf-8;
location / {
root /code/ios;
index index.html;
}
}
# 2.创建站点目录
[root@web03 ~]# mkdir /code/ios -p
# 3.部署IOS代码
[root@web03 ~]# echo '这里是IOS页面' > /code/ios/index.html
# 4.重启nginx
[root@web03 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web03 ~]# systemctl start nginx
# 5.域名解析
10.0.0.9 ios.hg.com
# 6.浏览器访问:http://ios.hg.com:9092
资源分离配置
[root@lb01 ~]# vim /etc/nginx/conf.d/source.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.fenli.com;
charset 'utf-8';
location / {
#如果客户端来源是Android则跳转到Android的资源;
if ($http_user_agent ~* "Android") {
proxy_pass http://android;
}
#如果客户端来源是Iphone则跳转到Iphone的资源;
if ($http_user_agent ~* "Iphone") {
proxy_pass http://ios;
}
#如果客户端是IE浏览器则返回403错误;
#if ($http_user_agent ~* "MSIE") {
# return 403;
#}
#默认跳转pc资源;
proxy_pass http://pc;
}
}
# 4.重启nginx
[root@lb01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 ~]# systemctl restart nginx
# 5.域名解析
10.0.0.5 www.fenli.com
# 6.浏览器访问:http://www.fenli.com
[root@lb01 ~]# vim /etc/nginx/conf.d/source.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.fenli.com;
charset 'utf-8';
location / {
#如果客户端来源是Android则跳转到Android的资源;
if ($http_user_agent ~* "Android") {
proxy_pass http://android;
}
#如果客户端来源是Iphone则跳转到Iphone的资源;
if ($http_user_agent ~* "Iphone") {
proxy_pass http://ios;
}
#如果客户端是IE浏览器则返回403错误;
#if ($http_user_agent ~* "MSIE") {
# return 403;
#}
#默认跳转pc资源;
proxy_pass http://pc;
}
}
# 4.重启nginx
[root@lb01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 ~]# systemctl restart nginx
# 5.域名解析
10.0.0.5 www.fenli.com
# 6.浏览器访问:http://www.fenli.com
PC端访问结果
Android端访问结果
IOS端访问结果
IE浏览器访问结果
Comments | NOTHING