飞道的博客

16@nginx之rewrite(重定向)

288人阅读  评论(0)

nginx之rewrite(重定向)

1、地址跳转

2、协议跳转

3、伪静态

4、搜索引擎

一、rewrite使用场景

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

二、rewrite语法

Syntax:	rewrite regex replacement [flag];    
Default:	—
Context:	server, location, if        #使用场景

server {
   
	rewrite 规则 路径或者内容 flag类型;
	rewrite (.*) http://www.baidu.com$1
}
1.规则:字符串,也可以使用正则匹配url;
2.路径或者内容:表示匹配到规则后要跳转的路径或内容,如果前面规则里面有正则,则可以使用$1等获取值
3.flag类型:last break redirect permanent

三、rewrite的flag类型

flag类型 作用
last 匹配完本条规则,停止匹配,重新请求server
break 匹配完本条规则,停止匹配,不再匹配后面的内容
redirect 302跳转,临时重定向
permanent 301跳转,永久重定向

1)last和 break之间的区别

[root@web01 conf.d]# vim rewrite.conf
server {
   
        listen 80;
        server_name rw.linux.com;
        root /code/rewrite;

        location ~ ^/break {
   
                rewrite ^/break /test/ break;
        }
        location ~ ^/last {
   
                rewrite ^/last /test/ last;
        }
        location /test/ {
   
                default_type application/json;
                return 200 "ok";
        }
}
break 只要匹配到规则,就回去本地路径目录中寻找请求的文件;
last  匹配到规则,跳转后没有内容,则带着跳转后的请求,重新的向server发起一次请求

break和last都可以继续执行后面的rewrite;
在location里面,如果配置break,则直接生效,停止后面匹配的location

break请求:
	1.请求rw.linux.com/break;
	2.首先,会去查找本地的/code/rewrite/test/index.html;
	3.如果找到了,则返回/code/rewrite/test/index.html内容;
	4.如果没有找到则返回404,找到目录却没有主页,则返回403;
	
last请求:
	1.请求rw.linux.com/last;
	2.首先,会去查找本地的/code/rewrite/test/index.html;
	3.如果找到了,则返回/code/rewrite/test/index.html内容;
	4.如果没找到,会带着新跳转的URI再向server发起一次请求,请求rw.linux.com/test;
	5.如果匹配到新的location,则返回该location匹配的内容;
	6.如果没有匹配到新的,则再返回404或403;
server {
   

        listen 80;
        server_name _;
        root /www/resources;
        location / {
   
                index index.html;
        }
        location ^~ /break {
   
                rewrite ^/break /android.html break;
        }
        location ^~ /last {
   
                rewrite ^/last /android.html last;
        }
        location ^~ /android {
   
                root /www/resources/android;
        }
}


break:匹配到之后,直接在本地请求的工作目录寻找匹配到的内容。
last :匹配到内容之后,会重新发起一次对server请求

共同点:break和last匹配到之后,不再向下匹配

2)redirect和permanent之间的区别

redirect是临时重定向,当nginx关闭时,随之报错。

permanent是永久重定向,当nginx关闭时,随之使用缓存。

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

        location /test {
   
                rewrite ^(.*)$ http://www.mumusir.com redirect;
                #rewrite ^(.*)$ http://www.mumusir.com permanent;
        }
}

redirect: 每次请求都会询问服务器,如果当服务器不可用时,则会跳转失败。
permanent: 第一次请求会询问,浏览器会记录跳转的地址,第二次则不再询问服务器,直接通过浏览器缓存的地址跳转。
[root@web01 conf.d]# cat flag.conf 
server {
   
	listen 80;
	server_name _;
	root /www/resources;
	location / {
   
		index index.html;
	}
	location ^~ /break {
   
		rewrite ^/break /android.html break;
	}
	location ^~ /last {
   
		rewrite ^/last /android.html last;
	}
	location ^~ /redirect {
   
		rewrite ^/redirect http://192.168.15.8 redirect;
	}
	location ^~ /permanent {
   
		rewrite ^/permanent http://192.168.15.8 permanent;
	}
	location ^~ /android {
   
		root /www/resources/android;
	}
}

四、rewrite规则匹配实例

1)、访问测试一

#用户访问`/abc/1.html`实际上真实访问的是`/ccc/bbb/2.html`
[root@web01 conf.d]# vim rw.conf 
server {
   
    listen 80;
    server_name rw.linux.com;
    root /code;

    location ~ /abc {
   
        rewrite ^(.*)$ /ccc/bbb/2.html redirect;
        #rewrite /abc/1.html /ccc/bbb/2.html redirect;
    }
}

#/abc/1.html   /ccc/bbb/1.html
    location ~ /abc {
   
        rewrite /abc/(.*)\.html /ccc/bbb/$1.html redirect;
    }

2)、访问测试二

#用户访问`/2018/ccc/2.html`实际上真实访问的是  `/2014/ccc/bbb/2.html`
[root@web01 conf.d]# mkdir /code/2014/ccc/bbb/ -p
[root@web01 conf.d]# echo "2014-ccc-bbb-222" > /code/2014/ccc/bbb/2.html
server {
   
    listen 80;
    server_name rw.linux.com;
    root /code;

    location ~ /2018 {
   
        rewrite /2018/ccc/2.html /2014/ccc/bbb/2.html redirect;
    }
}

#/2018/ccc/bbb/2.html  跳转 /2014/ccc/bbb/2.html
    location ~ /2018 {
   
        rewrite /2018/(.*) /2014/$1 redirect;
    }

3)、访问测试三

#用户访问`/test`实际上真实访问的是`www.baidu.com`

server {
   
    listen 80;
    server_name rw.linux.com;
    root /code;

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

4)、访问测试四

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

[root@web01 conf.d]# mkdir /code/course/11/22/33/ -p
[root@web01 conf.d]# echo "course-111-222-333" > /code/course/11/22/33/course_33.html
[root@web01 conf.d]# vim rw.conf 
server {
   
    listen 80;
    server_name rw.linux.com;
    root /code;

    location / {
   
        #rewrite  ^/course-11-22-33.html  /course/11/22/33/course_33.html redirect;
        rewrite ^/(.*)-(.*)-(.*)-(.*).html /$1/$2/$3/$4/$1_$4.html redirect;
    }
}

5)、将http请求跳转到https

server {
	listen 80;
	server_name www.mumusir.com;
	#rewrite (.*) https://www.mumusir.com redirect;
	return 302 https://www.mumusir.com;
}

http://www.mumusir.com  --> https://www.mumusir.com

server {
	listen 443;
	server_name www.mumusir.com;
	ssl on;
	ssl...... *.key;
	ssl..... *.crt;
}

五、rewrite正则匹配

就是通过正则匹配的方式去转发访问的资源。

1)开启正则匹配错误日志

1、修改nginx.conf配置文件种的错误日志
error_log  /var/log/nginx/error.log notice;

2、再http模块种增加
rewrite_log on;

2)正则匹配

1、通过192.168.15.7/index/1/2/3/4/5/6.html 访问更目录下的index-1-2-3-4-5-6.html
server {
   
        listen 80;
        server_name _;
        root /www/resources;
        location / {
   
                rewrite ^/index/([0-9])/([0-9])/([0-9])/([0-9])/([0-9])/([0-9]) /index-$1-$2-$3-$4-$5-$6.html break;
        }
}

2、使用192.168.15.7/jd访问www.jd.com
server {
   
	listen 80;
	server_name _;
    root /www/resources;
	location / {
   
		rewrite ^/(.*)  http://www.$1.com redirect;
	}
}



3、根目录有index-test.html和xxx-abc.html, 怎样通过192.168.15.7/index/test访问index-test.html, 使用192.168.15.7/xxx/abc访问xxx-abc.html
server {
   
	listen 80;
	server_name _;
        root /www/resources;
	location / {
   
		rewrite ^/(.*)/(.*) /$1-$2.html break;
	}

}


# 格式
rewrite  [匹配规则] [转发内容] flag
nginx只支持简单正则,高级正则不支持。

3)、rewrite的匹配优先级

#   if 模块 > location 模块 > server 模块

server {
   
    listen 80;
    server_name rw.linux.com;
	#rewrite ^(.*)$ http://www.jingdong.com;

    location /test {
   
    	rewrite ^(.*)$ http://www.mumusir.com;
    }
    
    location =/ {
   
        rewrite ^(.*)$ http://www.baidu.com;
    }
}

1.先执行server模块的rewrite指令
2.其次执行location匹配规则
3.最后执行location里面的rewrite

4)、rewrite全局变量

$server_name		#当前用户请求的域名
server {
   
    listen 80;
    server_name rw.linux.com;
    root /code;
	rewrite ^(.*)$ https://$server_name;
}

$request_filename	#请求的文件路径和名字(带着网站站点目录的路径和文件 /code/images/1.jpg)
$request_uri		#请求的文件路径和名字(不带网站站点目录的路径和文件 /images/1.jpg)

server {
   
    listen 80;
    server_name rw.linux.com;
    root /code;
	rewrite ^(.*)$ https://$server_name$request_uri;
}


#很久很久以前,网站优化
server {
   
    listen 80;
    server_name www.baidu.com baidu.com;
    root /code;
	if ($http_host = baidu.com) {
   
		rewrite (.*) http://www.baidu.com;
	}
}

#实际写法
server {
   
    listen 80;
    server_name baidu.com;
    rewrite (.*) http://www.baidu.com;

}
server {
   
    listen 80;
    server_name www.baidu.com;
    root /code;
}

六、rewrite伪静态实例

1.搭建discuz论坛

#创建站点目录
[root@web01 ~]# mkdir /hzl/discuz
[root@web01 ~]# rz -E  Discuz_X3.4_SC_UTF8_20210320.zip
[root@web01 ~]# unzip Discuz_X3.3_SC_GBK.zip -d /hzl/discuz/   #指定目录解压

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

#配置discuz论坛的配置文件
root@web01 ~]# cd/etc/nginx/conf.d/
[root@web01 conf.d]# vim discuz.conf
server {
   
    listen 80;
    server_name localhost;

    location / {
   
        root /hzl/discuz/upload;
        index index.php;
    }

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



2.数据库创建

#用户创建及数据库登录
[root@db01 ~]# mysql -uroot -p123


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

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

MariaDB [(none)]>

3.配置rewrite伪静态

[root@web01 ~]# vim /etc/nginx/conf.d/discuz.conf 

server {
   
    listen 80;
    server_name localhost;

    location / {
   
        root /hzl/discuz/upload;
        index index.php;
     rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
        rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
        rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
        rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
        rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
        rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
        rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
        rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/archiver/index.php?action=$2&value=$3 last;
        rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
        if (!-e $request_filename) {
   
           return 123;
       }
    }

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


[root@web01 conf.d]# systemctl restart nginx

4.浏览器测试

5.发送帖子

http://192.168.15.7/forum.php?mod=viewthread&tid=1&extra=

6.https配置

#配置https及本地域名解析
[root@web01 ~]# vim /etc/nginx/conf.d/discuz.conf 

server {
   
    listen 443 ssl;
    server_name www.hzl.com;

    #listen 80;
    #server_name localhost;
    
        ssl_certificate /etc/nginx/ssl_key/server.crt;
        ssl_certificate_key /etc/nginx/ssl_key/server.key;
    location / {
   
        root /hzl/discuz/upload;
        index index.php;
     rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
        rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
        rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
        rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
        rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
        rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
        rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
        rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/archiver/index.php?action=$2&value=$3 last;
        rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
        if (!-e $request_filename) {
   
           return 123;
       }
    }

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




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

    location / {
   
        rewrite (.*) https://$server_name$1 ;
        #return 302 https://$server_name$request_uri;
    }
}



#检测文件并重启nginx
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl restart nginx    php(502错误)

6.https测试(测试成功状态)

网址:https://www.hzl.com/forum.php?mod=forumdisplay&fid=2


转载:https://blog.csdn.net/weixin_55972781/article/details/116502275
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场