Rewrite基本概述

Rewrite基本概述

什麼是rewrite

Rewrite主要實現url地址重寫,以及重定向,就是把傳入web的請求重定向到其他url的過程。


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

#用於切換維護頁面場景
#rewrite ^(.*)$ /page/maintain.html break;

Rewrite標記Flag

rewrite指令根據表達式來重定向URL,或者修改字符串,可以應用於server,location,if環境下,每行rewrite指令最後跟一個flag標記,支持的flag標記有如下表格所示:

flag 作用
last 本條規則匹配完成後,停止匹配,不再匹配後面的規則
break 本條規則匹配完成後,停止匹配,不再匹配後面的規則
redirect 返回302臨時重定向,地址欄會顯示跳轉後的地址
permanent 返回301永久重定向,地址欄會顯示跳轉後的地址

last與break區別對比示例

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

        location ~ ^/break {
                rewrite ^/break /test/ break;
        }
        location ~ ^/last {
                rewrite ^/last /test/ last;
        }
        location /test/ {
                default_type application/json;
                return 200 "ok";
        }
}

[root@web01 conf.d]#  mkdir  /code
[root@web01 conf.d]# echo gjy.test_web01 > /code/test/index.html
#重啓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]# nginx -s reload

瀏覽器訪問網頁
在這裏插入圖片描述

如果懂shell腳本的,這兩個就類似於腳本中的,break和continue

瀏覽器訪問break
在這裏插入圖片描述


瀏覽器訪問last
在這裏插入圖片描述

last與break區別

break 只要匹配到規則,則會去本地配置路徑的目錄中尋找請求的文件;
而last只要匹配到規則,會對其所在的server(…)標籤重新發起請求。

break請求:
1、請求rewrite.gjy.com/break
2、首先:會去查找本地的/code/test/index.html;
3、如果找到了,則返回/code/test/index.html的內容;
4、如果沒找到該目錄則報錯404,如果找到該目錄沒找到對應的文件則403

last請求:
1、請求rewrite.gjy.com/last
2、首先:會去查找本地的/code/test/index.html;
3、如果找到了,則返回/code/test/index.html的內容;
4、如果沒找到,會對當前server重新的發起一次請求,rewrite.gjy.com/test/
5、如果有location匹配上,則直接返回該location的內容。
4、如果也沒有location匹配,再返回404;

所以,在訪問/break和/last請求時,雖然對應的請求目錄/test都是不存在的,理論上都應該返回404,但是實際上請求/last的時候,是會有後面location所匹配到的結果返回的,原因在於此。


redirect與permanent區別對比示例

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

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

redirect與permanent區別 (實現https)
在這裏插入圖片描述

在這裏插入圖片描述

redirect: 每次請求都會詢問服務器,如果當服務器不可用時,則會跳轉失敗。

permanent: 第一次請求會詢問,瀏覽器會記錄跳轉的地址,第二次則不再詢問服務器,直接通過瀏覽器緩存的地址跳轉。

Rewrite規則實踐

在寫rewrite規則之前,我們需要開啓rewrite日誌對規則的匹配進行調試。

[root@web01 code]# vim /etc/nginx/nginx.conf
/var/log/nginx/error.log notice;

http{
    rewrite_log on;
}

案例一

用戶訪問/abc/1.html實際上真實訪問的是/ccc/bbb/2.html

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

#1.準備真實訪問路徑
[root@web03 ~]# mkdir /code/ccc/bbb -p
[root@web03 ~]# echo "ccc_bbb_2" > /code/ccc/bbb/2.html

#2.Nginx跳轉配置
[root@web03 ~]# cd /etc/nginx/conf.d/
[root@web03 conf.d]# cat ccbb.conf 
server {
        listen 80;
        server_name  www.ccbb.com;
        
        location / {
                root /code;
                index index.html;
        }
        location /abc {
                rewrite (.*) /ccc/bbb/2.html redirect;
                #return 302 /ccc/bbb/2.html;
        }
}

#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

在這裏插入圖片描述

案例二

用戶訪問/2018/ccc/2.html實際上真實訪問的是/2014/ccc/bbb/2.html

##http://www.ccbb.com/2018/ccc/bbb/2.html  ==>  http://www.ccbb.com/2014/ccc/bbb/2.html

#1.準備真是的訪問路徑
[root@web03 conf.c]# mkdir /code/2014/ccc/bbb -p 
[root@web03 conf.c]# echo "2014_ccc_bbb_2" > /code/2014/ccc/bbb/2.html

#2.Nginx跳轉配置
[root@web03 conf.d]# cat ccbb.conf 
server {
        listen 80;

        location / {
                root /code;
                index index.html;
        }
        location /2018 {
                rewrite ^/2018/(.*)$ /2014/$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

在這裏插入圖片描述


案例三

用戶訪問/test實際上真實訪問的是https://www.baidu.com

#1.Nginx跳轉配置
[root@web03 conf.d]# cat test.conf 
server {
        listen 80;

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

#2.重啓nginx服務
[root@web03 conf.d]# nginx -s reload

在這裏插入圖片描述

案例四

用戶訪問course-11-22-33.html實際上真實訪問的是/course/11/22/33/course_33.html

#http://www.drz.com/course-11-22-33.html  ==>  http://www.drz.com/course/11/22/33/course_33.html

#1.準備真是的訪問路徑
[root@web03 ~]# mkdir /code/course/11/22/33 -p
[root@web03 ~]# echo "curl docs.etiantian.org" > /code/course/11/22/33/course_33.html

#2.Nginx跳轉配置
[root@web03 conf.d]# cat test.conf 
server {
        listen 80;
        root /code;
        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@web03 conf.d]# nginx -s reload

在這裏插入圖片描述


案例五
最核心的是 : return 302 https://[Math Processing Error]servernamerequest_uri;

http請求跳轉到https

#Nginx跳轉配置
server {
        listen 80;
        server_name www.drz.com;
        #rewrite ^(.*) https://$server_name$1 redirect;
        return 302 https://$server_name$request_uri;
}       

server {
        listen 443;
        server_name www.driverzeng.com;
        ssl on;
}

Rewrite場景示例

#部署discuz論壇
[root@web01 conf.d]# vim discuz.drz.com.conf
server {
        listen 80;
        server_name discuz.gjy.com;

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

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

#授權站點目錄
[root@web01 discuz]# chown www.www -R /code/

#創建數據庫
[root@db01 ~]# yum install -y mariadb-server
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# mysqladmin -uroot password '123'
[root@db01 ~]# mysql -uroot -p123
#創建庫
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)

#測試遠程連接數據庫
[root@web01 ~]# yum install -y mariadb
[root@web01 ~]# mysql -udiscuz -p123 -h172.16.1.51
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> \q
Bye

在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

查看僞靜態頁設置

在這裏插入圖片描述
在這裏插入圖片描述

server {
        listen 80;
        server_name discuz.gjy.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;
        }
}
將http請求跳轉到https
    

discuz解釋rewrite

thread-1-1-1.html

rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ 

$1/forum.php?mod=viewthread&tid=1&extra=page%3D1&page=1 last;

Rewrite規則補充

Rewrite匹配優先級

1.先執行server塊的rewrite指令
2.其次執行location匹配規則
3.最後執行location中的rewrite


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

如何更加規範的書寫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@web01 conf.d]# vim youxianji.conf
server {
        listen 80;
        server_name tz.gjy.com;

        rewrite (.*) http://www.baidu.com break;
      
        location / {
                rewrite (.*) http://www.jd.com redirect;
        }
        location /test {
                rewrite (.*) http://www.driverzeng.com;
        }

}
[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]# nginx -s reload

1.在server層配置了一個 rewrite的時候,location層不生效

瀏覽器訪問,tz.gjy.com, 出現的是https://www.baidu.com
在這裏插入圖片描述

.瀏覽器訪問,tz.gjy.com/test, 出現的還是https://www.baidu.com
在這裏插入圖片描述

2.在server層把配置的 rewrite註釋掉的時候,location層才生效

[root@web01 conf.d]# cat youxianji.conf 
server {
        listen 80;
        server_name tz.gjy.com;

        #rewrite (.*) http://www.baidu.com break;

        location / {
                rewrite (.*) http://www.jd.com redirect;
        }
        location /test {
                rewrite (.*) http://www.driverzeng.com;
        }

}

再次訪問tz.gjy.com, 會訪問到location層的 https://www.jd.com/
在這裏插入圖片描述

再次訪問tz.gjy.com/test ,會訪問到 https://www.driverzeng.com/
rite註釋掉的時候,location層才生效**

[root@web01 conf.d]# cat youxianji.conf 
server {
        listen 80;
        server_name tz.gjy.com;

        #rewrite (.*) http://www.baidu.com break;

        location / {
                rewrite (.*) http://www.jd.com redirect;
        }
        location /test {
                rewrite (.*) http://www.driverzeng.com;
        }

}

再次訪問tz.gjy.com, 會訪問到location層的 https://www.jd.com/
[外鏈圖片轉存中…(img-2AyX3EH4-1571750358496)]

再次訪問tz.gjy.com/test ,會訪問到 https://www.driverzeng.com/
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章