1六、Nginx Rewrite重寫

1.Rewrite基本概述

1.1.什麼是rewrite

Rewrite主要實現url地址重寫, 以及地址重定向,就是將用戶請求web服務器的地址從新定向到其餘URL的過程。html

1.2.Rewrite使用場景

  • 1.地址跳轉,用戶訪問www.xuliangwei.com/class這個URL時,將其定向至一個新的域名class.xuliangwei.com
  • 2.協議跳轉,用戶經過http協議請求網站時,將其從新跳轉至https協議方式
  • 3.僞靜態,將動態頁面顯示爲靜態頁面方式的一種技術, 便於搜索引擎的錄入, 同時減小動態URL地址對外暴露過多的參數, 提高更高的安全性。
  • 4.搜索引擎,SEO優化依賴於url路徑, 好記的url便於支持搜索引擎錄入

1.3.Rewrite配置示例

#rewrite表達式能夠應用在server,location, if標籤下
Syntax: rewrite regex replacement [flag];
Default: --
Context: server, location, if

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

2.Rewrite標記Flag

rewrite指令根據表達式來重定向URI,或者修改URI字符串。
每行rewrite指令最後跟一個flag標記,支持的flag標記有以下表格所示:nginx

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

2.1.last與break區別對比示例

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

    location ~ ^/break {
        rewrite ^/break /test/ break;
    }
    location ~ ^/last {
        rewrite ^/last /test/ last;
    }

    location  /test/ {
       
        return 200 'ok';
    }
}

#重啓Nginx服務
[root@bgx conf.d]# systemctl restart nginx

2.2.使用瀏覽器訪問/break測試

2.3.使用瀏覽器訪問/last測試

2.4.last和 break 都是一個做用,都是表示中止rewrite規則,那last和break區別在哪呢

break匹配到規則,則會去本地路徑中目錄中尋找對應請求的文件。
last匹配到規則,會對其所在的server{...}標籤從新發起請求。
因此,在訪問/break和/last請求時,雖然對應的請求目錄/test都是不存在了,理論上都應該返回404,可是實際請求/last的時候,是會有後面localtion所匹配到的結果返回的,若是last匹配不到location的結果則在返回錯誤。web

2.5.redirect與permanent區別對比示例

[root@Nginx ~]# cat /etc/nginx/conf.d/rewrite.conf
server {
    listen 80;
    server_name rewrite.oldboy.com;
    root /code;

    location ~ ^/bgx {
        rewrite ^(.*)$ https://www.xuliangwei.com redirect;
        rewrite ^(.*)$ https://www.xuliangwei.com permanent;
        #return 301 http://kt.xuliangwei.com;
        #return 302 http://kt.xuliangwei.com;
    }
}

2.6.經過瀏覽器訪問,redirect會進行302跳轉

2.7.使用 systemctl stop nginx中止nginx,而後再次訪問網站測試 redirect

2.8.經過瀏覽器訪問,permanent會進行301跳轉

2.9.使用 systemctl stop nginx中止nginx,而後再次訪問網站測試 permanent

2.10.redirect和permanent都是一個跳轉,那redirect和permanent區別在哪呢

3.Rewrite規則實踐

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

http://www.bgx.com/abc/1.html ==> http://www.bgx.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 conf.d]# cat ccbb.conf 
server {
    listen 80;
    
    location / {
        root /code;
        index index.html;
    }
    location /abc {
        rewrite (.*) /ccc/bbb/2.html redirect;
        #return 302 /ccc/bbb/2.html;
    }
}

#3.重啓Nginx服務
[root@web03 ~]# systemctl restart nginx

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

http://www.bgx.com/2018/ccc/bbb/2.html ==> http://www.bgx.com/2014/ccc/bbb/2.html安全

#1.準備真實的訪問路徑
[root@web03 ~]# mkdir /code/2014/ccc/bbb -p
[root@web03 ~]# 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 ~]# systemctl restart nginx

例3.3:用戶訪問/test目錄下任何內容, 實際上真實訪問是http://www.xuliangwei.com

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

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

http://www.bgx.com/course-11-22-33.html ==> http://www.bgx.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 ccbb.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 ~]# systemctl restart nginx

例3.5:將http請求,跳轉至https

server {
        listen 80;
        server_name bgx.com;
        rewrite ^(.*) https://$server_name$1 redirect;
        #return 302 https://$server_name$request_uri;
}

server {
    listen 443;
    server_name bgx.com;
    ssl on;
}

4.Rewrite規則補充

4.1.Rewrite優先級

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

4.2.Rewrite經常使用變量,在匹配過程當中能夠引用一些Nginx的全局變量

$server_name 當前用戶請求的域名
$request_filename 當前請求的文件路徑名(帶網站的主目錄/code/images/test.jpg)
$request_uri 當前請求的文件路徑名(不帶網站的主目錄/images/test.jpg)
$scheme用的協議,好比http或者https測試

4.3.如何優雅的書寫Rewrite規則

server {
    listen 80;
    server_name www.oldboyedu.com oldboyedu.com;
    if ($http_host = oldboyedu.com){
        rewrite (.*) http://www.nginx.org$1;
    }
}

#推薦的書寫格式
server {
    listen 80;
    server_name oldboyedu.com;
    rewrite ^ http://www.oldboyedu.com$request_uri;
}

server {
    listen 80;
    server_name www.oldboyedu.com;
}
相關文章
相關標籤/搜索