ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

16@nginx之rewrite(重定向)

2021-05-07 21:01:12  阅读:182  来源: 互联网

标签:16 root rewrite server nginx html location com


文章目录

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匹配完本条规则,停止匹配,不再匹配后面的内容
redirect302跳转,临时重定向
permanent301跳转,永久重定向

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 /code/discuz
[root@web01 code]# rz Discuz_X3.3_SC_GBK.zip
[root@web01 code]# unzip Discuz_X3.3_SC_GBK.zip -d /code/discuz/

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

#配置discuz论坛的配置文件
[root@web01 conf.d]# cp blog.conf discuz.linux.com.conf
server {
    listen 80;
    server_name discuz.linux.com;

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

    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;
    }
}

#配置hosts访问

#创建数据库
[root@db02 ~]# mysql -u root -p123456
。。。。。。

MariaDB [(none)]> create database discuz charset utf8;
Query OK, 1 row affected (0.00 sec)

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

MariaDB [(none)]>

2)、hosts配置,访问论坛,发表帖子

#查看帖子地址http://discuz.linux.com/forum.php?mod=viewthread&tid=1&extra=

3)、配置rewrite伪静态

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

server {
    listen 80;
    server_name discuz.linux.com;

    location / {
        root /code/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 404;
        }
    }

    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 conf.d]# systemctl restart nginx
#http://discuz.linux.com/thread-1-1-1.html

rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;

#http://discuz.linux.com/thread-1-1-1.html
discuz.linux.com/forum.php?mod=viewthread&tid=1&extra=page%3D1&page=1

标签:16,root,rewrite,server,nginx,html,location,com
来源: https://blog.csdn.net/weixin_55972781/article/details/116502275

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有