Day-44Tomcat与Rewrite

更新时间:2023-05-10 18:59:05 阅读:8 评论:0

Day-44Tomcat与Rewrite
动静分离
概念:将动态访问与静态访问区分
作⽤:提⾼⽤户访问速率,及⽤户体验
原理
静态:nginx处理
动态:tomcat处理
优点:解决了tomcat处理静态资源效率不⾼及资源开销问题
单机实战
<pre spellcheck="fal" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n17" mdtype="fences" Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; b
ackground: 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.Int(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.f
rver {
listen 80;
rver_;
3.启动
[root@web01 ~]# systemctl restart nginx
4.host劫持,浏览⽹页</pre>
集群实战
<pre spellcheck="fal" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n19" mdtype="fences" 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.f
upstream dong {
rver 127.16.1.7:8080;
}
uostream jing {
rver 127.16.1.8:80;
}
rver {
listen 80;
3.启动
[root@web01 ~]# systemctl start nginx
4.host劫持,浏览⽹页</pre>
Rewrite
基本概念
<pre spellcheck="fal" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" ci
d="n22" mdtype="fences" 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只能放在rver,location,if 中并只能对域名后边出去传递的参数外的字符串起作⽤;
例如:/a/we/index.php?id=1&u=str 只
对/a/we/index.php重写。</pre>
语法
<pre spellcheck="fal" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n24" mdtype="fences" 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反向代理
例如
location 与 rewrite 的区别
相同:都能实现跳转;
不同
rewrite:在同⼀域名内获取资源路径
location:是对⼀类路径做控制访问或反向代理,可proxy_pass⾄其他主机
实战
1.改写url
<pre spellcheck="fal" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n36" mdtype="fences" 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]# f
rver {
listen 80;
rver_ ;
location / {
if (language zh;
}
if (language en;
}
root /data/$languade;
}
}
rver {
listen 80;
rver_;
location / {
root /data;
}
}</pre>
2.根据浏览器语⾔
<pre spellcheck="fal" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" ci
d="n38" mdtype="fences" 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]# f
rver {
listen 80;
rver_;
location / {
if (language en;
}
if (language zh;
}
root /data/$language;
}
}</pre>
r etur n
eturn
<pre spellcheck="fal" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n40" mdtype="fences" 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; whi
te-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;">rver {
listen 80;
rver_;
永久维护
<pre spellcheck="fal" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n43" mdtype="fences" 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;">rver {
listen 80;
rver_name ;
root /data;
rewrite ^(.*)language /default;
if ( language /zh;
}
if ( language /en;
}
if ( language /jp;
}
rewrite ^/language;
location / {
index index.html;
}
}</pre>
临时维护(jd)

本文发布于:2023-05-10 18:59:05,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/880145.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:访问   静态   资源   域名   实现   结合
相关文章
留言与评论(共有 0 条评论)
昵称:
匿名发表 登录账号
         
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图