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

文章详细介绍了Nginx的Rewrite功能,包括URL重写、重定向的不同场景,如地址跳转、协议跳转、伪静态等。同时,讨论了last、break、redirect和permanent等flag标记的用途,并通过多个配置示例展示了如何使用这些标记。此外,还提供了几个实际的Rewrite使用案例,如URL路径转换、http到https的跳转、错误页处理和IP限制访问策略。

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 

3cd3972ff2b84f97872904afffc66dac.png

添加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

 ea48e88fe086474ebf5859e6d3ecb5ab.png

添加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 

07daaa86552045a488e77d3373015f10.png

 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

98e43e7824fc4579abcb157d2b7b9e77.png

 

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

e5c42ae2e4b540848bccd32226f56b89.png

案例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

3cca51b6d7fc4f7ea7de1106b1fae166.png

案例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

74147f7a5eee429fb8406f554fade293.png案例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

0fe45b1444514da0abc52b6f391f75fb.png

 案例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

da3feb39736b4e61b78bda8062523e2b.png

案例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

689e27a900714228ab57e7a4f80fb6b4.png

案例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

1962d6f8d5fe4292a8b868184db37f0e.png 3d80eaf1d7b04a96bccf77106ad62a75.png

案例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

03acf068f64642d38a2bc2f2e6285735.png

 

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年运维经验,持续分享运维干货,感谢大家的阅读和关注!

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是koten

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值