WEB服務與NGINX(18)- nginx rewrite功能詳解




1. nginx的rewrite功能詳解

Nginx服務器利用ngx_http_rewrite_module 模塊解析和處理rewrite請求,此功能依靠 PCRE(perl compatible regularexpression),因此編譯之前要安裝PCRE庫,rewrite是nginx服務器的重要功能之一,用於實現URL的重寫,URL的重寫是非常有用的功能,比如它可以在我們改變網站結構之后,不需要客戶端修改原來的書簽,也無需其他網站修改我們的鏈接,就可以設置為訪問,另外還可以在一定程度上提高網站的安全性。

ngx_http_rewrite_module模塊的官網文檔:https://nginx.org/en/docs/http/ngx_http_rewrite_module.html。

1.1 rewrite功能概述

  • rewrite使用場景

    • 1.地址跳轉,用戶訪問www.nginx01.com這個URL時,將其定向至一個新的域名www.nginx02.com

    • 2.協議跳轉,將用戶通過http的請求協議重新跳轉至https協議(實現https主要手段)。

    • 3.URL靜態化,將動態URL地址顯示為靜態URL的一種技術,能提高搜索引擎抓取 並且能減少動態URL對外暴露過多的參數。PS:Rewrite會輕微增加服務器負擔。

  • rewrite實現的原理,如下圖所示:

image

1.2 rewrite模塊的常用指令

1.2.1 if指令

支持環境:server, location

語法格式為:

if (條件匹配condition) {
    action;  
}

其中判斷條件condition可以使用正則表達式對變量進行匹配,匹配成功時條件為true,執行action動作;否則為false,不執行action動作。變量和表達式之間可以使用以下符號連接,符號的意義如下:

符號 意義
= 比較變量和字符串是否相同,精確匹配。相同為true
!= 比較變量和字符串是否不相同,不相同為true
~ 模式匹配,支持正則表達式,區分字符大小寫
~* 模式匹配,支持正則表達式,不區分字符大小寫
!~ 模式不匹配,支持正則表達式,區分字符大小寫
!~* 模式不匹配,支持正則表達式,不區分字符大小寫
-f !-f 判斷是否為文件並且存在
-d !-d 判斷是否為目錄並且存在
-x !-x 判斷文件是否有執行權限
-e !-e 判斷文件是否存在(包括文件,目錄,軟連接)
即只有一個變量名; 如果這個變量為空字符串或者”0”,則為false

if指令的使用示例如下:

#示例一:判斷文件是否存在,實現http跳轉https
[root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf
server {
	listen 443 ssl;
	listen 80;
	server_name www.xuzhichao.com;
	access_log /var/log/nginx/access_xuzhichao.log access_json;

	ssl_certificate /etc/nginx/certs/xuzhichao.crt;
	ssl_certificate_key /etc/nginx/certs/xuzhichao.key;
	ssl_session_cache shared:ssl_cache:30m;
	ssl_session_timeout 10m;

	location / {
		root /data/nginx/html/xuzhichao;
		index index.html;
		if ( $scheme = http ) {            <==http跳轉https
			rewrite / https://www.xuzhichao.com permanent;
		}
	}

	location /main {
		default_type text/html;
		if ( -f $request_filename ) {
			return 200 "file is exist!";
		}

		if ( !-f $request_filename ) {
			return 200 "file is not exist!";
		}
	}
}

#客戶端測試:
[root@xuzhichao ~]# curl http://www.xuzhichao.com/main/index.html
file is not exist!

#示例二:使用正則表達式判斷,配置文件main部分修改如下:
	location /main {
		default_type text/html;

		if ( $request_uri ~* 'id=\d{1,4}$' ) {
			return 200 "match";
		}
	}
#其中\d表示數字,{1,4}表示數字出現1-4次。

#客戶端測試
[root@xuzhichao ~]# curl http://www.xuzhichao.com/main/?id=123
match
[root@xuzhichao ~]# curl http://www.xuzhichao.com/main/?id=123456
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

1.2.2 set指令

語法:set $variable value;

Context: server, location, if

功能:為變量賦值,value 可包含:文本,變量,或文本和變量的組合。

注意:變量定義和調用都要以$開頭。

set指令使用示例如下:

需求:根據用戶訪問的URL跳轉至指定的目錄,用戶訪問http://www.xuzhichao.com.cn時跳轉到www.xuzhichao.com/cn;用戶訪問http://www.xuzhichao.com.us時跳轉到www.xuzhichao.com/us.

#1.nginx的配置文件如下:
[root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf 
server {
	listen 80;
	server_name www.xuzhichao.com;
	access_log /var/log/nginx/access_xuzhichao.log access_json;

	location / {
		root /data/nginx/html/xuzhichao;
		index index.html;
	}
}

server {
	listen 80;
	server_name www.xuzhichao.com.cn www.xuzhichao.com.us;
	
	location / {
		if ( $host ~* "cn" ) {    <==  也可以根據瀏覽器支持的語言類型來判斷:$http_accept_language	~*	"zh-CN|zh"
			set $language cn;
		}

		if ( $host ~* "us" ) {
			set $language us;
		}

		rewrite ^/$ http://www.xuzhichao.com/$language/ permanent;
	}
}

#2.建立相關的工作目錄
[root@nginx01 ~]# cd /data/nginx/html/xuzhichao/
[root@nginx01 xuzhichao]# mkdir us
[root@nginx01 xuzhichao]# echo "<h1>US</h1>" > us/index.html
[root@nginx01 xuzhichao]# mkdir cn
[root@nginx01 xuzhichao]# echo "<h1>CHINA</h1>" > cn/index.html

#3.重啟nginx
[root@nginx01 ~]# systemctl reload nginx.service 

#4.客戶端測試,訪問不同的域名跳轉到不同的目錄中。
[root@xuzhichao ~]# curl -L -i http://www.xuzhichao.com.cn
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 21 Jun 2021 11:05:49 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: http://www.xuzhichao.com/cn/

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 21 Jun 2021 11:05:49 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Mon, 21 Jun 2021 11:03:27 GMT
Connection: keep-alive
ETag: "60d071ff-f"
Accept-Ranges: bytes

<h1>CHINA</h1>

[root@xuzhichao ~]# curl -L -i http://www.xuzhichao.com.us
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 21 Jun 2021 11:05:55 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: http://www.xuzhichao.com/us/

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 21 Jun 2021 11:05:55 GMT
Content-Type: text/html
Content-Length: 12
Last-Modified: Mon, 21 Jun 2021 11:03:13 GMT
Connection: keep-alive
ETag: "60d071f1-c"
Accept-Ranges: bytes

<h1>US</h1>

1.2.3 break指令

break用於中斷當前相同作用域(location)中的其他Nginx配置,與該指令處於同一作用域的Nginx配置中,位於它前面的配置生效,位於后面的指令配置就不再生效了,Nginx服務器在根據配置處理請求的過程中遇到該指令的時候,回到上一層作用域繼續向下讀取配置,該指令可以在server塊和location塊以及if塊中使用。

break的使用示例如下:

[root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf 
server {
	listen 80;
	server_name www.xuzhichao.com;
	access_log /var/log/nginx/access_xuzhichao.log access_json;

	location / {
		root /data/nginx/html/xuzhichao;
		index index.html;
        set $name xuzhichao;
        echo $name;
        break;      <==break之后的此location語句中的內容將不再執行。
        set $name xuzhichao;
        echo $my_port;
	}
    
    location /test {   <==以下指定依舊會被執行
        set $sex man;
        echo $sex;
    }
}

1.2.4 return指令

從nginx版本0.8.2開始支持,return用於完成對請求的處理,並直接向客戶端返回響應狀態碼,比如其可以指定重定向URL(對於特殊重定向狀態碼,301/302等) 或者是指定提示文本內容(對於特殊狀態碼403/500等),處於此指令后的所有配置都將不被執行,return可以在server、if和location塊進行配置。

常用語法格式為:

return code [text];   <==返回提示信息和狀態碼
return code URL;      <==返回提示信息和指定的URL,重定向
return URL;           <==指定的URL,重定向

使用示例如下:

#示例一:如果用戶使用IE瀏覽器或curl訪問,則返回提示字符串。
#nginx配置文件如下:
[root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf 
server {
	listen 80;
	server_name www.xuzhichao.com;
	access_log /var/log/nginx/access_xuzhichao.log access_json;

	location / {
		root /data/nginx/html/xuzhichao;
		index index.html;
	}

	location /doc {
		if ( $http_user_agent ~* "curl|MSIE [1-6]\.") {
			return 200 "please change your agent!";
		}
	}
}

#客戶端測試:
[root@xuzhichao ~]# curl http://www.xuzhichao.com/doc/
please change your agent!

#示例二:如果用戶使用IE瀏覽器或curl訪問,則跳轉到/www.nginx01.com。
#nginx配置文件如下:
[root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf 
server {
	listen 80;
	server_name www.xuzhichao.com;
	access_log /var/log/nginx/access_xuzhichao.log access_json;

	location / {
		root /data/nginx/html/xuzhichao;
		index index.html;
	}

	location /doc {
		if ( $http_user_agent ~* "curl|MSIE [1-6]\.") {
			return 302 http://www.nginx01.com;
		}
	}
}

#客戶端測試:
[root@xuzhichao ~]# curl -L -i http://www.xuzhichao.com/doc/
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 21 Jun 2021 15:25:22 GMT
Content-Type: text/html
Content-Length: 138
Connection: keep-alive
Location: http://www.nginx01.com

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 21 Jun 2021 15:25:22 GMT
Content-Type: text/html; charset=utf-8,gbk
Content-Length: 16
Last-Modified: Wed, 16 Jun 2021 13:56:31 GMT
Connection: keep-alive
Keep-Alive: timeout=60
ETag: "60ca030f-10"
Accept-Ranges: bytes

www.nginx01.com

1.2.5 rewrite_log指令

設置是否開啟記錄ngx_http_rewrite_module模塊日志記錄到error_log日志文件當中,可以配置在http、server、location或if當中,需要日志級別為notice。

使用示例如下:

[root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf 
server {
	listen 80;
	server_name www.xuzhichao.com;
	access_log /var/log/nginx/access_xuzhichao.log access_json;

	location / {
		root /data/nginx/html/xuzhichao;
		index index.html;
        set $name xuzhichao;
        echo $name;
        rewrite_log on;
        break;      <==break之后的此location語句中的內容將不再執行。
        set $name xuzhichao;
        echo $my_port;
	}
}

1.3 rewite指令詳解

rewrite通過正則表達式的匹配來改變URI,可以同時存在一個或多個指令,按照順序依次對URI進行匹配,rewrite主要是針對用戶請求的URL或者是URI做具體處理。      

  • URI(universal resource identifier):通用資源標識符,標識一個資源的路徑,可以不帶協議。   

  • URL(uniform resource location):統一資源定位符,是用於在Internet中描述資源的字符串,是URI的子集,主要包括傳輸協議(scheme)、主機(IP、端口號或者域名)和資源具體地址(目錄和文件名)等三部分。

  • 每個URL都是一個URI,但是URI不都是URL,例如:

rewrite可以配置在server、location、if環境中,其語法格式為:     

rewrite regex replacement [flag];

rewrite將用戶請求的URI基於regex所描述的模式進行檢查,匹配到時將其替換為表達式指定的新的URI。   

rewrite匹配機制:

  • 如果在同一級配置塊中存在多個rewrite規則,那么會自上而下逐個檢查;被某條件規則替換完成后,會重新開始此配置塊新一輪的替換檢查,因此,隱含有循環機制;[flag]所表示的標志位用於控制此循環機制。

  • 如果重寫的URI不斷循環匹配, 這個循環的次數不超過10次,超出后提示500響應碼。

  • 永久重定向301

    如果replacement是以http://或https://開頭,無論flag設置為什么,則替換結果會直接以重定向返回給客戶端, 即永久重定向301。

其中flag有以下幾個選項,意義如下:

  • last

    重寫完成后停止對當前URI在當前location中后續的其它重寫操作,而后對新的URI啟動新一輪重寫檢查;提前重啟新一輪循環,響應碼為200,無需客戶端參與,客戶端請求的URI不會變化。默認規則。

    不建議在location中使用。

    例如:匹配規則有n條,用戶請求來之后,檢查第一條不匹配、第二條不匹配、…、第四條匹配了,后面的就不檢查了。此時因為一重寫已經成為了一個新的URL,這個新的URL會再次被重新發給Nginx服務器,Nginx 服務器一樣會從第一條檢查、第二條、…,如果檢查第三條又被重寫了,因此又一次改成新的URL再次發起請求從頭到尾來檢查。

  • break

    重寫完成后停止對當前URI在當前location中后續的其它重寫操作,而后直接跳轉至重寫規則配置塊之后的其它配置;結束循環,無需客戶端參與,客戶端請求的URI不會變化。

    建議在location中使用。

  • redirect

    臨時重定向,重寫完成后以臨時重定向方式直接返回重寫后生成的新URI給客戶端,由客戶端重新發起請求;使用相對路徑,不能使用http://或https://開頭,狀態碼:302

  • permanent

    重寫完成后以永久重定向方式直接返回重寫后生成的新URI給客戶端,由客戶端重新發起請求,狀態碼:301

1.3.1 臨時重定向和永久重定向

  • 臨時重定向和永久重定向的主要區別為:

    域名臨時重定向,只是告訴瀏覽器不是固定重定向到當前目標域名,后期可能隨時會更改,因此瀏覽器不會緩存當前域名的DNS解析記錄;

    如果是永久重定向,瀏覽器會緩存當前域名的DNS解析記錄。

  • 臨時重定向示例:

    #將/images/臨時重定向到/pictures/
    #1.nginx的配置文件如下
    [root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf 
    server {
    	listen 80;
    	server_name www.xuzhichao.com;
    	access_log /var/log/nginx/access_xuzhichao.log access_json;
        
    	location / {
    		root /data/nginx/html/xuzhichao;
    		index index.html;
    	}
    
    	location /images/ {
    		index index.html;
    		rewrite /images/(.*) /pictures/$1 redirect;
            #rewrite /images/(.*) http://www.xuzhichao.com/pictures/$1 redirect;  <==或這種寫法
    	}
    }
    
    #2建立對應目錄
    [root@nginx01 xuzhichao]# mkdir pictures
    [root@nginx01 xuzhichao]# echo "pictures page" > pictures/index.html
    
    #3.重啟nginx服務
    [root@nginx01 ~]# systemctl reload nginx.service
    
    #4.客戶端測試:
    [root@xuzhichao ~]# curl -L -i http://www.xuzhichao.com/images/
    HTTP/1.1 302 Moved Temporarily             <==提示為302臨時重定向
    Server: nginx
    Date: Mon, 21 Jun 2021 16:17:01 GMT
    Content-Type: text/html
    Content-Length: 138
    Location: http://www.xuzhichao.com/pictures/
    Connection: keep-alive
    
    HTTP/1.1 200 OK
    Server: nginx
    Date: Mon, 21 Jun 2021 16:17:01 GMT
    Content-Type: text/html
    Content-Length: 14
    Last-Modified: Mon, 21 Jun 2021 16:15:16 GMT
    Connection: keep-alive
    ETag: "60d0bb14-e"
    Accept-Ranges: bytes
    
    pictures page
    
  • 永久重定向示例:

    #將/images/永久重定向到/pictures/
    #1.nginx的配置文件如下
    [root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf 
    server {
    	listen 80;
    	server_name www.xuzhichao.com;
    	access_log /var/log/nginx/access_xuzhichao.log access_json;
        
    	location / {
    		root /data/nginx/html/xuzhichao;
    		index index.html;
    	}
    
    	location /images/ {
    		index index.html;
    		rewrite /images/(.*) /pictures/$1 permanent;
            #rewrite /images/(.*) http://www.xuzhichao.com/pictures/$1 permanent;  <==或這種寫法
    	}
    }
    
    #2.重啟nginx服務
    [root@nginx01 ~]# systemctl reload nginx.service
    
    #3.客戶端測試:
    [root@xuzhichao ~]# curl -L -i http://www.xuzhichao.com/images/
    HTTP/1.1 301 Moved Permanently                <==提示為301永久重定向
    Server: nginx
    Date: Mon, 21 Jun 2021 16:18:45 GMT
    Content-Type: text/html
    Content-Length: 162
    Location: http://www.xuzhichao.com/pictures/
    Connection: keep-alive
    
    HTTP/1.1 200 OK
    Server: nginx
    Date: Mon, 21 Jun 2021 16:18:45 GMT
    Content-Type: text/html
    Content-Length: 14
    Last-Modified: Mon, 21 Jun 2021 16:15:16 GMT
    Connection: keep-alive
    ETag: "60d0bb14-e"
    Accept-Ranges: bytes
    
    pictures page
    

1.3.2 break和last的使用區別

1.3.2.1 break使用案例

break匹配成功后,不會繼續向下匹配,也不會跳轉到其他的location進行匹配,而是結束匹配將結果返回給客戶端。請看下面的示例:

#示例一:
#1.nginx的配置文件如下:
[root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf 
server {
	listen 80;
	server_name www.xuzhichao.com;
	access_log /var/log/nginx/access_xuzhichao.log access_json;

	location / {
		root /data/nginx/html/xuzhichao;
		index index.html;
	}

	location /break {
		root /data/nginx/html/xuzhichao;
		index index.html;
		rewrite ^/break/(.*) /test1/$1 break;    <==本location之后的rewrite和return語句都不再執行,其他location的語句也不執行;
		rewrite ^/test1/(.*) /test2/$1 break;
		return 601 "break\n";
	}

	location /test1/ {
		root /data/nginx/html/xuzhichao;
		index index.html;
		return 888 "test1 dir!\n";
	}

	location /test2/ {
		root /data/nginx/html/xuzhichao;
		index index.html;
		return 889 "test2 dir!\n";
	}
}

#2.建立nginx工作目錄
[root@nginx01 xuzhichao]# mkdir test1/
[root@nginx01 xuzhichao]# echo "test1 page" > test1/index.html
[root@nginx01 xuzhichao]# mkdir test2/
[root@nginx01 xuzhichao]# echo "test2 page" > test2/index.html
[root@nginx01 xuzhichao]# mkdir break/
[root@nginx01 xuzhichao]# echo "break page" > break/index.html

#3.重啟nginx服務
[root@nginx01 ~]# systemctl reload nginx.service

#4.客戶端測試:
#跟上index.html返回test1默認主頁內容;
[root@xuzhichao ~]# curl -i http://www.xuzhichao.com/break/index.html
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 22 Jun 2021 02:25:48 GMT
Content-Type: text/html
Content-Length: 11
Last-Modified: Tue, 22 Jun 2021 01:35:07 GMT
Connection: keep-alive
ETag: "60d13e4b-b"
Accept-Ranges: bytes

test1 page

#注意:不加index.html返回狀態碼888,執行了location test1中的return語句。
[root@xuzhichao ~]# curl -i http://www.xuzhichao.com/break/
HTTP/1.1 888 
Server: nginx
Date: Tue, 22 Jun 2021 02:25:57 GMT
Content-Type: text/html
Content-Length: 10
Connection: keep-alive

test1 dir!

[root@xuzhichao ~]# curl -i http://www.xuzhichao.com/test1/
HTTP/1.1 888 
Server: nginx
Date: Tue, 22 Jun 2021 02:25:20 GMT
Content-Type: application/octet-stream
Content-Length: 10
Connection: keep-alive

test1 dir!


#示例二:
#1.nginx的配置文件如下:
[root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf 
server {
	listen 80;
	server_name www.xuzhichao.com;
	access_log /var/log/nginx/access_xuzhichao.log access_json;

	location / {
		root /data/nginx/html/xuzhichao;
		index index.html;
	}

	location /break {
		root /data/nginx/html/xuzhichao;
		index index.html;
		rewrite ^/break/(.*)/ /test1/$1 break;    <==注意,在語句塊中多加了一個/,則rewrite跳轉失敗。
		rewrite ^/test1/(.*)/ /test2/$1 break;
		return 601 "break\n";
	}

	location /test1/ {
		root /data/nginx/html/xuzhichao;
		index index.html;
		return 888 "test1 dir!\n";
	}

	location /test2/ {
		root /data/nginx/html/xuzhichao;
		index index.html;
		return 889 "test2 dir!\n";
	}
}

#2.重啟nginx服務
[root@nginx01 ~]# systemctl reload nginx.service

#3.客戶端測試:
[root@xuzhichao ~]# curl -i http://www.xuzhichao.com/break/
HTTP/1.1 601 
Server: nginx
Date: Tue, 22 Jun 2021 02:35:52 GMT
Content-Type: application/octet-stream
Content-Length: 5
Connection: keep-alive

break 

[root@xuzhichao ~]# curl -i http://www.xuzhichao.com/break/index.html
HTTP/1.1 601 
Server: nginx
Date: Tue, 22 Jun 2021 02:35:56 GMT
Content-Type: text/html
Content-Length: 5
Connection: keep-alive

break
1.3.2.2 last使用案例

last在某個location中匹配成功后,則當前location的后續rewrite和return語句都不執行,結束當前location,然后將匹配新生成的URL跳轉到其他的location重新進行匹配,直到沒有其他location可以匹配到為止,將最后一次location的數據返回給客戶端。

#示例一:
#1.nginx配置文件:
[root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf 
server {
	listen 80;
	server_name www.xuzhichao.com;
	access_log /var/log/nginx/access_xuzhichao.log access_json;

	location / {
		root /data/nginx/html/xuzhichao;
		index index.html;
	}

	location /last {
		root /data/nginx/html/xuzhichao;
		index index.html;
		rewrite ^/last/(.*) /test1/$1 last;     <==本location之后的rewrite和return語句都不再執行,會以新的URL重新進行匹配;
		rewrite ^/test1/(.*) /test2/$2 last;
	}

	location /test1/ {
		root /data/nginx/html/xuzhichao;
		index index.html;
		rewrite ^/test1/(.*) /test3/$1 last;
	}

	location /test2/ {
		root /data/nginx/html/xuzhichao;
		index index.html;
		return 889 "test2 dir!\n";
	}

	location /test3 {
		root /data/nginx/html/xuzhichao;
		index index.html;
		return 890 "test3 dir!\n";
	}
}

#2.新建nginx工作目錄:
[root@nginx01 xuzhichao]# mkdir last
[root@nginx01 xuzhichao]# echo "<h1>last page</h1>" > last/index.html
[root@nginx01 xuzhichao]# mkdir test3
[root@nginx01 xuzhichao]# echo "<h1>test3 page</h1>" > test3/index.html

#3.客戶端測試:
[root@xuzhichao ~]# curl -i http://www.xuzhichao.com/last/index.html
HTTP/1.1 890 
Server: nginx
Date: Tue, 22 Jun 2021 03:11:26 GMT
Content-Type: text/html
Content-Length: 11
Connection: keep-alive

test3 dir!

[root@xuzhichao ~]# curl -i http://www.xuzhichao.com/last/
HTTP/1.1 890 
Server: nginx
Date: Tue, 22 Jun 2021 03:11:30 GMT
Content-Type: application/octet-stream
Content-Length: 11
Connection: keep-alive

test3 dir!


#示例二:
#1.nginx配置文件:
[root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf 
server {
	listen 80;
	server_name www.xuzhichao.com;
	access_log /var/log/nginx/access_xuzhichao.log access_json;

	location / {
		root /data/nginx/html/xuzhichao;
		index index.html;
	}

	location /last {
		root /data/nginx/html/xuzhichao;
		index index.html;
		rewrite ^/last/(.*)/ /test1/$1 last;     <==注意,在語句塊中多加了一個/,則rewrite跳轉失敗。;
		rewrite ^/test1/(.*)/ /test2/$2 last;
	}

	location /test1/ {
		root /data/nginx/html/xuzhichao;
		index index.html;
		rewrite ^/test1/(.*) /test3/$1 last;
	}

	location /test2/ {
		root /data/nginx/html/xuzhichao;
		index index.html;
		return 889 "test2 dir!\n";
	}

	location /test3 {
		root /data/nginx/html/xuzhichao;
		index index.html;
		return 890 "test3 dir!\n";
	}
}

#2.客戶端測試:
[root@xuzhichao ~]# curl -i http://www.xuzhichao.com/last/index.html
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 22 Jun 2021 03:16:36 GMT
Content-Type: text/html
Content-Length: 19
Last-Modified: Tue, 22 Jun 2021 03:05:05 GMT
Connection: keep-alive
ETag: "60d15361-13"
Accept-Ranges: bytes

<h1>last page</h1>

[root@xuzhichao ~]# curl -i http://www.xuzhichao.com/last/
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 22 Jun 2021 03:16:39 GMT
Content-Type: text/html
Content-Length: 19
Last-Modified: Tue, 22 Jun 2021 03:05:05 GMT
Connection: keep-alive
ETag: "60d15361-13"
Accept-Ranges: bytes

<h1>last page</h1>

1.4 rewrite生產案例

  • 若用戶使用手機訪問網站www.xuzhichao.com,則跳轉到www.xuzhichao.com/mobile 或mobile.xuzhichao.com.

    [root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf 
    server	{				
        listen	80;
        server_name www.xuzhichao.com;
        root /data/nginx/html/xuzhichao;
    	access_log /var/log/nginx/access_xuzhichao.log access_json;
    				
        if	($http_user_agent ~* "android|iphone|ipad")	{
    		rewrite / /mobile;
            #rewrite / http://mobile.xuzhichao.com;
    	}
    }
    
  • 網站在維護過程中,只有指定IP192.168.20.7和192.168.500/24可以正常訪問網頁,其他用戶訪問的所有網頁都重定向到一個維護頁面。

    [root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf 
    server	{				
        listen	80;
        server_name www.xuzhichao.com;
        root /data/nginx/html/xuzhichao;
    	access_log /var/log/nginx/access_xuzhichao.log access_json;
    	
        set $ip 0;
        
        if ( $remote_addr = "192.168.20.7|192.168.500/24" ) {
            set $ip 1;
        }
        
        if ( $ip = 0 ) {
            rewrite ^(.*)$ /weihu.html break;
        } 
        
        location / {
            root /data/nginx/html/xuzhichao;
            index index.html;
        }
    }
    
  • 當服務器遇到502,403錯誤時,將用戶訪問跳轉到維護頁面

    [root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf 
    server	{				
        listen	80;
        server_name www.xuzhichao.com;
        root /data/nginx/html/xuzhichao;
    	access_log /var/log/nginx/access_xuzhichao.log access_json;
    	
        error_page 403 502 = @weihu;
        location @weihu {
            rewrite ^(.*)$ /weihu.html break;
        }
        
        location / {
            root /data/nginx/html/xuzhichao;
            index index.html;
        }
    }
    
  • nginx的stub狀態頁面,只允許公司的公網出口IP可以訪問,其他IP訪問全部返回500或跳轉到網站首頁。

    [root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf 
    server	{				
        listen	80;
        server_name www.xuzhichao.com;
        root /data/nginx/html/xuzhichao;
    	access_log /var/log/nginx/access_xuzhichao.log access_json;
        
        location / {
            root /data/nginx/html/xuzhichao;
            index index.html;
        }
        
        location /stub {
            set $ip 0;
            
            if ( $remote_addr = "192.168.20.7|192168.50.0/24") {
                set $ip 1;
            }
            
            if ( $ip = 0 ) {
                return 500;
                #rewrite /(.*)$ http://www.xuzhichao.com/ redirect;
            }
            
            stub_status;        
        }
    }
    
  • 當用戶訪問網站時輸入了一個不存在資源頁面,可以將用戶重定向到網站首頁。

    [root@nginx01 ~]# cat /etc/nginx/conf.d/xuzhichao.conf 
    server	{				
        listen	80;
        server_name www.xuzhichao.com;
        root /data/nginx/html/xuzhichao;
    	access_log /var/log/nginx/access_xuzhichao.log access_json;
        
        location / {
            root /data/nginx/html/xuzhichao;
            index index.html;
        }
        
        if ( !-f $request_filename ) {
            rewrite ^(.*)$  http://www.xuzhichao.com/ redirect;
            # return 404;
        }
    }
    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM