动静分离
概念:将动态访问与静态访问区分
作用:提高用户访问速率,及用户体验
-
原理
静态:nginx处理
动态:tomcat处理
优点:解决了tomcat处理静态资源效率不高及资源开销问题
单机实战
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n17" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#一.Tomcat端
1.安装
[root@web01 ~]# yum indtall -y java tomcat
2.配置创建站点目录
[root@web01 ~]# mkdir /usr/share/tomcat/webapps/ROOT
3.上传Java代码
[root@web01 ~]# vi /usr/share/tomcat/webapps/ROOT/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"
%>
<html>
<head>
<title>Nginx+Tomcat</title>
</head>
<body>
<%
Random rand = new Random();
out.println("<h2>动态资源</h2>");
out.println(rand.nextInt(99)+100);
%>
<h2>静态图片</h2>
<img src="nginx.png" />
</body>
</html>
4.上传静态图片
[root@web01 ~]# wget 或 rz
5.启动
[root@web01 ~]# systemctl start tomcat
二.nginx
1.安装
2.配置
[root@web01 ~]# vim /etc/nginx/conf.d/dj.conf
server {
listen 80;
server_name dj.com;
location / {
proxy_pass http://127.0.0.1:8080;
}
location ~ .(png|jpg|gif|mp4)$ {
root /images;
expires 1d;
}
}
3.启动
[root@web01 ~]# systemctl restart nginx
4.host劫持,浏览网页</pre>
集群实战
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n19" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#1.将单台中动与静的配置中的其中之一随意scp至另一台主机
2.配置负载均衡
[root@web01 ~]# vim /etc/nginx/conf.d/dj.conf
upstream dong {
server 127.16.1.7:8080;
}
uostream jing {
server 127.16.1.8:80;
}
server {
listen 80;
server dj.com
location / {
proxy_pass http://dong;
include proxy_params;
}
location ~ .(png|jpg|jmp|mp4)$ {
proxy_pass http://jing;
include proxy_params;
expires 2d;
}
}
3.启动
[root@web01 ~]# systemctl start nginx
4.host劫持,浏览网页</pre>
Rewrite
基本概念
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n22" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#作用:使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志实现url重写及重定向
PS:rewrite只能放在server,location,if 中并只能对域名后边出去传递的参数外的字符串起作用;
例如:http://seanlook.com/a/we/index.php?id=1&u=str 只对/a/we/index.php重写。</pre>
语法
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n24" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">rewrite regex replacement [flag]
关键字 正则 改写内容 标记
如果相对域名或参数字符串起作用,可以使用全局变量匹配,也可用proxy_pass反向代理
例如
rewrite ^/(.*) http://www.czlun.com/$1 permanent;</pre>
location 与 rewrite 的区别
相同:都能实现跳转;
-
不同
rewrite:在同一域名内获取资源路径
location:是对一类路径做控制访问或反向代理,可proxy_pass至其他主机
实战
1.改写url
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n36" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@web01 conf.d]# vim url.conf
server {
listen 80;
server_name url.com.cn url.com.en;
location / {
if (language zh;
}
if (language en;
}
root /data/$languade;
}
}
server {
listen 80;
server_name url.com;
location / {
root /data;
}
}</pre>
2.根据浏览器语言
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n38" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@web01 conf.d]# vim url.conf
server {
listen 80;
server_name url.com;
location / {
if (language en;
}
if (language zh;
}
root /data/$language;
}
}</pre>
return
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n40" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">server {
listen 80;
server_name url.com;
location / {
default_type text/html;
if (request_uri ~* "est") {
return 302 "https://www.jd.com";
}
}
}</pre>
永久维护
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n43" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">server {
listen 80;
server_name url.oldxu.com;
root /data;
rewrite ^(.*)language /default;
if ( language /zh;
}
if ( language /en;
}
if ( language /jp;
}
rewrite ^/language;
location / {
index index.html;
}
}</pre>
临时维护(jd)
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n45" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#error_page 403 404 500 502 /wh.png;
error_page 403 404 500 502 http://$http_host;
error_page 403 404 500 502 @temperror;
location @temperror {
rewrite ^(.*)http_host;
}
</pre>
flag
redirect ---302---临时跳转
permanent---301---永久跳转
last---停止当前location,继续匹配下一个location;
break ---终止当前匹配
案例
需求: 用户通过手机设备访问url.oldxu.com,跳转至url.oldxu.com/m
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="bash" cid="n55" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">server {
listen 80;
server_name url.oldxu.com;
root /data;
if ( /m;
}
}
</pre>
需求: 用户通过手机设备访问url.oldxu.com,跳转至m.oldxu.com
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="bash" cid="n58" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">server {
listen 80;
server_name url.oldxu.com;
root /data;
if ( http://m.oldxu.com;
}
}
server {
listen 80;
server_name m.oldxu.com;
root /data/m;
location / {
index index.html;
}
}
</pre>
需求: 用户访问oldxu.com/test,跳转至https://xuliangwei.com location-------------------------------------------------------------- location /test { #rewrite ^(.*)$ https://www.xuliangwei.com/; return 302 https://www.xuliangwei.com/; } if ---------------------------------------------------------------------
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="bash" cid="n60" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">server {
listen 80;
server_name url.oldxu.com;
root /data;
if ( https://www.xuliangwei.com/;
return 302 https://www.xuliangwei.com/;
}
location / {
index index.html;
}
}
</pre>
rewrite场景示例
- 需求: 用户访问course-11-22-33.html实际上真实访问是/course/11/22/33/course_33.html
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="bash" cid="n65" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@web01 conf.d]# cat url.oldxu.com.conf
server {
listen 80;
server_name url.oldxu.com;
root /data;
location / {
index index.html;
#用户访问的url #文件真实位置
rewrite ^/(.)-(.)-(.)-(.).html /2/
4/
4.html;
}
}
</pre>