Linux服务器技术教程之七重定向

举报
tea_year 发表于 2025/01/16 20:47:12 2025/01/16
247 0 0
【摘要】 重定向(Redirect)指通过各种方法将各种网络请求重新定个方向转到其它位置(如:网页重定向、域名的重定向、路由选择的变化也是对数据报文经由路径的一种重定向)。 URL重写是指通过配置conf文件,以让网站的URL中达到某种状态时则定向/跳转到某个规则,比如常见的伪静态、301重定向、浏览器定向等。当客户端浏览某个网址时,将其访问导向到另一个网址的技术。

课程目标

  • 理解重定向

  • 掌握指令

一、重定向概述

重定向:也叫url重定向,也叫url改写

未来需求:

  • 网站是http(80)---------------->https(443) URL重定向

    用户http://www.baidu.com https://www.baidu.com

  • 根据客户端访问类型进行跳转 希望根据用户客户端进行判断

    如果用户的客户端是ios,iphone,android,访问m.jd.com. 否则默认访问www.jd.com

  • 新老域名跳转:

    www.360buy.com > jd.com

  • 需要我们调整url格式:伪静态(搜索引擎收入) 运营要求.

二、模块与指令

相关的指令 说明
return 实现对url的改写,一般与nginx变量一起使用
rewrite 实现对url的改写,使用正则匹配uri进行改写,还有各种标记
set 创建或修改nginx变量
if 判断,一般与nginx变量一起使用

1. return 指令

格式 说明
格式1 return code URL; 返回新的状态码+新的URL地址
格式2 return code 返回指定的状态码
放哪 server,location,if
 #如果用户访问/admin/页面返回403
 [root@web01 /etc/nginx/conf.d]# cat rewrite.cn.conf.bak 
 server{
     listen 80;
     server_name rewrite.cn;
     root /app/code/rewrite;
     error_log  /var/log/nginx/rewrite.cn-error.log notice;
     access_log  /var/log/nginx/rewrite.cn-access.log  main;
  
     location / { 
         index index.html;
     
     }
     location /admin/ {
         return 403;
     }
 [root@web01 /etc/nginx/conf.d]# mkdir /app/code/rewrite
 [root@web01 /etc/nginx/conf.d]# echo rewrite > /app/code/rewrite/index.html
 [root@web01 /etc/nginx/conf.d]# mkdir /app/code/rewrite/admin
 [root@web01 /etc/nginx/conf.d]# echo admin index  > /app/code/rewrite/admin/index.html
 ​
 [root@web01 /etc/nginx/conf.d]# curl -H Host:rewrite.cn http://10.0.0.7/admin/
 <html>
 <head><title>403 Forbidden</title></head>
 <body>
 <center><h1>403 Forbidden</h1></center>
 <hr><center>nginx/1.22.1</center>
 </body>
 </html>
 [root@web01 /etc/nginx/conf.d]# curl -H Host:rewrite.cn http://10.0.0.7
 rewrite
 #域名间跳转
 [root@web01 /etc/nginx/conf.d]# cat rewrite.cn.conf.bak 
 server{
     listen 80;
     server_name rewrite.cn;
     return 301 http://www.baidu.com;
 }
  
 #-L location 跟随跳转,响应是301,302跳转的时候使用.
 [root@web01 /etc/nginx/conf.d]# curl -Lv -H Host:rewrite.cn http://10.0.0.7
 * About to connect() to 10.0.0.7 port 80 (#0)
 *   Trying 10.0.0.7...
 * Connected to 10.0.0.7 (10.0.0.7) port 80 (#0)
 > GET / HTTP/1.1
 > User-Agent: curl/7.29.0
 > Accept: */*
 > Host:rewrite.cn
 > 
 < HTTP/1.1 301 Moved Permanently
 < Server: nginx/1.22.1
 < Date: Fri, 17 Feb 2023 06:40:29 GMT
 < Content-Type: text/html
 < Content-Length: 169
 < Connection: keep-alive
 < Location: http://www.baidu.com
 < 
 * Ignoring the response-body
 * Connection #0 to host 10.0.0.7 left intact
 * Issue another request to this URL: 'http://www.baidu.com'
 * About to connect() to www.baidu.com port 80 (#1)
 *   Trying 110.242.68.4...
 * Connected to www.baidu.com (110.242.68.4) port 80 (#1)
 > GET / HTTP/1.1
 > User-Agent: curl/7.29.0
 > Host: www.baidu.com
 > Accept: */*
 > 
 < HTTP/1.1 200 OK
 < Accept-Ranges: bytes
 < Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
 < Connection: keep-alive
 < Content-Length: 2381
 < Content-Type: text/html
 < Date: Fri, 17 Feb 2023 06:40:35 GMT
 < Etag: "588604c1-94d"
 < Last-Modified: Mon, 23 Jan 2017 13:27:29 GMT
 < Pragma: no-cache
 < Server: bfe/1.0.8.18

2.使用浏览器插件查看

2.1.安装插件

1238843-20230222210433129-1824555710.png


2.2.查看跳转

1238843-20230222210445057-1036096208.png


3. if判断

指令 说明
放在哪里 server,loaction
 #访问rewrite.cn 网站只准许GET,POST 请求方法,其他访问禁止访问
 [root@web01 /etc/nginx/conf.d]# cat rewrite.cn.conf.bak 
 server{
     listen 80;
     server_name rewrite.cn;
     root /app/code/rewrite ;
     if ( $request_method  !~ "GET|POST"  ) {
        return 403;
     }
   
     location / {
       index index.html;
     }        
     
 }
 #正常访问
 [root@web01 /etc/nginx/conf.d]# curl -H Host:rewrite.cn http://10.0.0.7
 rewrite
 #HEAD方式访问
 [root@web01 /etc/nginx/conf.d]# curl -I -H Host:rewrite.cn http://10.0.0.7
 HTTP/1.1 403 Forbidden
 Server: nginx/1.22.1
 Date: Fri, 17 Feb 2023 07:14:47 GMT
 Content-Type: text/html
 Content-Length: 153
 Connection: keep-alive
  
 注: $request_method 取出请求方法.

if指令可以使用的符号

常用~,~, !~,!~

nginx取反,排除,只能用if.

补充:

if只能有一重判断,不能套

4. set

温馨提示:

nginx变量,进行赋值与使用都需要加上$符号

 #设置网站是否为维护状态:如果维护状态:返回503状态码,否则正常访问
 #设置标记$flag 默认是0.
 #判断如果$flag的值是1则网站返回503
 server{
     listen 80;
     server_name rewrite.cn;
     root /app/code/rewrite ;
     set $flag 1;
     if ( $flag = 1 ){
       return 503;
     }
     location / {
       index index.html;
     }   
 }
  

5. rewrite

5.1 rewrite指令

  • rewrite正则用于匹配用户请求的uri.

  • 命令的格式与sed 'sg' 类似,实现替换功能,rewrite替换url内容.(改写)

指令 说明
格式 rewrite 找什么(具体内容/正则/保护分组) 替换成什么(具体内容,后向引用) [标记]; 标记可以省略,默认使用 redirect标记(302)
放在哪里 server,location,if
rewrite标记
redirect 301
permanent 302

温馨提示:

rewrite匹配的内容,匹配uri

标记的时候,用的是单词,不是数字

 #域名跳转
 [root@web01 /etc/nginx/conf.d]# cat rewrite.cn.conf
 server{
     listen 80;
     server_name rewrite.cn;
     rewrite ^(.*)$ http://www.baidu.com$1;  
 }
  

5.2 rewrite标记

标记 说明 补充
redirect 302 临时 用户访问的时候,收到302提示及新的位置Location(响应头),用户根据 Location新的位置进行访问(让用户重新发出http请求) 新旧地址都可以用.
permanent 301 永久 用户访问的时候,收到302提示及新的位置Location(响应头),用户根据 Location新的位置进行访问(让用户重新发出http请求) 旧的地址排名取消,旧地旧的 不用了,只用新的网站.
break 用户的请求匹配到包含break指令或rewrite规则后,及时后面还有location规则, 不会继续运行.终止运行.
last 用户请求匹配到包含last标记的rewrite规则后,停止继续执行,ngx会重新发出内 部请求,请求与location规则进行匹配. 开启nginx rewrite_log才能 看到
 server {
    listen 80;
    server_name flag.cn;
    root /app/code/flag;
    error_log /var/log/nginx/flag-error.log notice;
    rewrite_log on; #需要错误日志debug  notice
    location / {
    rewrite /1.html /2.html ;
    rewrite /2.html /3.html ;
    }
    location /2.html {
      rewrite /2.html /3.html ;
    }
    location /3.html {
      rewrite /3.html /a.html ;
    }
 }
 echo 1.html url >/app/code/flag/1.html
 echo 2.html url >/app/code/flag/2.html
 echo 3.html url >/app/code/flag/3.html
 echo a.html url >/app/code/flag/a.html
 echo b.html url >/app/code/flag/b.html
  
 1.访问/1.html显示a.html内容
 [root@web01 /etc/nginx/conf.d]# curl -H Host:flag.cn 10.0.0.7/1.html
 a.html url
  
 2.访问/2.html显示a.html内容
 [root@web01 /etc/nginx/conf.d]# curl -H Host:flag.cn 10.0.0.7/2.html
 a.html url
  
 3. 在rewrite /1.html /2.html的时候加上标记break标记.
 rewrite /1.html /2.html break; 执行完成rewrite后直接结束.
 server {
    listen 80;
    server_name flag.cn;
    root /app/code/flag;
    error_log /var/log/nginx/flag-error.log notice;
    rewrite_log on; #需要错误日志debug  notice
    location / {
    rewrite /1.html /2.html break;
    rewrite /2.html /3.html ;
    }
    location /2.html {
      rewrite /2.html /3.html ;
    }
    location /3.html {
      rewrite /3.html /a.html ;
    }
 }
 [root@web01 /etc/nginx/conf.d]# curl -H Host:flag.cn 10.0.0.7/1.html
 2.html url
  
 4. 在rewrite /1.html /2.html的时候加上标记last标记.
 注意这一步修改下配置文件,创建新的页面b.html.
 [root@web01 /etc/nginx/conf.d]# cat
 flag.cn.conf
 server {
    listen 80;
    server_name flag.cn;
    root /app/code/flag;
    error_log /var/log/nginx/flag-error.log notice;
    rewrite_log on; #需要错误日志debug  notice
    location / {
    rewrite /1.html /2.html last;
    rewrite /2.html /3.html ;
    }
    location /2.html {
      rewrite /2.html /b.html ;
    }
    location /3.html {
      rewrite /3.html /a.html ;
    }
 }
 [root@web01 /etc/nginx/conf.d]# curl -H Host:flag.cn 10.0.0.7/1.html
 b.html url
  

总结

通过本章技术教程,我们掌握了Nginx的重定向。

重定向(Redirect)指通过各种方法将各种网络请求重新定个方向转到其它位置(如:网页重定向、域名的重定向、路由选择的变化也是对数据报文经由路径的一种重定向)。

URL重写是指通过配置conf文件,以让网站的URL中达到某种状态时则定向/跳转到某个规则,比如常见的伪静态、301重定向、浏览器定向等。当客户端浏览某个网址时,将其访问导向到另一个网址的技术。

其主要场景有如下两个:

  • 将一串很长的网址,转成较短的网址,从而实现便于传播、易于记忆。
  • 调整或更换Web服务器,网址(域名)又必须要变更(如访问目录、访问扩展名HTML变为PHP、访问域名),为了能使旧的访问依旧生效,从而实现自动重定向到新的网站。
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。