什么是rewrite?

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

Rewrite使用场景

1、地址跳转,用户访问www.drz.com这个URL是,将其定向至一个新的域名mobile.drz.com
2、协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式
3、伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动态URL地址对外暴露过多的参数,提升更高的安全性。
4、搜索引擎,SEO优化依赖于url路径,好记的url便于智齿搜索引擎录入

配置语法

句法:Syntax: rewrite regex replacement [flag]
默认:Default: --
语境:Context: server,location,if

Rewrite标记Flag rewrite指令根据表达式来重定向URL,或者修改字符串,可以应用于server,location,if环境下,每行rewrite指令最后跟一个flag标记,支持的flag标记有如下表格所示:

flag 作用
last 本条规则匹配完成后,停止匹配,不再匹配后面的规则
break 本条规则匹配完成后,停止匹配,不再匹配后面的规则
redirect 返回302临时重定向,地址栏会显示跳转后的地址
permanent 返回301永久重定向,地址栏会显示跳转后的地址
# 不怎么使用,比较适合开发使用
last
break
# 经常使用
redirect 302临时重定向,地址栏会显示跳转后的地址
permanent 301永久重定向,地址栏会显示跳转后的地址

语法案例

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

        location ~ ^/break {
                rewrite ^/break /test/ break;
        }
        location ~ ^/last {
                rewrite ^/last /test/ last;
        }
        location /test/ {
                default_type application/json;
                return 200 "ok";
        }
}

#重启nginx服务
[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]# nginx -s reload

image-20230512091144094

image-20230512091148830

last与break区别

break 只要匹配到规则,则会去本地配置路径的目录中寻找请求的文件; 而last只要匹配到规则,会对其所在的server(...)标签重新发起请求。

#break请求:
1、请求rewrite.drz.com/break
2、首先:会去查找本地的/code/test/index.html;
3、如果找到了,则返回/code/test/index.html的内容;
4、如果没找到该目录则报错404,如果找到该目录没找到对应的文件则403

#last请求:
1、请求rewrite.drz.com/last
2、首先:会去查找本地的/code/test/index.html;
3、如果找到了,则返回/code/test/index.html的内容;
4、如果没找到,会对当前server重新的发起一次请求,rewrite.drz.com/test/
5、如果有location匹配上,则直接返回该location的内容。
4、如果也没有location匹配,再返回404;

所以,在访问/break和/last请求时,虽然对应的请求目录/test都是不存在的,理论上都应该返回404,但是实际上请求/last的时候,是会有后面location所匹配到的结果返回的,原因在于此。

Rewrite规则实践

在写rewrite规则之前,我们需要开启rewrite日志对规则的匹配进行调试。

[root@web01 code]# vim /etc/nginx/nginx.conf
/var/log/nginx/error.log notice;

http{
    rewrite_log on;
}

#在http模块下加入rewrite_log on;即可

案列一

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

#http://www.drz.com/abc/1.html  ==>  http://www.drz.com/ccc/bbb/2.html
 #1.准备真实访问路径
[root@web03 ~]# mkdir /ccc/bbb -p
[root@web03 ~]# echo "ccc_bbb_2" > /ccc/bbb/2.html
#2.Nginx跳转配置
[root@web03 ~]# cd /etc/nginx/conf.d/
[root@web03 conf.d]# cat ccbb.conf 
server {
        listen 80;

        location / {
                root /;
                index index.html;
        }
        location /abc {
                rewrite (.*) /ccc/bbb/2.html redirect;
                #return 302 /ccc/bbb/2.html;
        }
}

#3.重启Nginx服务
[root@web03 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@web03 conf.d]# nginx -s reload
#浏览器访问
jiujiu.com/abc/1.html

案例二

用户访问/2018/ccc/bbb/2.html实际上真实访问的是/2014/ccc/bbb/2.html

##http://www.drz.com/2018/ccc/2.html  ==>  http://www.drz.com/2014/ccc/bbb/2.html
 #1.准备真是的访问路径
[root@web03 conf.c]# mkdir /code/2014/ccc/bbb -p 
[root@web03 conf.c]# echo "2014_ccc_bbb_2" > /code/2014/ccc/bbb/2.html
 #2.Nginx跳转配置
[root@web03 conf.d]# cat ccbb.conf 
server {
        listen 80;

        location / {
                root /;
                index index.html;
        }
        location /2018 {
                rewrite ^/2018/(.*)$ /2014/$1 redirect;
        }
}

#3.重启nginx服务
[root@web03 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@web03 conf.d]# nginx -s reload
#浏览器访问
jiujiu.com/2018/ccc/bbb/2.html

案例三

用户访问/test实际上真实访问的是https://blog.driverzeng.com

#1.Nginx跳转配置
[root@web03 conf.d]# cat test.conf 
server {
        listen 80;

        location /test {
                rewrite (.*) https://blog.driverzeng.com redirect;
        }
}

#2.重启nginx服务
[root@web03 conf.d]# nginx -s reload
#浏览器访问
jiujiu.com/test

案例四

用户访问couese-11-22-33.html实际上真实访问的是/course/11/22/33/course_33.html

#http://jiujiu.com/couese-11-22-33.html  ==>  http://jiujiu.com/course/11/22/33/course_33.html
#1.准备真是的访问路径
[root@web03 ~]# mkdir /course/11/22/33 -p
[root@web03 ~]# echo "curl docs.etiantian.org" > /course/11/22/33/course_33.html

#2.Nginx跳转配置
[root@web03 conf.d]# cat test.conf 
server {
        listen 80;
        root /;
        index index.html;
        location /couese-11-22-33.html{
                #灵活配法
                rewrite ^/course-(.*)-(.*)-(.*).html$ /course/$1/$2/$3/course_$3.html redirect;
                #固定配法
                #rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;
        }
}

#3.重启nginx服务
[root@web03 conf.d]# nginx -s reload

案例五

将http请求跳转发到https

#Nginx跳转配置
server {
        listen 80;
        server_name jiujiu.com;
        rewrite ^(.*) https://$server_name$1 redirect;
        #return 302 https://$server_name$request_uri;
}       

server {
        listen 443;
        server_name jiujiu.com;
        ssl on;
}