您好,登錄后才能下訂單哦!
rewrite_log on; #http 段加入
error_log logs/xxxerror.log notice; #在將錯(cuò)誤日志級(jí)別調(diào)低
location / {
rewrite / https://www.baidu.com;
}
#表示,只要訪問這個(gè)域名直接跳轉(zhuǎn)到 baidu
#查看日志能看到記錄用"/"訪問了"111.com",跳轉(zhuǎn)到了"baidu"
修改下代碼
location /rewrite/ {
rewrite / https://www.baidu.com;
}
#表示只有用域名后面跟著"/rewrite/",文件夾才會(huì)跳轉(zhuǎn)(只有/rewrite/才會(huì)觸發(fā)跳轉(zhuǎn),其他文件夾正常轉(zhuǎn)發(fā))
如下圖
#我這邊用"www.111.com/rewrite/123.com" 訪問才跳轉(zhuǎn)了
例子1,
www.111.com/111/index.html 跳轉(zhuǎn)到 www.111.com/222/index.html
location / {
rewrite ^/111/(.*)$ /222/$1 ;
}
#"^"表示根的意思,就表示 www.111.com 的的意思,(.*) 匹配所有的意思,后面"$1"調(diào)用,
#日志,能就看出/111/index.html 跳轉(zhuǎn)到 /222/index.html
last 終止在本location塊中處理接收到的URI,并將此處重寫的URI作為新的URI使用其他location進(jìn)行處理,(轉(zhuǎn)到第1個(gè)location 匹配)
break 將此處重寫的URI作為一個(gè)新的URI在當(dāng)前l(fā)ocation中繼續(xù)執(zhí)行,并不會(huì)將新的URI轉(zhuǎn)向其他location。(繼續(xù)從當(dāng)前的location 繼續(xù)往下匹配)
redirect 將重寫后的URI返回個(gè)客戶端,狀態(tài)碼是302,表明臨時(shí)重定向,主要用在replacement字符串不以“http://”,“ https://”或“ $scheme” 開頭;
permanent 將重寫的URI返回客戶端,狀態(tài)碼為301,指明是永久重定向;
那么什么是永久性跳轉(zhuǎn),什么是臨時(shí)跳轉(zhuǎn),這有什么作用呢?下面我們舉例說明:
如果有一個(gè)url,/a。
如果配置成
rewrite "/a" "http://test.html" redirect;
則說明這個(gè)跳轉(zhuǎn)是一個(gè)臨時(shí)跳轉(zhuǎn),此時(shí)如果有網(wǎng)絡(luò)爬蟲爬這個(gè)鏈接時(shí),是不會(huì)更新自己的url數(shù)據(jù)庫的。
但是如果配置成permanet,則爬蟲會(huì)更新自己的url數(shù)據(jù)庫,把/a更新為http://test.html。
這也就是臨時(shí)跳轉(zhuǎn)和永久跳轉(zhuǎn)的區(qū)別。
break和last區(qū)別測(cè)試
location /222/ {
rewrite ^/222/(.*) /333/$1;
}
location / {
rewrite ^/111/(.*) /222/$1 break;
}
#上面配置,最后url會(huì)輸出到"222"文件夾里,如果把break 換成了last的話,url里的111會(huì)被替換成222 從第一個(gè)location繼續(xù)匹配,這樣最終輸出url是"333"
#
實(shí)例1
if ($server_port = 80) {
rewrite ^(.*)$ https://$host$1 permanent;
}
#在http段配置,表示如果訪問者是以80端口訪問的,就執(zhí)行跳轉(zhuǎn)。匹配根后面的url,重寫成https://"訪問的域名"+"匹配的url"
實(shí)例2
if ( !-f $request_filename ){
rewrite (.*) http://$host/bucunzai.html;
}
#在location 段配置,"! -f $request_filename" 表示,檢測(cè)url請(qǐng)求本地是否有問題,如果頁面文件不存在,就轉(zhuǎn)發(fā)給 "buzunzai.html".
實(shí)例3 www.111.com/jpg-aaa-bbb-ccc.html => www.111.com/jpg/aaa/bbb/ccc/jpg_ccc.html
location / {
rewrite ^/(.*)-(.*)-(.*)-(.*).html$ /$1/$2/$3/$4/$1_$4.html ;
}
實(shí)例4 補(bǔ)全www
if ($host = "111.com"){
rewrite ^(.*)$ http://www.111.com$1 last;
}
#寫在http段,"$host" 變量就是 url里的域名假如以 "111.com/123.html" 訪問就是
"111.com",假如以 "www.111.com/233.html" 就是"www.111.com"
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。