39-Nginx-rewrite跳转

39-Nginx-rewrite跳转

Rewrite基本概述
什么是rewrite?
	rewrite主要实现rul地址重写,以及重定向,就是把传入web的请求重定向到其他url的过程。
rewirte的使用场景
	1.地址跳转,用户访问www.lzy.com这个url时,将其定向到一个新的域名mobile.lzy.com
	2.协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式
	3.伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时减少动态URL地址对外暴露过多的参数,提升更高的安全性
	4.搜索引擎,SEO优化依赖于url路径,好记的url便于支持搜索引擎录入
Rewrite配置示例
Syntax:rewrite regex replacement [flag]
Default:-
Context:server,location,if
#用于切换维护页面场景
#rewrite ^(.*)$ /page/maintain.html break;
Rewrite标记flag
rewrite 指令根据表达式来重定向url,或者修改字符串,可以应用于server location if环境下,每行rewrite后跟一个flag标记,支持的flag标记有如下所示:
flag作用
last本条规则匹配完成后,停止匹配,不再匹配后面的规则
break本条规则匹配完成后,停止匹配,不再匹配后面的规则
redirect返回302临时重定向,地址栏会显示跳转后的地址
permanent返回301永久重定向,地址栏会显示跳转后的地址
Rewrite跳转标记
[root@web01 conf.d]# cat rewrite.conf 
server {
        listen 80;
        server_name test.oldboy.com;
        root /code/test/;

        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;
        }
    }
[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
[root@web01 conf.d]# 
[root@web01 conf.d]# echo 2.html > /code/test/2.html
[root@web01 conf.d]# echo 3.html > /code/test/3.html
[root@web01 conf.d]# echo a.html > /code/test/a.html
[root@web01 conf.d]# echo b.html > /code/test/b.html

访问测试:
http://test.oldboy.com/1.html		返回b.html
http://test.oldboy.com/2.html		返回a.html

[root@web01 conf.d]# cat rewrite.conf 
server {
        listen 80;
        server_name test.oldboy.com;
        root /code/test/;

        location / {
        rewrite /1.html /2.html last;		#last停止向后匹配。到2.html终止。重新对2.html发起一个请求
        rewrite /2.html /3.html; 
        }

        location /2.html {
        rewrite /2.html /a.html;
        }

        location /3.html {
        rewrite /3.html /b.html;
        }
    }
    
 访问http://test.oldboy.com/1.html		返回a.html 
[root@web01 conf.d]# cat rewrite.conf 
server {
        listen 80;
        server_name test.oldboy.com;
        root /code/test/;

        location / {
        rewrite /1.html /2.html break;		#break到此为止,将2.html的内容返回。不再重新对2.html进行请求
        rewrite /2.html /3.html; 
        }

        location /2.html {
        rewrite /2.html /a.html;
        }

        location /3.html {
        rewrite /3.html /b.html;
        }
    }

访问:http://test.oldboy.com/1.html		返回2.html
#302 临时跳转:每次都要请求源站,然后源站返回新的站点给用户,如果源站挂了则无法访问。与redirect功能相同

#301 永久跳转:只访问一次源站,后面不再访问源站直接去找新的站点。与permanent功能相同。

[root@web01 conf.d]# cat rewrite.conf 
server {
        listen 80;
        server_name test.oldboy.com;
        root /code;

        location /test {
                rewrite ^(.*)$  http://www.baidu.com redirect;
                #rewrite ^(.*)$  http://www.baidu.com permanent;
                #return 301 http://www.baidu.com;
                #return 302 http://www.baidu.com;
        }
}

访问www.oldboy.com/test
临时跳转到www.baidu.com  。redirect 每次都要请求原站,然后跳转新的站点。permanent只请求一次源站。
Rewrite资源跳转
[root@web01 conf.d]# cat ccbb.conf 
server {
        listen 80;
	server_name test.oldboy.com;

        location / {
                root /code;
                index index.html;
        }
        location /abc {
                rewrite (.*) /ccc/bbb/2.html redirect;
                #return 302 /ccc/bbb/2.html;
        }
}
[root@web01 conf.d]# mkdir -p /code/ccc/bbb
[root@web01 conf.d]# echo /code/ccc/bbb/2.html > /code/ccc/bbb/2.html

访问:test.oldboy.com/abcxxx

通过后向引用,调用用户输入的路径
root@web01 conf.d]# vim bbcc.conf
server {
        listen 80;
        server_name test.oldboy.com;
        location / {
                root /code;
        index index.html;
        }
        location /2018 {
        rewrite ^/2018/(.*)$ /2014/$1 redirect;
        }

访问:
https://test.oldboy.com/2018/aaa/index.html
访问:http://test.oldboy.com/course/11/22/33/course_33.html
[root@web01 conf.d]# cat rewrite.conf 
server {
	listen 80;
	server_name test.oldboy.com;
	root /code;
	index index.html;
	location / {
		#灵活配法
		rewrite ^/course-(.*)-(.*)-(.*).html$ /course/$1/$2/$3/course_$3.html redirect;
		#固定配法
		#rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;
	}
}
错误页跳转
[root@web01 conf.d]# vim rewrite.conf 

server {
        listen 80;
        server_name test.oldboy.com;
        root /code;
        index index.html;
        location / {
                #灵活配法
                rewrite ^/course-(.*)-(.*)-(.*).html$ /course/$1/$2/$3/course_$3.html redirect;
                #固定配法
                #rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;
        }
        error_page 403 404 500 501 502 @error_test;
        location @error_test {
        rewrite ^(.*)$ /404.html break;
}

#客户端IP地址为10.0.0.1访问test.oldboy.com时,自动在请求行后面加上参数&showoffline=1
[root@web01 conf.d]# vim rewrite.conf 
server {
        listen 80;
        server_name test.oldboy.com;
        # $args为Nginx内置变量请求行的参数
        set $args "&showoffline=1";
        location / {
                root /code;
                index index.html;
        }
        if ($remote_addr = 10.0.0.1 ){
        rewrite (.*) http://test.oldboy.com$1;
}
}

#通过变量指定返回给用户的页面
server {
    listen 80;
    server_name test.oldboy.com;
    root /code/test;
    charset utf-8,gbk;

    location / {
        index index.html;
        set $ip 0;       # 设置变量为0
        if ($remote_addr = "10.0.0.2"){
            set $ip 1;   # 如果来源IP为0.1则设置为1
        }
        if ($ip = 0){    # 判断如果变量为0 则跳转维护页面
            rewrite ^(.*)$ /wh.html break;
        }
    }


}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

atomLg

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

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

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

打赏作者

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

抵扣说明:

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

余额充值