13.Nginx之rewrite重写

1. Rewrite基本概述

1.1 什么是Rewrite

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

1.2 Rewrite使用场景

1、地址跳转,用户访问www.oldboyedu.com这个URL是,将其定向至一个新的域名sz.oldboy.com

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

3、伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动态URL地址对外暴露过多的参数,提升更高的安全性。

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

1.3 Rewrite配置示例

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

#用于切换维护页面场景
#rewrite ^(.*)$ /page/maintain.html break;

2. Rewrite标记Flag

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

flag
last 本条规则匹配完成后,停止匹配,不再匹配后面的规则
break 本条规则匹配完成后,停止匹配,不再匹配后面的规则
redirect 返回302临时重定向,地址栏会显示跳转后的地址
permanent 返回301永久重定向,地址栏会显示跳转后的地址

2.1 last与break区别对比示例

[root@web01 conf.d]# cat rewrite.conf 
server {
        listen 80;
        server_name rewrite.oldboy.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

2.1.1 使用浏览器访问break结果

-c

2.1.2 使用浏览器访问last结果

-c

2.1.3 last与break区别

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

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

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

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

2.2 redirect与permanent区别对比示例

2.2.1 网页跳转配置为redirect

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

        location /test {
                rewrite ^(.*)$  https://www.baidu.com redirect;
        }
}

2.2.2 通过浏览器访问,redirect会进行302跳转

-c

2.2.3 关闭nginx,在尝试访问网站测试redirect

-c

2.2.4 网页跳转配置为permanent

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

        location /test {
                rewrite ^(.*)$  https://www.baidu.com permanent;
        }
}

2.2.5 通过浏览器访问,permanent会进行301跳转

-c

2.2.6 关闭nginx,在尝试访问网站测试permanent,仍然可以跳转访问

-c

2.2.7 redirect与permanent区别(实现https)

redirect: 每次请求都会询问服务器,如果当服务器不可用时,则会跳转失败。

permanent: 第一次请求会询问,浏览器会记录跳转的地址,第二次则不再询问服务器,直接通过浏览器缓存的地址跳转。

3. Rewrite规则实践

字符 描述
\ 将后面接着的字符标记为一个特殊字符或者一个原义字符或一个向后引用
^ 匹配输入字符串的起始位置
$ 匹配输入字符串的结束位置
* 匹配前面的字符零次或者多次
+ 匹配前面字符串一次或者多次
? 匹配前面字符串的零次或者一次
. 匹配除“\n”之外的所有单个字符

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

#http://....../abc/1.html  ==>  http://....../ccc/bbb/2.html

#1.准备真实访问路径
[root@web03 ~]# mkdir /code/ccc/bbb -p
[root@web03 ~]# echo "ccc_bbb_2" > /code/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 /code;
                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

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

##http://....../2018/ccc/2.html  ==>  http://....../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 /code;
                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

3.3 例3:用户访问/test实际上真实访问的是http://39.104.205.72

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

        location /test {
                rewrite (.*) http://39.104.205.72 redirect;
        }
}

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

3.4 例4:用户访问course-11-22-33.html实际上真实访问的是/course/11/22/33/course_33.html

#http://....../couese-11-22-33.html  ==>  http://....../course/11/22/33/course_33.html

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

#2.Nginx跳转配置
[root@web03 conf.d]# cat test.conf 
server {
        listen 80;
        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]# echo 'course-22-33-44.html' > /code/course/22/33/44/course_44.html
[root@web01 conf.d]# echo 'course-33-44-55.html' > /code/course/33/44/55/course_55.html

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

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

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

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

4. Rewrite场景示例,部署discuz

#Discuz! 论坛(BBS),是一个采用 PHP 和 MySQL 等其他多种数据库构建的性能优异、功能全面、安全稳定的社区论坛平台

#创建站点目录部署代码
[root@web01 ~]# mkdir /code/discuz
[root@web01 ~]# rz Discuz_X3.3_SC_GBK.zip
[root@web01 ~]# unzip Discuz_X3.3_SC_GBK.zip -d /code/discuz/

#配置discuz论坛的nginx配置
[root@web01 conf.d]# cat discuz.conf
server {
        listen 80;
        server_name discuz.haoda.com;

        location / {
                root /code/discuz/upload;
                index index.php index.html;
        }

        location ~ \.php$ {
                root /code/discuz/upload;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

#授权站点目录
[root@web01 discuz]# chown www.www -R /code/

#创建数据库
MariaDB [(none)]> create database discuz;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all on discuz.* to discuz@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

-c-c-c-c-c -c -c -c-c

把上面的rewrite规则都加到nginx配置后访问

-c


5. Rewrite规则补充

5.1 Rewrite优先级

1、先执行server块的rewrite

2、其次执行location匹配规则

3、追后执行location中的rewrite

5.2 Rewrite在匹配过程中会用到一些Nginx全局变量

$server_name 当前用户请求的域名
server {
        listen 80;
        server_name test.oldboy.com;
        rewrite ^(.*)$ https://$server_name$1;
}
$request_filename 请求的文件路径名(带网站的主目录/code/images/test.jpg)
$request_uri 当前请求的文件路径(不带网站的主目录/inages/test.jpg)
#大多数用于http协议转gttps协议
server {
        listen 80;
        server_name php.haoda.com;
        return 302 https://$server_name$request_uri;
}
$scheme 用的协议,比如http或者https

5.3 如何更加规范的书写Rewrite规则

server {
        listen 80;
        server_name www.haoda.com haoda.com;
        if ($http_host = haoda.com){
            rewrite (.*) http://www.haoda.com$1;
        }
}

#推荐书写格式
server {
        listen 80;
        server_name haoda.com;
        rewrite ^ http://www.haoda.com$request_uri;
}
server {
        listen 80;
        server_name www.haoda.com;
}

猜你喜欢

转载自www.cnblogs.com/chenmiao531759321/p/11436183.html