溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

nginx的rewrite配置

發(fā)布時間:2020-07-10 19:50:15 來源:網(wǎng)絡(luò) 閱讀:208 作者:大屁孩兒 欄目:系統(tǒng)運維

域名跳轉(zhuǎn)(重定向)、URL重寫(偽靜態(tài))、動靜分離(跳轉(zhuǎn)域名,并接入CDN實現(xiàn)加速)
#依賴PCRE庫
#模塊:ngx_http_rewrite_module
Rwrite相關(guān)指令
#if (條件) { command } coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/if.md
#break和last coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/break.md
#return 后面跟狀態(tài)碼、URL、text(支持變量)coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/return.md
#rewrite規(guī)則 coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/rewrite_ruler.md
#rewrite_log定義rewrite日志 rewrite_log on; 寫到error_log notice級別

===========================================================================
if指令
格式:if (條件判斷) { 具體的rewrite規(guī)則 }
條件舉例
條件判斷語句由Nginx內(nèi)置變量、邏輯判斷符號和目標(biāo)字符串三部分組成。
其中,內(nèi)置變量是Nginx固定的非自定義的變量,如,$request_method, $request_uri等。
邏輯判斷符號,有=, !=, ~, ~, !~, !~
!表示相反的意思,~為匹配符號,它右側(cè)為正則表達式,區(qū)分大小寫,而~為不區(qū)分大小寫匹配。
目標(biāo)字符串可以是正則表達式,通常不用加引號,但表達式中有特殊符號時,比如空格、花括號、分號等,需要用單引號引起來。
示例1
if ($request_method = POST) //當(dāng)請求的方法為POST時,直接返回405狀態(tài)碼
{
return 405; //在該示例中并未用到rewrite規(guī)則,if中支持用return指令。
}
示例2
if ($http_user_agent ~ MSIE) //user_agent帶有MSIE字符的請求,直接返回403狀態(tài)碼
{
return 403;
}
如果想同時限制多個user_agent,還可以寫成這樣
if ($http_user_agent ~ "MSIE|firefox|spider")
{
return 403;
}
示例3
if(!-f $request_filename) //當(dāng)請求的文件不存在,將會執(zhí)行下面的rewrite規(guī)則
{
rewrite 語句;
}
示例4
if($request_uri ~
'gid=\d{9,12}/') //\d表示數(shù)字,{9,12}表示數(shù)字出現(xiàn)的次數(shù)是9到12次,如gid=123456789/就是符合條件的。
{
rewrite 語句;
}

rewrite中的break和last
兩個指令用法相同,但含義不同,需要放到rewrite規(guī)則的末尾,用來控制重寫后的鏈接是否繼續(xù)被nginx配置執(zhí)行(主要是rewrite、return指令)。
示例1(連續(xù)兩條rewrite規(guī)則):
server{
listen 80;
server_name test.com;
root /tmp/123.com;

rewrite /1.html /2.html ;
rewrite /2.html /3.html ;

}
當(dāng)我們請求1.html時,最終訪問到的是3.html,兩條rewrite規(guī)則先后執(zhí)行。

break和last在location {}外部

格式:rewrite xxxxx break;

示例2(增加break):
server{
listen 80;
server_name test.com;
root /tmp/123.com;

rewrite /1.html /2.html break;
rewrite /2.html /3.html;

}
當(dāng)我們請求1.html時,最終訪問到的是2.html
說明break在此示例中,作用是不再執(zhí)行break以下的rewrite規(guī)則。
但,當(dāng)配置文件中有l(wèi)ocation時,它還會去執(zhí)行l(wèi)ocation{}段的配置(請求要匹配該location)。

示例3(break后面還有l(wèi)ocation段):
server{
listen 80;
server_name test.com;
root /tmp/123.com;

rewrite /1.html /2.html break;
rewrite /2.html /3.html;
location /2.html {
    return 403;
}

}
當(dāng)請求1.html時,最終會返回403狀態(tài)碼,說明它去匹配了break后面的location{}配置。

以上2個示例中,可以把break替換為last,它們兩者起到的效果一模一樣。

當(dāng)break和last在location{}里面

示例4(什么都不加):
server{
listen 80;
server_name test.com;
root /tmp/123.com;

location / {
    rewrite /1.html /2.html;
    rewrite /2.html /3.html;
}
location /2.html
{
    rewrite /2.html /a.html;
}
location /3.html
{
    rewrite /3.html /b.html;
}

}
當(dāng)請求/1.html,最終將會訪問/b.html,連續(xù)執(zhí)行l(wèi)ocation /下的兩次rewrite,跳轉(zhuǎn)到了/3.html,然后又匹配location /3.html

示例5(增加break):
server{
listen 80;
server_name test.com;
root /tmp/123.com;

location / {
    rewrite /1.html /2.html break;
    rewrite /2.html /3.html;
}
location /2.html
{
    rewrite /2.html /a.html;
}
location /3.html
{
    rewrite /3.html /b.html;
}

}
當(dāng)請求/1.html,最終會訪問/2.html
在location{}內(nèi)部,遇到break,本location{}內(nèi)以及后面的所有l(wèi)ocation{}內(nèi)的所有指令都不再執(zhí)行。

示例6(增加last):
server{
listen 80;
server_name test.com;
root /tmp/123.com;

location / {
    rewrite /1.html /2.html last;
    rewrite /2.html /3.html;
}
location /2.html
{
    rewrite /2.html /a.html;
}
location /3.html
{
    rewrite /3.html /b.html;
}

}
當(dāng)請求/1.html,最終會訪問/a.html
在location{}內(nèi)部,遇到last,本location{}內(nèi)后續(xù)指令不再執(zhí)行,而重寫后的url再次從頭開始,從頭到尾匹配一遍規(guī)則。

結(jié)論

當(dāng)rewrite規(guī)則在location{}外,break和last作用一樣,遇到break或last后,其后續(xù)的rewrite/return語句不再執(zhí)行。但后續(xù)有l(wèi)ocation{}的話,還會近一步執(zhí)行l(wèi)ocation{}里面的語句,當(dāng)然前提是請求必須要匹配該location。
當(dāng)rewrite規(guī)則在location{}里,遇到break后,本location{}與其他location{}的所有rewrite/return規(guī)則都不再執(zhí)行。
當(dāng)rewrite規(guī)則在location{}里,遇到last后,本location{}里后續(xù)rewrite/return規(guī)則不執(zhí)行,但重寫后的url再次從頭開始執(zhí)行所有規(guī)則,哪個匹配執(zhí)行哪個。

nginx的return指令
該指令一般用于對請求的客戶端直接返回響應(yīng)狀態(tài)碼。在該作用域內(nèi)return后面的所有nginx配置都是無效的。
可以使用在server、location以及if配置中。
除了支持跟狀態(tài)碼,還可以跟字符串或者url鏈接。
直接返回狀態(tài)碼
示例1:
server{
listen 80;
server_name www.aming.com;
return 403;
rewrite /(.*) /abc/$1; //該行配置不會被執(zhí)行。
}

示例2:
server {
.....

if ($request_uri ~ ".htpasswd|.bak")
{
return 404;
rewrite /(.*) /aaa.txt; //該行配置不會被執(zhí)行。
}
//如果下面還有其他配置,會被執(zhí)行。
.....
}

返回字符串

示例3:
server{
listen 80;
server_name www.aming.com;
return 200 "hello";
}
說明:如果要想返回字符串,必須要加上狀態(tài)碼,否則會報錯。
還可以支持json數(shù)據(jù)

示例4:
location ^~ /aming {
default_type application/json ;
return 200 '{"name":"aming","id":"100"}';
}

也支持寫一個變量

示例5:
location /test {
return 200 "$host $request_uri";
}

返回url

示例6:
server{
listen 80;
server_name www.aming.com;
return http://www.aminglinux.com/123.html;
rewrite /(.*) /abc/$1; //該行配置不會被執(zhí)行。
}
注意:return后面的url必須是以http://或者https://開頭的。

生成場景實戰(zhàn)

背景:網(wǎng)站被黑了,凡是在百度點擊到本網(wǎng)站的請求,全部都跳轉(zhuǎn)到了一個其他網(wǎng)站。
通過nginx解決:
if ($http_referer ~ 'baidu.com')
{
return 200 "<html><script>window.location.href='//$host$request_uri';</script></html>";
}

如果寫成:
return http://$host$request_uri; 在瀏覽器中會提示“重定向的次數(shù)過多”。

rewrite規(guī)則

格式:rewrite regex replacement [flag]

  • rewrite配置可以在server、location以及if配置段內(nèi)生效

  • regex是用于匹配URI的正則表達式,其不會匹配到$host(域名)

  • replacement是目標(biāo)跳轉(zhuǎn)的URI,可以以http://或者https://開頭,也可以省略掉$host,直接寫$request_uri部分(即請求的鏈接)

  • flag,用來設(shè)置rewrite對URI的處理行為,其中有break、last、rediect、permanent,其中break和last在前面已經(jīng)介紹過,
    rediect和permanent的區(qū)別在于,前者為臨時重定向(302),而后者是永久重定向(301),對于用戶通過瀏覽器訪問,這兩者的效果是一致的。
    但是,對于搜索引擎蜘蛛爬蟲來說就有區(qū)別了,使用301更有利于SEO。所以,建議replacemnet是以http://或者https://開頭的flag使用permanent。

示例1

location / {
rewrite /(.) http://www.aming.com/$1 permanent;
}
說明:.
為正則表達式,用()括起來,在后面的URI中可以調(diào)用它,第一次出現(xiàn)的()用$1調(diào)用,第二次出現(xiàn)的()用$2調(diào)用,以此類推。

示例2

location / {
rewrite /.* http://www.aming.com$request_uri permanent;
}
說明:在replacement中,支持變量,這里的$request_uri就是客戶端請求的鏈接

示例3

server{
listen 80;
server_name www.123.com;
root /tmp/123.com;
index index.html;
rewrite /(.*) /abc/$1 redirect;
}
說明:本例中的rewrite規(guī)則有問題,會造連續(xù)循環(huán),最終會失敗,解決該問題有兩個方案。
關(guān)于循環(huán)次數(shù),經(jīng)測試發(fā)現(xiàn),curl 會循環(huán)50次,chrome會循環(huán)80次,IE會循環(huán)120次,firefox會循環(huán)20次。

示例4

server{
listen 80;
server_name www.123.com;
root /tmp/123.com;
index index.html;
rewrite /(.*) /abc/$1 break;
}
說明:在rewrite中使用break,會避免循環(huán)。

示例5

server{
listen 80;
server_name www.123.com;
root /tmp/123.com;
index index.html;
if ($request_uri !~ '^/abc/')
{
rewrite /(.*) /abc/$1 redirect;
}
}
說明:加一個條件限制,也可以避免產(chǎn)生循環(huán)

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI