10. Nginx Rewrite(重定向)

Nginx Rewrite(重定向)

1.什么是rewrite

Rewrite及URL重写,主要是实现地址重写,以及重定向,就是把输入Web的请求重定向到其他URL的过程

2.Rewrite使用场景

  • URL地址跳转,列入用户访问old.com将其跳转到oldboy.com,或者当用户通过http的方式访问old.com时,将其跳转至https的方式访问oldboy.com.
  • URL伪静态,将动态页面显示为静态页面的一种技术,便于搜索引擎的录入,同时减少动态URL地址对外暴露过多的参数,提升更高的安全性。
  • 搜索引擎SEO优化依赖与URL路径,以便支持搜索引擎录入。
  • 可以调整用户浏览的URL,看起来更规范,合乎开发及产品人员的要求。

3.Rewrite配置语法

示例

Syntax:rewrite regex replacement [flag];

Default:–

Context:server,location,if

4.常用正则表达式

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

5.Rewrite 标记Flag

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

标记符号 说明
last 本条规则匹配完成后继续向下匹配新的location URI规则
break 本条规则匹配完成后终止,不在匹配任何规则
redirect 返回302临时重定向
permanent 返回301永久重定向

last与break对比总结

last

  1. last进行Rewrite匹配成功后,停止当前这个请求,并根据Rewrite匹配的规则重新向Server发起一个请求。
  2. 新请求的内容是域名+URL,对应的地址为www.lodboy.com/2017_old

break

  1. break进行Rewrite匹配成功后,不会像last重新发起请求,首先找到站目录下/clod/2017_old/默认返回页面是否存在,如不存在则404,如存在继续往下匹配。
  2. 根据Rewrite匹配的规则,跳转至www.oldboy.com/2017_old/的URL地址,匹配成功后则不继续匹配。

6.引用Nginx的全局变量

列:http://localhost:88/test1/test2/test.php

  • $host:localhost
  • $server_port: 88
  • $request_uri:/test1/test2/test.php
  • $document_uri: /test1/test2/test.php
  • $document_root: /var/www/html
  • $request_filename: /var/www/html/test1/test2/test.php

7.Rewrite匹配优先级

  • 1.执行server块的rewrite指令
  • 2.执行location匹配
  • 3.执行选定的location中的rewrite

8.开启Nginx的Rewrite 日志功能

vim /etc/nginx/nginx.conf

#1.设置nginx的错误日志级别为notice

error_log /var/log/nginx/error.log notice;

#2.在http模块层,增加一行rewrite on开启日志功能

http {
	rewrite_log on;
	server {
 .......
	}	
}

9.案例

案例一:

页面跳转到www.baidu.com

 server  {
    
    
        listen          80;
            server_name     yan.wordpress.com;
            charset     utf-8;
            root /html/wordpress;
            index index.php index.html;
        location / {
    
    
                rewrite ^/ https://www.baidu.com;	//正则匹配 到www.baidu.com
        }
        location ~ \.php$ {
    
    
            root /html/wordpress;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass 127.0.0.1:9000;
            include fastcgi_params;
            fastcgi_index index.php;
        }
}

案例二:

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

http://rewrite.odboy.com/abc/1.html => http://rewrite.odboy.com/ccc/bbb/2.html

[root@web02 html]# mkdir -p /ccc/bbb
[root@web02 html]# echo "bbbbbcccccc" > /ccc/bbb/2.html

[root@web02 html]# cat /etc/nginx/conf.d/rewrite.conf 
server {
    
    
	listen 80;
	server_name rewrite.oldboy.com;
	root /html;
	index index.html;
	location ~* /abc/1.html {
    
    
		rewrite /abc/1.html /ccc/bbb/2.html;
	}
}
配置优化一:
[root@web02 html]# cat /etc/nginx/conf.d/rewrite.conf 
server {
    
    
	listen 80;
	server_name rewrite.oldboy.com;
	root /html;
	index index.html;
	location ~* /abc/1.html {
    
    
		rewrite  .* /ccc/bbb/2.html;
	}
}

在这里插入图片描述

配置优化二:
[root@web02 html]# cat /etc/nginx/conf.d/rewrite.conf 

server {
    
    
        listen 80;
        server_name rewrite.oldboy.com;
        root /html;
        index index.html;
        location ~* /abc/ {
    
    
        		//这两行都是302跳转,不要一起配置。
                #rewrite .* /ccc/bbb/2.html redirect;  
                return 302 /ccc/bbb/2.html;
        }
}

在这里插入图片描述

案例三:

用户访问http://rewrite.oldboy.com/2018/ccc/bbb/2.html ==> http://rewrite.oldboy/2014/ccc/bbb/2.html

[root@web02 html]# mkdir 2014
[root@web02 html]# mv ccc 2014/
[root@web02 html]# chown -R www.www 2014/

[root@web02 html]# cat /etc/nginx/conf.d/rewrite.conf 
server {
    
    
        listen 80;
        server_name rewrite.oldboy.com;
        root /html;
        index index.html;
        location /2018 {
    
                    
			rewrite ^/2018/(.*)$ /2014/$1 redirect;   
        }       
}  

在这里插入图片描述

案例四:

用户访问/test目录下任何内容,实际上真实访问的是rewirte.oldboy.com

[root@web02 html]# cat /etc/nginx/conf.d/rewrite.conf 
server {
    
    
        listen 80;
        server_name rewrite.oldboy.com;
        index 2.html;
        root /html/ccc/bbb;
        location /test {
    
    
                rewrite (.*) http://rewrite.oldboy.com redirect;
        }

}

在这里插入图片描述

案例五:

#http://rewrite.oldboy.com/course-11-22-33-course_33.html ==> http://rewrite.oldboy.com/course/11/22/33/course_33.html

[root@web02 html]# mkdir -p 11/22/33
[root@web02 html]# echo "/11/22/23" > 11/22/33/course_33.html

[root@web02 html]# cat /etc/nginx/conf.d/rewrite.conf 

server {
    
    
        listen 80;
        server_name rewrite.oldboy.com;
        index 2.html;
        root /html;
        location / {
    
    
        		#灵活
                rewrite ^/(.*)-(.*)-(.*).html$ /$1/$2/$3/course_$3.html redirect;
                #固定
                rewrite ^/(.*)   /11/22/33/course_33.html redirect;
        }

}

在这里插入图片描述

案例六:

讲http请求,跳转至https

[root@web02 html]# cat /etc/nginx/conf.d/rewrite.conf 
server {
    
    
        listen 80;
        server_name rewrite.oldboy.com;
        rewrite ^(.*) https://$server_name$1 redirect;
        #return 302 https://$request_uri;
}       
server {
    
    
        listen 443;
        server_name rewrite.oldboy.com;
        ssl on
}   

猜你喜欢

转载自blog.csdn.net/weixin_43357497/article/details/113764186
今日推荐