静态请求:不需要访问数据库
动态请求:需要访问数据库,登录
静态页:打开一个网页,不需要访问数据库
动态页:打开一个网页,需要访问数据库
无论是静态资源还是动态资源,都需要做共享存储
web单台服务器实现动静分离
location / {
root /code/wordpress;
index index.php;
}
location ~* \.(png|jpg|mp4|)${
root /code/wordpress/images;
gzip on;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
}
多台服务器实现动静分离
环境准备
系统 | 作用 | 服务 | 地址 |
---|---|---|---|
Centos7.5 | 负载均衡 | nginx proxy | 10.0.0.5 |
Centos7.5 | 静态资源 | nginx static | 10.0.0.7 |
Centos7.5 | 动态资源 | tomcat server | 10.0.0.8 |
web01配置静态资源
[root@web01 ~]# vim /etc/nginx/conf.d/jt.conf
server {
listen 80;
server_name cs.jd.com;
root /code;
index index.html;
location ~* \.(jpg|png|gif)$ {
root /code/images;
}
}
#配置一个主页
[root@web01 conf.d]# echo "web01" > /code/index.html
#创建图片目录
[root@web01 conf.d]# mkdir /code/images/
#上传一个静态文件
[root@web01 conf.d]# cd /code/images/
[root@web01 images]# rz cjk.gif
[root@web01 conf.d]# 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 conf.d]# nginx -s reload
#浏览器访问
web02配置动态资源
Tomcat使用Java代码
Tomcat的访问与nginx无关
[root@web02 ~]# yum install -y tomcat
[root@web02 ~]# mkdir /usr/share/tomcat/webapps/ROOT
[root@web02 ~]# vim /usr/share/tomcat/webapps/ROOT/1.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>
[root@web02 ~]# systemctl start tomcat
# 打开浏览器访问
http://10.0.0.8:8080/1.jsp
负载均衡上整合静态资源和动态资源
[root@lb01 conf.d]# vim /etc/nginx/conf.d/jd.conf
upstream static {
server 172.16.1.7:80;
}
upstream java {
server 172.16.1.8:8080;
}
server {
listen 80;
server_name cs.jd.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@lb01 conf.d]# 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 conf.d]# nginx -s reload
2.5 配置本地hosts,通过负载访问动态与静态资源
负载均衡上整合动静分离模板
[root@lb01 ~]# vim /etc/nginx/conf.d/djfl.conf
upstream static {
server 172.16.1.7:80;
}
upstream java {
server 172.16.1.8:8080;
}
server {
listen 80;
server_name pic.drz.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@lb01 ~]# mkdir -p /code
#编辑整合后的index.html
[root@lb01 ~]# cat /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.drz.com/java_test.jsp",
success: function(data){
$("#get_data").html(data)
},
error: function() {
alert("哎呦喂,失败了,回去检查你服务去~");
}
});
});
</script>
<body>
<h1>曾老湿带你测试动静分离</h1>
<img src="http://pic.drz.com/cjk.gif">
<div id="get_data"></div>
</body>
</html>
tomcat
[root@web02 ~]# yum install -y tomcat
[root@web02 ~]# rpm -q tomcat
tomcat-7.0.76-11.el7_7.noarch
[root@web02 ~]# rpm -ql tomcat
/etc/logrotate.d/tomcat #日志切割
/etc/sysconfig/tomcat #
/etc/tomcat/conf.d #从配置文件
/etc/tomcat/tomcat.conf #主配置文件
/usr/bin/tomcat-digest
/usr/bin/tomcat-tool-wrapper
/usr/lib/systemd/system/tomcat.service #
/usr/lib/systemd/system/tomcat@.service
/usr/sbin/tomcat #
/usr/share/tomcat/conf
/usr/share/tomcat/lib
/usr/share/tomcat/logs
/usr/share/tomcat/temp
/usr/share/tomcat/webapps
/usr/share/tomcat/work
/var/cache/tomcat/temp #
/var/cache/tomcat/work
/var/lib/tomcat
/var/lib/tomcat/webapps
/var/lib/tomcats
/var/log/tomcat #日志
[root@web02 ROOT]# netstat -lntup
tcp6 0 0 :::8080 :::* LISTEN 13065/java
[root@web02 ROOT]# ps -ef|grep tomcat