当前位置: 首页 > news >正文

【运维知识进阶篇】集群架构-Rewrite重定向

Rewrite主要实现url地址重写,以及重定向,就是把传入web的请求重定向到其他url的过程。

分以下几种场景使用

1、地址跳转,用户访问一个URL,将其定向到另一个URL

2、协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式

3、伪静态,动态页面静态化,为了搜素引擎收录。

4、搜索引擎,SEO优化依赖于url路径,好记的url便于支持搜索引擎录入

Rewrite标记

每个Rewrite后面都有flag标记,主要有以下几种

flag规则
last停止当前匹配,并重新发送请求
barek终止匹配,不发送新请求
redirector临时跳转,关闭nginx请求就不跳转了,302
premanent永久跳转,访问过一次就不会访问原站了,301,第一次请求会保存缓存到浏览器中,通过浏览器缓存跳转

更改配置文件,准备代码文件进行测试last与break

[root@Web01 conf.d]# vim rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code/rewrite/;location / {rewrite /1.html /2.html;rewrite /2.html /3.html;}location /2.html {rewrite /2.html /a.html;}location /3.html {rewrite /3.html /b.html;
"rewrite.conf" 18L, 343C written 
[root@Web01 conf.d]# systemctl restart nginx
[root@Web01 conf.d]# mkdir -p /code/rewrite
[root@Web01 conf.d]# echo 1.html > /code/rewrite/1.html
[root@Web01 conf.d]# echo 2.html > /code/rewrite/2.html
[root@Web01 conf.d]# echo 3.html > /code/rewrite/3.html
[root@Web01 conf.d]# echo a.html > /code/rewrite/a.html
[root@Web01 conf.d]# echo b.html > /code/rewrite/b.html

发现访问1.html,实际重定向到了b.html 

添加last标记

[root@Web01 conf.d]# vim rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code/rewrite/;location / {rewrite /1.html /2.html last;rewrite /2.html /3.html;}location /2.html {rewrite /2.html /a.html;}location /3.html {rewrite /3.html /b.html;
"rewrite.conf" 18L, 348C written 
[root@Web01 conf.d]# systemctl restart nginx

 跳过了当前location,进行下一location重定向,最终跳转到a.html

 

添加down标记

[root@Web01 conf.d]# vim rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code/rewrite/;location / {rewrite /1.html /2.html break;rewrite /2.html /3.html;}location /2.html {rewrite /2.html /a.html;}location /3.html {rewrite /3.html /b.html;
"rewrite.conf" 18L, 349C written 
[root@Web01 conf.d]# systemctl restart nginx

break后不再进行重定向操作,最终定向到2.html 

 redirect与permanent测试

[root@Web01 conf.d]# vim rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code;location /test {#临时重定向rewrite ^(.*)$  http://www.koten.vip redirect;    #return 302 http://www.koten.vip#永久重定向#rewrite ^(.*)$  http://www.koten.vip permanent;  #return 301 http://www.koten.vip;}
}
~                                                  
~                                                  
"rewrite.conf" 12L, 356C written 
[root@Web01 conf.d]# systemctl restart nginx

 访问rewrite.koten.com/test,定向到www.koten.vip

Rewrite使用案例

我们先开启rewrite日志对规则进行匹配调试

 rewrite_log on;  #加入到/etc/nginx/nginx.conf中

案例1:用户访问/abc/1.html实际上真实访问的是/ccc/bbb/2.html中

[root@Web01 conf.d]# mkdir -p /code/rewrite/ccc/bbb/
[root@Web01 conf.d]# echo '/ccc/bbb/2.html' > /code/rewrite/ccc/bbb/2.html
[root@Web01 conf.d]# vim rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code/rewrite;location /abc {rewrite (.*) /ccc/bbb/2.html redirect;#return 302 /ccc/bbb/2.html}
}
~                                                  
~                                                  
~                                                  
~                                                  
~                                                  
"rewrite.conf" 10L, 217C written 
[root@Web01 conf.d]# systemctl restart nginx

案例2:用户访问/2018/ccc/bbb/2.html实际上真实访问的是/2023/ccc/bbb.2.html

[root@Web01 conf.d]# mkdir -p /code/rewrite/2023/ccc/bbb/
[root@Web01 conf.d]# echo '/2023/ccc/bbb/2.html' > /code/rewrite/2023/ccc/bbb/2.html
[root@Web01 conf.d]# vim rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code/rewrite;location /2018 {rewrite ^/2018/(.*) /2023/$1 redirect;}
}
~                                                  
~                                                  
~                                                  
~                                                  
~                                                  
~                                                  
"rewrite.conf" 9L, 188C written  
[root@Web01 conf.d]# systemctl restart nginx

案例3:用户访问/test实际上访问的是https://www.koten.vip

[root@Web01 conf.d]# vim rewrite.conf
server {listen 80;server_name rewrite.koten.com;location /test {rewrite (.*) https://www.koten.vip redirect;}
}
~                                                  
~                                                  
~                                                  
~                                                  
~                                                  
~                                                  
~                                                  
"rewrite.conf" 8L, 154C written
[root@Web01 conf.d]# systemctl restart nginx

案例4:访问course-11-22-33.html实际真实访问/course/11/22/33/course_33.html

[root@Web01 conf.d]# mkdir -p /code/rewrite/course/
11/22/33
[root@Web01 conf.d]# echo '/code/rewrite/course/11/22/33' >
/code/rewrite/course/11/22/33/course_33.html
[root@Web01 conf.d]# vim /etc/nginx/conf.d/rewrite.conf 
server {listen 80;server_name rewrite.koten.com;root /code/rewrite;index index.html;location / {rewrite ^/course-(.*)-(.*)-(.*).html$ /course/$1/$2/$3/course_$3.html redirect;}
}
~                                                  
~                                                  
~                                                  
~                                                  
~                                                  
"rewrite.conf" 10L, 230C written 
[root@Web01 conf.d]# systemctl restart nginx

 案例5:将http请求跳转到https

[root@Web01 conf.d]# vim /etc/nginx/conf.d/rewrite.conf
server {listen 443;server_name rewrite.koten.com;location / {root /code;index index.php index.html;}
}server {listen 80;server_name rewrite.koten.com;rewrite ^(.*) https://$server_name$1 redire
ct;
"rewrite.conf" 17L, 285C written 
[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]# systemctl restart nginx

案例6:错误页跳转

[root@Web01 rewrite]# cat /etc/nginx/conf.d/rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code/rewrite;error_page 403 404 500 501 502 @error_test;location @error_test {rewrite ^(.*)$ /404.png break;}
}[root@Web01 rewrite]# systemctl restart nginx

案例7:在跳转的请求行后加上想要的参数&showoffline=1

[root@Web01 rewrite]# vim /etc/nginx/conf.d/rewrite.conf
server {listen 80;server_name rewrite.koten.com;set $args "&showoffline=1";location / {root /code/rewrite;index index.html;}if ($remote_addr = 10.0.0.1 ){rewrite (.*) http://rewrite.koten.com$1;}
}
~                                                  
~                                                  
~                                                  
<rewrite.conf" 13L, 252C written 
[root@Web01 rewrite]# systemctl restart nginx

 

案例8:网站维护,指定IP正常访问,其他IP跳转至维护页面

[root@Web01 rewrite]# vim /etc/nginx/conf.d/rewrite.conf
server {listen 80;server_name rewrite.koten.com;root /code/rewrite;charset utf-8,gbk;location / {index index.html;if ($remote_addr != "10.0.0.2"){rewrite ^(.*)$ /网站维护.jpg break; #如
果来源IP不等于10.0.0.1,则跳转维护页面}}}
<rewrite.conf" 16L, 321C written 
[root@Web01 rewrite]# systemctl restart nginx

Nginx内置参数

$args               #这个变量等于请求行中的参数。
$content_length     #请求头中的Content-length字段。
$content_type       #请求头中的Content-Type字段。
$document_root      #当前请求在root指令中指定的值。
$host               #请求主机头字段,否则为服务器名称。
$http_user_agent    #客户端agent信息
$http_cookie        #客户端cookie信息
$limit_rate         #这个变量可以限制连接速率。
$request_body_file  #客户端请求主体信息的临时文件名。
$request_method     #客户端请求的动作,通常为GET或POST。
$remote_addr        #客户端的IP地址。
$remote_port        #客户端的端口。
$remote_user        #已经经过Auth Basic Module验证的用户名。
$request_filename   #当前请求的文件路径,由root或alias指令与URI请求生成。
$query_string       #与$args相同。
$scheme             #HTTP方法(如http,https)。
$server_protocol    #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_addr        #服务器地址,在完成一次系统调用后可以确定这个值。
$server_name        #服务器名称。
$server_port        #请求到达服务器的端口号。
$request_uri        #包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
$uri                #不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
$document_uri       #与$uri相同。
$X-Forwarded-For:HTTP的请求端真实的IP,只有在通过了HTTP 代理或者负载均衡服务器时才会添加该项。标准格式如下:X-Forwarded-For: client1, proxy1, proxy2

我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!

http://www.lryc.cn/news/69575.html

相关文章:

  • JavaScript如何使用while循环
  • 『MySQL 实战 45 讲』16 - “order by” 是怎么工作的
  • 怎么给移动硬盘查错?移动硬盘查错能恢复数据吗
  • javaIO流之缓冲流
  • 定义制造业操作(定义 MES/MOM 系统)
  • 人工智能专栏第二讲——人工智能的基础技术
  • 注意!ChatGPT的Plus账号也会被封禁
  • 理解:Public Key Cryptography的应用
  • 深度学习中的图像分类介绍
  • 自然语言处理基础
  • 低代码与其拓荒,不如颠覆开发行业
  • 【数据结构】散列表(哈希表)
  • Flutter 笔记 | Flutter 核心原理(一)架构和生命周期
  • 【Linux进阶之路】基本指令(下)
  • Vue--》Vue 3 路由进阶——从基础到高级的完整指南
  • 【华为OD机试真题】【python】 网上商城优惠活动(一)【2022 Q4 | 100分】
  • 【业务数据分析】—— 用户留存分析(以挖掘Aha时刻为例)
  • 极客的git常用命令手册
  • spring-data 一统江湖,玩转多种数据源
  • 【EMC专题】为什么PCB上的单端阻抗控制在50欧?
  • 想自学写个操作系统,有哪些推荐看的书籍?
  • 深入理解Java虚拟机:JVM高级特性与最佳实践-总结-7
  • ES6中flat与flatMap使用
  • 苹果手机、电脑如何进行屏幕录制?苹果录屏功能在哪?
  • 什么是研发 Lead Time?我悟了!
  • android 窗口焦点介绍
  • 研发工程师玩转Kubernetes——构建、推送自定义镜像
  • [网络安全]DVWA之XSS(Stored)攻击姿势及解题详析合集
  • VP记录:Codeforces Round 873 (Div. 2) A~D1
  • 【C++】函数提高