day15.1
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 |
部署前端(静态页面)
[root@web01 ~]
[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 ~]
[root@web02 conf.d]
server {
listen 80;
server_name pic.wc.com;
charset utf-8;
root /code;
index index.html;
location ~* \.(jpg|png|gif)$ {
root /code/images;
}
}
[root@web02 conf.d]
[root@web02 conf.d]
10.0.0.8 pic.wc.com
[root@web02 ~]
[root@web02 ~]
[root@web02 conf.d]
total 112
-rw-r--r-- 1 root root 113402 Jun 6 15:46 abc.jpg
部署后端(动态页面)
[root@web03 webapps]
[root@web03 webapps]
[root@web03 webapps]
tcp6 0 0 :::8080 :::* LISTEN 7742/java
[root@web03 webapps]
/usr/share/tomcat/webapps
[root@web03 webapps]
部署代码
[root@web03 webapps]
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
<HEAD>
<TITLE>曾老湿JSP Page</TITLE>
</HEAD>
<BODY>
<%
Random rand = new Random();
out.println("<h1>嗨害:<h1>");
out.println(rand.nextInt(99)+100);
%>
</BODY>
</HTML>

在代理整合资源
[root@web01 conf.d]
upstream static {
server 172.16.1.8:80;
}
upstream java {
server 172.16.1.9:8080;
}
server {
listen 80;
server_name pic.wc.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;
}
}
[root@web01 ~]
10.0.0.7 pic.wc.com
代理部署
[root@web01 conf.d]
upstream static {
server 172.16.1.8:80;
}
upstream java {
server 172.16.1.9:8080;
}
server {
listen 80;
server_name pic.wc.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;
}
}
[root@web01 ~]
[root@web01 conf.d]
<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.drz.com/test.jsp",
success: function(data){
$("#get_data").html(data)
},
error: function() {
alert("哎呦喂,失败了,回去检查你服务去~");
}
});
});
</script>
<body>
<h1>曾老湿带你测试动静分离</h1>
<img src="http://pic.drz.com/abc.jpg">
<div id="get_data"></div>
</body>
</html>
[root@web01 ~]
[root@web01 ~]
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端 |
nginx、iOS端代码 |
lb01 |
10.0.0.5 |
172.16.1.5 |
代理 |
nginx |
部署PC端
[root@web01 conf.d]
server{
listen 9090;
server_name pc.wc.com;
location /{
root /code/pc;
index index.html;
}
}
[root@web01 conf.d]
[root@web01 ~]
[root@web01 conf.d]
[root@web01 conf.d]
10.0.0.7 pc.wc.com

部署安卓端代码
[root@web02 conf.d]
server{
listen 9091;
server_name an.wc.com;
charset utf-8;
location /{
root /code/an;
index index.html;
}
}
[root@web02 conf.d]
[root@web02 conf.d]
[root@web02 conf.d]
[root@web02 conf.d]
10.0.0.8 an.wc.com

部署iOS页面
[root@web03 webapps]
server{
listen 9092;
server_name ios.wc.com;
location /{
root /code/ios;
index index.html;
}
}
[root@web03 webapps]
[root@web03 webapps]
[root@web03 webapps]
[root@web03 webapps]
10.0.0.9 an.wc.com

资源分离配置
[root@lb01 conf.d]
upstream pc {
server 172.16.1.7:9090;
}
upstream an {
server 172.16.1.8:9091;
}
upstream ios {
server 172.16.1.9:9092;
}
server {
listen 80;
server_name www.wc.com;
charset 'utf-8';
location / {
if ($http_user_agent ~* "Android") {
proxy_pass http://an;
}
if ($http_user_agent ~* "Iphone") {
proxy_pass http://ios;
}
if ($http_user_agent ~* "(MSIE|WOW64)") {
return 403;
}
proxy_pass http://pc;
}
}


iOS端


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了