1. 程式人生 > 其它 >第十八章 Nginx Rewrite重寫

第十八章 Nginx Rewrite重寫

一、Nginx Rewrite概述

現在Nginx已經成為很多公司作為前端反向代理伺服器的首選,在實際工作中往往會到很多跳轉(重寫URL)的需求。比如更換域名後需要保持舊的域名能跳轉到新的域名上、某網頁發生改變需要跳轉到新的頁面、網站防盜鏈等等需求。如果在後端使用的Apache伺服器,雖然也能做跳轉,規則庫也很強大,但是用Nginx跳轉效率會更高。

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

二、Nginx Rewrite使用場景

1.地址跳轉,使用者訪問www.baidu.com這個URL時,將其定向至一個新的域名mobile.baidu.com
2.協議跳轉,使用者通過http協議請求網站時,將其重新跳轉至https協議方式
3.偽靜態,將動態頁面顯示為靜態頁面方式的一種技術,便於搜尋引擎的錄入,同時建上動態URL地址對外暴露過多的引數,提升更高的安全性。
4.搜尋引擎,SEO優化依賴於url路徑,好記的url便於智齒搜尋引擎錄入

三、Nginx Rewrite跳轉實現

Nginx是通過ngx_http_rewrite_module模組支援url重寫、支援if條件判斷,但不支援else。另外該模組需要 PCRE支援,應在編譯Nginx時指定PCRE 支援,預設已經安裝。根據相關變數重定向和選擇不同的配置,從一個location跳轉到另一個location,不過這樣的迴圈最多可以執行10次,超過後Nginx將返回500錯誤。同時,重寫模組包含set指令,來建立新的變數並設其值,這在有些情景下非常有用的,如記錄條件標識、傳遞引數到其他location、記錄做了什麼等等。rewrite功能就是,使用Nginx提供的全域性變數或自己設定的變數,結合正則表示式和標誌位實現url重寫以及重定向。
Nginx跳轉需求的實現方式:
1.使用rewrite進行匹配跳轉
2.使用if匹配全域性變數後跳轉
3.使用location匹配在跳轉

四、Nginx Rewrite基本語法

Syntax:	rewrite regex replacement [flag];
Default:	—
Context:	server, location, if

rewrite			#呼叫模組
regex 			#請求的連結(可以使用正則表示式)
replacement 	#跳轉的連結
[flag];			#rewrite支援的flag標記

#示例
server {
    ...
    rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
    rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra  last;
    ...
}

五、Nginx Rewrite的flag標記

rewrite指令根據表示式來重定向URL,或者修改字串,可以應用於server,location,if環境下,每行rewrite指令最後跟一個flag標記,支援的flag標記有如下表格所示:
標記 說明
last 相當於Apache的【L】標記,表示完成rewrite,本條規則匹配完成後,停止匹配,不再匹配後面的規則
break 本條規則匹配完成即終止,不在匹配後面的任何規則
redirect 返回302臨時重定向,瀏覽器地址會顯示跳轉後的URL地址,爬蟲不會更新url
permanent 返回301永久重定向,瀏覽器位址列會顯示跳轉後的URL地址,爬蟲更新url

六、last和break的區別

1.配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/linux.rewrite.com.conf
server {
        listen 80;
        server_name linux.rewrite.com;
        root /code;

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

2.重啟
[root@web01 ~]# nginx -t
[root@web01 ~]# systemctl restart nginx

3.配置hosts測試
10.0.0.7 linux.rewrite.com

#結果
1)訪問 linux.rewrite.com/break,返回結果404
2)訪問 linux.rewrite.com/last,返回結果ok

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

break請求:
    1)請求 linux.rewrite.com/break
    2)匹配 location ~ ^/break 會跳轉請求 到 linux.rewrite.com/test
    3)請求跳轉後,會去查詢本地的站點目錄下的 test/index.html;
    4)如果找到了,則返回站點目錄下的 test/index.html的內容;
    5)如果沒找到該目錄則報錯404,如果找到該目錄沒找到對應的檔案則403

last請求:
    1)請求 rewrite.drz.com/last
    2)匹配 location ~ ^/last 會跳轉請求 到 linux.rewrite.com/test
    2)請求跳轉後,會去查詢本地的站點目錄下的 test/index.html;
    3)如果找到了,則返回站點目錄下的 test/index.html的內容;
    4)如果沒找到,會對當前server重新的發起一次請求,linux.rewrite.com/test/
    5)如果有location匹配上,則直接返回該location的內容。
    4)如果也沒有location匹配,再返回404;

七、redirect和permanent的區別

1.配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/linux.rw.com.conf
server {
    listen 80;
    server_name linux.rw.com;
    root /code;

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

2.配置hosts測試
#配置redirect時
	關閉nginx之後訪問失敗
#配置permanent時
	關閉nginx仍然訪問成功
	
#結論:
redirect,每次訪問伺服器都會進行詢問,是否進行跳轉
permanent,記錄一次跳轉,以後都不會詢問,直接跳轉頁面,除非清空快取

八、Nginx Rewrite實踐

1.案例一:使用者訪問/abc/1.html實際上真實訪問的是/ccc/bbb/2.html

1.建立頁面
[root@web01 ~]# mkdir /code/ccc/bbb/ -p
[root@web01 ~]# echo "/ccc/bbb/2.html" > /code/ccc/bbb/2.html

2.配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/rw.conf
server {
    listen 80;
    server_name nginx.rewrite.com;
    root /code;

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

2.案例二:使用者訪問/2018/ccc/bbb/2.html實際上真實訪問的是/2014/bbb/ccc/2.html

[root@web01 ~]# mkdir /code/2014/bbb/ccc/ -p
[root@web01 ~]# echo "/2014/bbb/ccc/2.html" > /code/2014/bbb/ccc/2.html

#配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/rw.conf
server {
    listen 80;
    server_name nginx.rewrite.com;
    root /code;

    location ~* ^/2018 {
        rewrite ^/2018/(.*)/(.*)/(.*) /2014/$2/$1/$3 redirect;
    }
}

3.案例三:使用者訪問course-11-22-33.html實際上真實訪問的是/course/11/22/33/course_33.html

[root@web01 ~]# mkdir /code/course/11/22/33/ -p
[root@web01 ~]# echo "/course/11/22/33/course_33.html" >/code/course/11/22/33/course_33.html

#配置
[root@web01 ~]# vim /etc/nginx/conf.d/rw.conf
server {
    listen 80;
    server_name nginx.rewrite.com;
    root /code;

    location ~* ^/course {
        #靈活配法
        rewrite ^/(.*)-(.*)-(.*)-(.*).html$ /$1/$2/$3/$4/$1_$4.html redirect;
        #固定配法
        #rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;
    }
}

4.案例四:將http請求跳轉到https

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

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

瀏覽器輸入:baidu.com
瀏覽器轉換:http://www.baidu.com/index.html
server層轉換,rewrite跳轉:https://www.baidu.com/index.html

九、Nginx Rewrite實現偽靜態

1.建立站點目錄
[root@web01 /code]# mkdir discuz
[root@web01 /code]# unzip Discuz_X3.3_SC_GBK.zip -d /code/discuz/
#授權
[root@web01 /code]# chown -R www.www /code/discuz/

2.配置nginx配置檔案
[root@web01 /code]# vim /etc/nginx/conf.d/linux.discuz.com.conf
server {
    listen 80;
    server_name linux.discuz.com;
    root /code/discuz/upload;

    location / {
        index index.php index.html;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

3.重啟訪問
[root@web01 /code]# nginx -t
[root@web01 /code]# systemctl restart nginx

#配置hosts
10.0.0.7 nginx.rewrite.com linux.discuz.com

4.建立資料庫
[root@db01 ~]# mysql -uroot -pLinhd@123

MariaDB [(none)]> create database discuz;
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.01 sec)

5.配置偽靜態
[root@web01 /code]# cat /etc/nginx/conf.d/linux.discuz.com.conf
server {
	listen 80;
	server_name linux.discuz.com;
	root /code/discuz/upload;

    location / {
        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;
	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$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
	}
}

rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;

http://linux.discuz.com/forum-2-1.html
http://linux.discuz.com/forum.php?mod=forumdisplay&fid=2&page=1
	if (!-e $request_filename) {
		return 404;
	}
#判斷請求的內容是否存在
-e:檔案或目錄是否存在
-d:目錄是否存在
-f:檔案是否存在
-x:檔案是否可執行

十、Nginx Rwrite匹配優先順序

1.匹配優先順序
	1)先執行server層的rewrite
	2)再根據location匹配優先順序匹配
	3)再執行location下的rewrite
	4)最後再執行location下if配置的rewrite
	
2.配置
[root@web01 /code]# vim /etc/nginx/conf.d/youxianji.conf
server {
    listen 80;
    server_name linux.youxian.com;
    location / {
        rewrite (.*) http://www.jd.com;
    }

    location =/ {
        rewrite (.*) http://www.taobao.com;
    }

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

十一、Nginx Rewrite環境變數

1.$server_name

$server_name		#當前請求的域名

server {
        listen 80;
        server_name test.linux.com;
        rewrite ^(.*)$ https://$server_name$1;
}

http://test.linux.com/1.html
https://test.linux.com/1.html

2.請求檔案
$request_filename 	#請求的檔案路徑名(帶網站的站點目錄 /code/images/test.jpg)
$request_uri 		#當前請求的檔案路徑(不帶網站的站點目錄 /images/test.jpg)

#大多數用於http協議轉https協議
server {
        listen 80;
        server_name test.linux.com;
        root /code;
        return 302 https://$server_name$request_uri;
}

URL:	http://test.linux.com/images/test.jpg
URI:	/images/test.jpg

3.$scheme 
$scheme      用的協議,比如http或者https

4.$http_host
#很久之前的配置方法
server {
        listen 80;
        server_name www.baidu.com baidu.com;
        if ($http_host = baidu.com){
            rewrite (.*) http://www.baidu.com$1;
        }
}

#推薦書寫格式
server {
        listen 80;
        server_name baidu.com;
        return 302 http://www.baidu.com$request_uri;
}
server {
        listen 80;
        server_name www.drz.com;
}

十二、Nginx Rewrite日誌開啟

#配置
[root@web01 /code]# vim /etc/nginx/nginx.conf
... ...
error_log  /var/log/nginx/error.log notice;
... ...
http {
    ... ...
    rewrite_log on;
    ... ...
}