day 42 nginx rewrite跳转

第1章 Nginx rewrite跳转

1.官方地址
https://nginx.org/en/docs/http/ngx_http_rewrite_module.html

2.应用场景
http跳转https
旧站点跳转到新站点
根据终端类型进行跳转到不同页面
根据不同地址跳使用不同的代码目录

3.ngx_http_rewrite_module模块介绍

break #中断配置
if #请求判断
set #设置变量
return #返回状态或URL
rewrite #对用户请求的URL或URI进行重写

第2章 if指令
1.官方文档:
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if

2.指令说明:
if用于条件判断,并且可以根据判断的结果执行不同的配置
if可以配置在server或location中。

3.指令语法:

if (匹配条件) { 
	执行的动作
}

4.实验配置

cat >/etc/nginx/conf.d/if.conf << 'EOF'
server {
    listen       80 default_server;
    server_name  www.oldzhang.com;
    root   /usr/share/nginx/html/www;
location / {
    #客户端类型完全匹配iphone
    if ($http_user_agent = iphone) {
       return 200 "agent is iphone";
                              }
    
    #客户端类型区分大小写匹配
    if ($http_user_agent ~ Android) {
       return 200 "agent is Android";
                       }
    
    #客户端类型不区分大小写匹配
    if ($http_user_agent ~* Chrome) {
       return 200 "agent is Chrome";
                }
    
    #如果不是GET则输出
    if ($request_method != GET){
       return 200 "method is not GET";
                 }
    
    #如果是IE浏览器,直接返回404
    if ($http_user_agent ~* IE){
       return 111 "IE is too low!";
               }
    
    #都没匹配上则执行下面语句
    return 200 "not match agent ==> $http_user_agent request ==> $request_method";
          }
    }
EOF

5.测试方法

默认访问

[root@web-7 ~]# curl 127.0.0.1
not match agent ==> curl/7.29.0 request ==> GET

测试精确匹配=

[root@web-7 ~]# curl -A "iphone" 127.0.0.1
agent is iphone

[root@web-7 ~]# curl -A "iphoneeeee" 127.0.0.1
not match agent ==> iphoneeeee request ==> GET

[root@web-7 ~]# curl -A "iphonE" 127.0.0.1
not match agent ==> iphonE request ==> GET

区分大小写匹配

[root@web-7 ~]# curl -A "Android" 127.0.0.1
agent is Android

[root@web-7 ~]# curl -A "Androiddddddd" 127.0.0.1
agent is Android

[root@web-7 ~]# curl -A "aAndroiddddddd" 127.0.0.1
agent is Android

[root@web-7 ~]# curl -A "123Axndroiddddddd" 127.0.0.1
not match agent ==> 123Axndroiddddddd request ==> GET

客户端类型不区分大小写匹配

[root@web-7 ~]# curl -A "Chrome" 127.0.0.1
agent is Chrome
[root@web-7 ~]# curl -A "chrome" 127.0.0.1
agent is Chrome

如果不是GET则输出

[root@web-7 ~]# curl -X "POST" 127.0.0.1
method is not GET

[root@web-7 ~]# curl -X "PUT" 127.0.0.1
method is not GET

如果是IE浏览器,直接返回404

[root@web-7 ~]# curl -A "ie" 127.0.0.1
IE is too low!
[root@web-7 ~]# curl -A "Ie" 127.0.0.1
IE is too low!

第3章 rewrite指令
1.官方文档
https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite

2.指令语法
rewrite regex replacement [flag];
rewrite 匹配规则 重写指令 [标志符];

rewrite指令可以作用于server, location, if

3.flag标识符说明
flag作用说明:
redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址,浏览器不会缓存DNS记录
permanent 返回301临时重定向,浏览器地址会显示跳转后的URL地址,浏览器会缓存DNS记录
last 本条规则匹配完成后,继续向下匹配新的location URI规则
break 本条规则匹配完成即终止,不再匹配后面的任何规则

使用场景区分:

  • 使用redirect和permanent参数,浏览器会显示跳转后的URL,服务端将改写后的URL返回给客户端,由客户端重新发起新的请求。
  • 使用last和break,浏览器显示的还是原来的URL,由服务器内部完成跳转。

last和break区别:

last标记在本条规则匹配完成后,对其所在的server标签重新发起修改后的URL请求,再次匹配location。
break标记则会在本条规则匹配完成后,终止匹配,不会在匹配后面的规则。

第4章 redirect和permanent实验
1.permanent 301 永久跳转 实验
访问: www.oldboy.com
跳转: blog.oldboy.com

2.配置文件:

cat > /etc/nginx/conf.d/www.conf << 'EOF'
server   {
    listen       80;
    server_name  www.oldboy.com;
    location / {
        rewrite / http://blog.oldboy.com permanent;
    }
}
EOF

cat > /etc/nginx/conf.d/blog.conf << 'EOF'
server   {
    listen       80;
    server_name  blog.oldboy.com;
    location / {
        root   /code/blog;
        index  index.html;
    }
}
EOF

mkdir /code/{www,blog} -p
echo www > /code/www/index.html
echo blog > /code/blog/index.html

nginx -t 
systemctl restart nginx

3.替换的坑

If a replacement string starts with “http://”, “https://”, or “$scheme”, the processing stops and the redirect is returned to a client.

默认是替换:

rewrite / blog.oldboy.com permanent;
www.oldboy.com/
www.oldboy.com/blog.oldboy.com

如果替换的内容以http://开头,则直接访问新的地址

rewrite / http://blog.oldboy.com permanent;
www.oldboy.com 
blog.oldboy.com

4.302跳转

cat > /etc/nginx/conf.d/www.conf << 'EOF'
server   {
    listen       80;
    server_name  www.oldboy.com;
    location / {
	    rewrite / http://blog.oldboy.com redirect;
        #rewrite / http://blog.oldboy.com permanent;
    }
}
EOF

5.模拟jd跳转

www.360buy.com --> 301 http://www.jd.com/ --> 302 https://www.jd.com/
www.oldboy.com --> 301 http://blog.oldboy.com --> https://blog.oldboy.com/

www的配置:

cat > /etc/nginx/conf.d/www.conf << 'EOF'
server   {
    listen       80;
    server_name  www.oldboy.com;
    location / {
        rewrite / http://blog.oldboy.com permanent;
    }
}
EOF

blog的配置:

cat > /etc/nginx/conf.d/blog.conf << 'EOF'
server   {
    listen       80;
    server_name  blog.oldboy.com;
    location / {
		rewrite / https://blog.oldboy.com redirect;
    }
}
EOF

第5章 last实验
1.配置文件

cat > /etc/nginx/conf.d/www.conf << 'EOF'
server   {
    listen       80;
    server_name  www.oldboy.com;
    location /CCC {
        return 200 "CCCC \n";
    } 
location /BBB {
    rewrite ^/BBB/(.*)$ /CCC/$1 last;
}

location /AAA {
    rewrite ^/AAA/(.*)$ /BBB/$1 last;
}
}
EOF
rewrite ^/AAA/(.*)$ /BBB/$1 last;
www.oldboy.com/AAA/123.txt
www.oldboy.com/BBB/123.txt

rewrite ^/BBB/(.*)$ /CCC/$1 last;
www.oldboy.com/BBB/123.txt
www.oldboy.com/CCC/123.txt

第6章 break
配置步骤:

server   {
    listen       80;
    server_name  www.oldboy.com;
    location /CCC {
        return 200 "CCCC \n";
    } 
location /BBB {
    rewrite ^/BBB/(.*)$ /CCC/$1 last;
}

location /AAA {
    rewrite ^/AAA/(.*)$ /BBB/$1 break;
    root /code/www;
    index index.html;
}
}

创建目录:

mkdir -p /code/www/AAA
mkdir -p /code/www/BBB
mkdir -p /code/www/CCC
echo AAA > /code/www/AAA/index.html
echo BBB > /code/www/BBB/index.html   
echo CCC > /code/www/CCC/index.html   
tree /code/www/

总结:
不同域名之间跳转:
redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址,浏览器不会缓存DNS记录
permanent 返回301临时重定向,浏览器地址会显示跳转后的URL地址,浏览器会缓存DNS记录

同一个域名内部跳转:
last 本条规则匹配完成后,继续向下匹配新的location URI规则
break 本条规则匹配完成即终止,不再匹配后面的任何规则

第7章 跳转实验
1.需求:
用户访问 www.oldboy.com/img/2021/05/10/linux.jpg
跳转到 img.oldboy.com/2021-05-10-linux.jpg

2.www的配置:

cat > /etc/nginx/conf.d/www.conf << 'EOF'
server   {
    listen       80;
    server_name  www.oldboy.com;
    location /img/ {
        rewrite ^/img/([0-9]+)/([0-9]+)/([0-9]+)/(.*).jpg$ http://img.oldboy.com/$1-$2-$3-$4.jpg permanent;
    }
}
EOF

3.blog的配置:

cat > /etc/nginx/conf.d/img.conf << 'EOF'
server   {
    listen       80;
    server_name  img.oldboy.com;
    location / {
      root /code/img;
      index index.html;
    }
}
EOF

总结:
location模块:
| 匹配符 | 匹配规则 | 优先级 |
| = | 精确匹配 | 1 |
| ^~ | 以某个字符串开头 | 2 |
| ~ | 区分大小写的正则匹配 | 3 |
| ~* | 不区分大小写的正则匹配 | 4 |
| / | 通用匹配,任何请求都会匹配到5 | 5 |

rewrite模块:
return直接返回
if判断
rewrite跳转
不同域名之间跳转:
redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址,浏览器不会缓存DNS记录
permanent 返回301临时重定向,浏览器地址会显示跳转后的URL地址,浏览器会缓存DNS记录

同一个域名内部跳转:
last 本条规则匹配完成后,继续向下匹配新的location URI规则
break 本条规则匹配完成即终止,不再匹配后面的任何规则

原文地址:https://www.cnblogs.com/zhaocheng690/p/15609722.html