一、Rewrite跳转场景

1.1、URL看起来更规范、合理
1.2、企业会将动态URL地址伪装成静态地址提供服务
1.3、网站换新域名后,让旧的访问跳转到新的域名上
1.4、服务端某些业务调整

二、Rewrite跳转实现

后端redirect和nginx nginx redirect配置_linux

三、Rewrite实际场景

3.1、Nginx跳转需求的实现方式
3.1.1、使用rewrite进行匹配跳转
3.1.2、使用if匹配全局变量后跳转
3.1.3、使用location匹配再跳转

3.2、rewrite放在server{},if{},location{}段中
3.2.1、location只对域名后边的除去传递参数外的字符串起作用

3.3、对域名或参数字符串
3.3.1、使用if全局变量匹配
3.3.2、使用proxy_pass反向代理

四、Nginx正则表达式

常用的正则表达式元字符

字符

说明

^

匹配输入字符串的起始位置

$

匹配输入字符串的结束位置

*

匹配前面的字符零次或多次

+

匹配前面的字符一次或多次

?

匹配前面的字符零次或一次

.

匹配除”\n”之外的任何单个字符

\

将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用

\d

匹配纯数字

{n}

重复n次

{n,}

重复n次或更多次

[c]

匹配单个字符c

[a-z]

匹配a-z小写字母的任意一个

[a-zA-Z]

匹配a-z小写字母或A-Z大写字母的任意一个

五、Rewrite命令

5.1、Rewrite命令语法

rewrite <regex> <replacement> [flag];
<regex>:正则表达式        
<replacement>:跳转后的内容     
[flag]rewrite支持的flag标记

5.2、flag标记说明

标记

说明

last

相当于Apache的[L]标记,表示完成rewrite

break

本条规则匹配完成即终止,不再匹配后面的任何规则

redirect

返回302临时重定向,浏览器地址会显示跳转后的URL地址,爬虫不会更新url

permanent

返回301永久重定向,浏览器地址会显示跳转后的URL地址,爬虫更新url

1)last:url重写后,马上发起一个新请求,再次进入server块,重试location匹配,超过10次匹配不到报500错误,地址栏不变

2)break:url重写后,直接使用当前资源,不再使用location余下的语句,完成本次请求,地址栏不变

总结:last和break再重定向后,地址栏都不会发生变化,这是他们的相同点,不同点在于last会写在server和if中,break是写在location中,last不会终止重写后的url匹配,break会终止重写后的url匹配

六、location分类

6.1、分类

location = patt{} [精准匹配]
location patt {} [一般匹配]
location ~ patt {} [正则匹配]

6.2、正则匹配的常用表达式

标记

说明

~

执行一个正则匹配,区分大小写

~*

执行一个正则匹配,不区分大小写

!~

执行一个正则匹配,区分大小写不匹配

!~*

执行一个正则匹配,不区分大小写不匹配

^~

普通字符匹配,使用前缀匹配,如果匹配成功,则不再匹配其他location

=

普通字符精确匹配,也就是完全匹配

@

定义一个命名的location,使用再内部定向时

6.3、location优先级
6.3.1、相同类型的表达式,字符串长的会优先匹配
6.3.2、按优先级排列

= 类型
^~ 类型表达式
正则表达式(~和~*)类型
常规字符串匹配类型,按前缀匹配
通用匹配(/),如果没有其他匹配,任何请求都会匹配到

6.3.3、location优先级的示例1

location = / {                     ①精确匹配/,主机名后面不能带任何字符串
[configuration A]
}
location ~ /documents/abc {        ②匹配任何以/documents/abc开头的地址,当后面的正则表达式没有匹配到时,才会起作用
[configuration B]
}
location /documents/ {             ③匹配任何以/documents/开头的地址,当后面的正则表达式没有匹配到时,才起作用
[configuration C]
}
location / {                       ④所有的地址都以/开头,这条规则将匹配到所有请求,但正则表达式和最长字符会优先匹配
[configuration D]
}
location ^~ /images/ {             ①以/images/开头的地址,匹配符合后,停止往下匹配
[configuration E]
}
location ~* \.(gif|jpg|jpeg)$ {    ②匹配所有以gif,jpg或jpeg结尾的请求,因为^~的优先级最高
[configuration F]
}
location ~ /images/abc {           ③以/images/abc开头的,优先级次之
[configuration G]
}
location /images/abc/1.html {      ④如果和正则~ /images/abc/1.html相比,正则优先级最高
[configuration H]
}
location /images/abc {             ⑤最长字符匹配到/images/abc,优先级最低
[configuration I]
}

6.4、比较rewrite和location
6.4.1、相同点:都能实现跳转
6.4.2、不同点:
①、rewrite是在同一域名内更改获取资源的路径
②、location是对一类路径控制访问或反向代理,还可以proxy_pass到其他机器
6.4.3、rewrite会写在location里,执行顺序
①、执行server块里面的rewrite指令
②、执行location匹配
③、执行选定的location中的rewrite指令

6.5、location优先级规则
6.5.1、匹配某个具体文件

(location = 完整路径)>(location ^~ 完整路径)>(location ~* 完整路径)=(location ~ 完整路径)>(location /)

6.5.2、用目录做匹配访问某个文件

(location = 目录)>(location ^~ 目录)>(location ~* 目录)=(location ~ 目录)>(location /)

七、实例说明

7.1、基于域名的跳转
公司旧域名www.aaa.com,因业务需求有变更,需要使用新域名www.bbb.com代替
不能废除旧域名
从旧域名跳转到新域名,且保持其参数不变
7.1.1、修改新站点配置文件(基于LNMP架构),如果没有PHP,可以直接创建index.html文件进行访问

vi /usr/local/nginx/conf/nginx.conf
server {
        listen       80;
        server_name  www.aaa.com;

        charset utf-8;

        access_log  logs/host.access.log  main;
        if ($host = 'www.aaa.com')                      #添加rewrite功能
         {
            rewrite ^/(.*)$ http://www.bbb.com/$1 permanent;
        }
省略部分内容
}

7.1.2、在配置文件末尾添加新域名www.bbb.com的站点位置(保证添加的内容在http括号内)

server {
        listen       80;
        server_name  www.bbb.com;

        charset utf-8;

        access_log  /var/log/www.bbb.com.access.log  main;
        location / {
            root   /usr/local/share/html;
            index  index.html index.htm;
        }
        location ~ \.php$ {
            root           /usr/local/share/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }

7.1.3、重启nginx

nginx -t       #查看配置文件语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok        #输出语法内容    
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful      #输出文件配置是否正常
systemctl restart nginx     #重启

7.1.4、创建新站点的网页

mkdir -p /usr/local/share/html/php
vi /usr/local/share/html/php/index.php
<?php
phpinfo();
?>

7.1.5、客户端测试

后端redirect和nginx nginx redirect配置_html_02


后端redirect和nginx nginx redirect配置_linux_03


后端redirect和nginx nginx redirect配置_php_04


注:只要是www.bbb.com可以访问的网页都是可以跳转的

7.2、基于客户端IP访问跳转
今天公司业务版本上线,所有IP访问任何内容都显示一个固定维护页面,只有公司IP访问正常

7.2.1、修改默认站点配置文件

vi /usr/local/nginx/conf/nginx.conf
server {
        listen       80;
        server_name  www.aaa.com;
        set $rewrite true;
        if ($remote_addr = "20.0.0.10"){
            set $rewrite false;
        }
        if ($rewrite = true){
            rewrite (.+) /maintenance.html;
        }
        location = /maintenance.html {
            root /usr/local/nginx/html;
        }
省略部分内容
}

7.2.2、将上个项目里的末尾添加的内容删除,修改重定向的网页

vi /usr/local/nginx/html/maintenance.html
<html><body><h1>website is maintaing,please visit later</h1></body></html>

7.2.3、重启并测试

nginx -t
systemctl restart nginx

后端redirect和nginx nginx redirect配置_php_05


后端redirect和nginx nginx redirect配置_nginx_06


7.3、基于旧、新域名跳转并加目录

将域名http://www.aaa.com下面的发帖都跳转到http://www.bbb.com/bbs,且域名跳转后保持参数不变

7.3.1、修改默认站点配置文件

vi /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  www.aaa.com;

        charset utf-8;

        access_log  logs/host.access.log  main;
        location /post
         {
            rewrite (.+) http://www.bbb.com/bbs$1 permanent;
        }
        location / {
            root   html;
            index  index.html index.htm;
        }
省略部分内容
}
下面的内容在末尾添加(保证添加的内容在http括号内)
server {
        listen       80;
        server_name  www.bbb.com;

        charset utf-8;

        access_log  logs/host.access.log  main;
        location /
         {
             root /usr/local/share/html;
             index   index.html index.htm;
        }
}

7.3.2、创建bbs目录

cd /usr/local/share/html/
mkdir bbs
mv index.html bbs/
cd bbs/
mkdir post
mv index.html post/

7.3.3、重启并测试

nginx -t
systemctl restart nginx

后端redirect和nginx nginx redirect配置_nginx_07


后端redirect和nginx nginx redirect配置_nginx_08


7.4、基于参数匹配的跳转

7.4.1、修改默认找点配置文件

vi /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  www.aaa.com;
        if ($request_uri ~ ^/100-(100|200)-(\d+).html$)
        {
            rewrite (.+) http://www.aaa.com permanent;
        }

        charset utf-8;

        access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
省略部分内容
}

7.4.2、重启并测试

nginx -t
systemctl restart nginx

后端redirect和nginx nginx redirect配置_nginx_09


后端redirect和nginx nginx redirect配置_linux_10


7.5、基于目录下所有php文件跳转

7.5.1、修改默认站点配置文件

vi /usr/local/nginx/conf/nginx.conf
server {
        listen       80;
        server_name  www.aaa.com;
        location ~* /upload/.*\.php$
        {
            rewrite (.+) http://www.aaa.com permanent;
        }

        charset utf-8;

        access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
省略部分内容
}

7.5.2、重启并测试

nginx -t
systemctl restart nginx

后端redirect和nginx nginx redirect配置_linux_11


后端redirect和nginx nginx redirect配置_nginx_12