ICode9

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

第十四章•Nginx实现Rewrite重写

2021-03-09 11:04:32  阅读:144  来源: 互联网

标签:rewrite root Rewrite server Nginx html com conf 重写


1、rewrite基本概述

1.1、什么是rewrite

rewrite主要实现URL地址重写,以及重定向,就是把传入web的请求重定向到其他url的过程。

1.2、rewrite使用场景

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

1.3、rewrite配置示例

句法:Syntax: rewrite regex replacement [flag]

默认:Default: --

语境:Context: server,location,if

 

#用于切换维护页面场景

#rewrite ^(.*)$ /page/maintain.html break;

2、rewrite标记Flag

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

Flag

作用

Last

本条规则匹配完成后,停止匹配,不再匹配后面的规则

break

本条规则匹配完成后,停止匹配,不再匹配后面的规则

redirect

返回302临时重定向,地址栏会显示跳转后的地址

permanent

返回301永久重定向,地址栏会显示跳转后的地址

 

2.1、last与break区别对比示例

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

1 server {

2 listen 80;

3 server_name rewrite.sunedu.com;

4 root /www/rewrite;

5

6 location ~ ^/break {

7 rewrite ^/break /test/ break;

8 }

9

10 location ~ ^/last {

11 rewrite ^/last /test/ last;

12 }

13

14 location /test/ {

15 default_type application/json;

16 return 200 "ok";

17 }

18 }

~

~

~

~

~

~

~

~

~

~

"/etc/nginx/conf.d/rewrite.conf" 18L, 296C written

[root@web01 ~]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@web01 ~]# systemctl restart nginx

如果懂shell脚本的,这两个就类似于脚本中的,break和continue

2.2、浏览器访问break

 

2.3、浏览器访问last

2.4、last与break区别

break 只要匹配到规则,则会去本地配置路径的目录中寻找请求的文件;

而last只要匹配到规则,会对其所在的server(...)标签重新发起请求。

break请求:

1、请求rewrite.sunedu.com/break

2、首先:会去查找本地的/code/test/index.html;

3、如果找到了,则返回/code/test/index.html的内容;

4、如果没找到该目录则报错404,如果找到该目录没找到对应的文件则403

 

last请求:

1、请求rewrite.sunedu.com/last

2、首先:会去查找本地的/code/test/index.html;

3、如果找到了,则返回/code/test/index.html的内容;

4、如果没找到,会对当前server重新的发起一次请求,rewrite.sunedu.com/test/

5、如果有location匹配上,则直接返回该location的内容。

4、如果也没有location匹配,再返回404;

所以,在访问/break和/last请求时,虽然对应的请求目录/test都是不存在的,理论上都应该返回404,但是实际上请求/last的时候,是会有后面location所匹配到的结果返回的,原因在于此。

2.5、redirect与permanent区别对比示例

[root@web01 ~]# cat /etc/nginx/conf.d/rewrite.conf

server {

    listen 80;

    server_name rewrite.sunedu.com;

    root /www/rewrite;

    

    location /test {

    rewrite ^(.*)$ https://blog.sunedu.com redirect;

    #rewrite ^(.*)$ https://blog.sunedu.com permanent;

    #rewrite 301 https://blog.sunedu.com;

    #rewrite 302 https://blog.sunedu.com;

    }

}

[root@web01 ~]# cat /etc/nginx/conf.d/blog.conf

server {

    listen 80;

    server_name blog.sunedu.com;

 

    location / {

        root /www/blog;

        index index.html;

    }

}

[root@web01 ~]# mkdir /www/blog

[root@web01 ~]# echo "blog" > /www/blog/index.html

[root@web01 ~]# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

10.0.0.207 blog.sunedu.com

2.6、redirect与permanent区别(实现https)

redirect: 每次请求都会询问服务器,如果当服务器不可用时,则会跳转失败

permanent: 第一次请求会询问,浏览器会记录跳转的地址,第二次则不再询问服务器,直接通过浏览器缓存的地址跳转。

3、rewrite规则实践

在写rewrite规则之前,我们需要开启rewrite日志对规则的匹配进行调试。

3.1、案例一

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

#http://www.sunedu.com/abc/1.html ==> http://www.sunedu.com/ccc/bbb/2.html

#1.准备真实访问路径

[root@web01 ~]# mkdir /www/ccc/bbb -p

[root@web01 ~]# echo "ccc_bbb_2" > /www/ccc/bbb/2.html

 

#2.Nginx跳转配置

[root@web03 ~]# cd /etc/nginx/conf.d/

[root@web01 conf.d]# vim ccbb.conf

server {

listen 80;

        server_name rewrite1.sunedu.com;

location / {

root /www;

index index.html;

}

location /abc {

rewrite (.*) /ccc/bbb/2.html redirect;

#return 302 /ccc/bbb/2.html;

}

}

 

 

#3.重启Nginx服务

[root@web01 conf.d]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

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

3.2、案例二

用户访问/2018/ccc/2.html实际上真实访问的是/2014/ccc/bbb/2.html

#1.准备真是的访问路径

[root@web01 conf.d]# mkdir /www/2021/ccc/bbb -p

[root@web01 conf.d]# echo "2021_ccc_bbb_2"> /www/2021/ccc/bbb/2.html

 

#2.Nginx跳转配置

[root@web03 conf.d]# cat ccbb.conf

server {

listen 80;

        server_name rewrite1.sunedu.com;

location / {

root /code;

index index.html;

}

location /2020 {

rewrite ^/2020/(.*)$ /2021/$1 redirect;

}

}

 

#3.重启nginx服务

[root@web03 conf.d]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@web03 conf.d]# nginx -s reload

3.3、案例三

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

#1.Nginx跳转配置

[root@web03 conf.d]# cat test.conf

server {

listen 80;

        server_name rewrite1.sunedu.com;

location /test {

rewrite (.*) https://www.baidu.com redirect;

}

}

 

#2.重启nginx服务

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

3.4、案例四

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

#1.准备真是的访问路径

[root@web01 conf.d]# mkdir /www/course/11/22/33 -p

[root@web01 conf.d]# echo "curl sun.com" > /www/course/11/22/33/course_33.html

 

 

#2.Nginx跳转配置

[root@web01 conf.d]# vim test.conf

server {

listen 80;

server_name test1.sun.com;

root /www;

index index.html;

location / {

rewrite ^/course-(.*)-(.*)-(.*).html /course/$1/$2/$3/course_$3.html redirect;

        #rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;

}

}

 

 

#3.重启nginx服务

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

3.5、案例五

将http请求跳转https

#Nginx跳转配置

server {

listen 80;

server_name www.sun.com;

rewrite ^(.*) https://$server_name$1 redirect;

#return 302 https://$server_name$request_uri;

}

 

server {

listen 443;

server_name blog.sun.com;

ssl on;

}

4、rewrite场景示例

[root@web01 conf.d]# vim discuz.sun.com

server {

listen 80;

server_name discuz.sun.com;

location / {

root /www;

index index.php index.html;

}

location ~ \.php$ {

root /www;

fastcgi_pass 127.0.0.1:9000;

fastcgi_param SCRIPT_FILENAME $document_root$fastcg

i_script_name;

include fastcgi_params;

}

}

[root@web01 conf.d]# mkdir /www/–p

[root@web01 discuz]# unzip Discuz_X3.4_SC_UTF8_20210119.zip

[root@web01 discuz]# chown -R www.www /www/

[root@web01 discuz]# chmod 777 upload/

MariaDB [(none)]> create database discuz;

Query OK, 1 row affected (0.00 sec)

 

MariaDB [(none)]> grant all on discuz.* to discuz@'%' identified by '123';

Query OK, 0 rows affected (0.00 sec)

server {

listen 80;

server_name discuz.drz.com;

 

location / {

root /code/discuz/upload;

index index.php index.html;

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;

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;

}

}

5、Rewrite规则补充

5.1、Rewrite匹配优先级

1.先执行server块的rewrite指令

2.其次执行location匹配规则

3.最后执行location中的rewrite

 

5.2、Rewrite与Nginx全局变量

Rewrite在匹配过程中,会用到一些Nginx全局变量

 

$server_name #当前用户请求的域名

 

server {

listen 80;

server_name test.drz.com;

rewrite ^(.*)$ https://$server_name$1;

}

$request_filename 请求的文件路径名(带网站的主目录/code/images/test.jpg)

$request_uri 当前请求的文件路径(不带网站的主目录/inages/test.jpg)

 

#大多数用于http协议转gttps协议

server {

listen 80;

server_name php.drz.com;

return 302 https://$server_name$request_uri;

}

$scheme 用的协议,比如http或者https

5.3、如何更加规范的书写Rewrite规则

server {

listen 80;

server_name www.drz.com drz.com;

if ($http_host = drz.com){

rewrite (.*) http://www.drz.com$1;

}

}

 

#推荐书写格式

server {

listen 80;

server_name drz.com;

rewrite ^ http://www.drz.com$request_uri;

}

server {

listen 80;

server_name www.drz.com;

}

标签:rewrite,root,Rewrite,server,Nginx,html,com,conf,重写
来源: https://www.cnblogs.com/sunyuhang1/p/14504347.html

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

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

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

ICode9版权所有