1. 概述
url重寫是指通過配置conf文件,以讓網(wǎng)站的url中達(dá)到某種狀態(tài)時則定向/跳轉(zhuǎn)到某個規(guī)則冤荆,比如常見的偽靜態(tài)朴则、301重定向、瀏覽器定向等
2. 語法
rewrite ??? <regex> ??? <replacement> ??? [flag];
關(guān)鍵字 ?????? 正則 ?????????? 替代內(nèi)容 ???????? flag標(biāo)記
關(guān)鍵字
其中關(guān)鍵字error_log不能改變
正則
perl兼容正則表達(dá)式語句進(jìn)行規(guī)則匹配
替代內(nèi)容
將正則匹配的內(nèi)容替換成replacement
flag標(biāo)記
rewrite支持的flag標(biāo)記
說明:
- last ---> 本條規(guī)則匹配完成后钓简,繼續(xù)向下匹配新的location URI規(guī)則, 瀏覽器地址欄URL地址不變
- break ---> 本條規(guī)則匹配完成即終止乌妒,不再匹配后面的任何規(guī)則, 瀏覽器地址欄URL地址不變
- redirect ---> 返回302臨時重定向,瀏覽器地址會顯示跳轉(zhuǎn)后的URL地址
- permanent ---> 返回301永久重定向外邓,瀏覽器地址欄會顯示跳轉(zhuǎn)后的URL地址
3. rewrite參數(shù)的標(biāo)簽可使用的位置
server, location, if
4. 簡單示例
server {
# 訪問 /last.html 的時候撤蚊,頁面內(nèi)容重寫到 /index.html 中
rewrite /last.html /index.html last;
# 訪問 /break.html 的時候,頁面內(nèi)容重寫到 /index.html 中损话,并停止后續(xù)的匹配
rewrite /break.html /index.html break;
# 訪問 /redirect.html 的時候侦啸,頁面直接302定向到 /index.html中
rewrite /redirect.html /index.html redirect;
# 訪問 /permanent.html 的時候,頁面直接301定向到 /index.html中
rewrite /permanent.html /index.html permanent;
# 把 /html/*.html => /post/*.html 丧枪,301定向
rewrite ^/html/(.+?).html$ /post/$1.html permanent;
# 把 /search/key => /search.html?keyword=key
rewrite ^/search\/([^\/]+?)(\/|$) /search.html?keyword=$1 permanent;
}
5. if判斷
簡單重寫很多時候滿足不了需求光涂,比如需要判斷當(dāng)文件不存在時、當(dāng)路徑包含xx時等條件拧烦,則需要用到if
5.1 語法
if (表達(dá)式) {
... ...
}
注意:
- 當(dāng)表達(dá)式只是一個變量時忘闻,如果值為空或任何以0開頭的字符串都會當(dāng)做false
- 直接比較變量和內(nèi)容時,使用=或!=
- 正則表達(dá)式匹配恋博,*不區(qū)分大小寫的匹配齐佳,!~區(qū)分大小寫的不匹配
一些內(nèi)置的條件判斷:
- -f 和 !-f用來判斷是否存在文件
- -d 和 !-d用來判斷是否存在目錄
- -e 和 !-e用來判斷是否存在文件或目錄
- -x 和 !-x用來判斷文件是否可執(zhí)行
內(nèi)置的全局變量:
query_string
$content_length : 請求頭中的Content-length字段。
$content_type : 請求頭中的Content-Type字段炼吴。
$document_root : 當(dāng)前請求在root指令中指定的值本鸣。
$host : 請求主機(jī)頭字段,否則為服務(wù)器名稱缺厉。
$http_user_agent : 客戶端agent信息
$http_cookie : 客戶端cookie信息
$limit_rate : 這個變量可以限制連接速率永高。
$request_method : 客戶端請求的動作隧土,通常為GET或POST提针。
$remote_addr : 客戶端的IP地址。
$remote_port : 客戶端的端口曹傀。
$remote_user : 已經(jīng)經(jīng)過Auth Basic Module驗(yàn)證的用戶名辐脖。
$request_filename : 當(dāng)前請求的文件路徑,由root或alias指令與URI請求生成皆愉。
$scheme : HTTP方法(如http嗜价,https)。
$server_protocol : 請求使用的協(xié)議幕庐,通常是HTTP/1.0或HTTP/1.1久锥。
$server_addr : 服務(wù)器地址,在完成一次系統(tǒng)調(diào)用后可以確定這個值异剥。
$server_name : 服務(wù)器名稱瑟由。
$server_port : 請求到達(dá)服務(wù)器的端口號。
$request_uri : 包含請求參數(shù)的原始URI冤寿,不包含主機(jī)名歹苦,如:”/foo/bar.php?arg=baz”。
uri不包含主機(jī)名殴瘦,如”/foo/bar.html”。
uri相同号杠。
5.2 小示例
server {
# 如果文件不存在則返回400
if (!-f $request_filename) {
return 400;
}
# 如果host不是xuexb.com蚪腋,則301到xuexb.com中
if ( $host != "xuexb.com" ){
rewrite ^/(.*)$ https://xuexb.com/$1 permanent;
}
# 如果請求類型不是POST則返回405
if ($request_method = POST) {
return 405;
}
# 如果參數(shù)中有 a=1 則301到指定域名
if ($args ~ a=1) {
rewrite ^ http://example.com/ permanent;
}
}
結(jié)合location使用:
server {
listen 80;
server_name lotus.com;
location = /test.html {
# 默認(rèn)值為lotus
set $name lotus;
# 如果參數(shù)中有 name=xx 則使name用該值
if ($args ~* name=(\w+?)(&|$)) {
set $name $1;
}
# 301
rewrite ^ /$name.html permanent;
}
}
上面表示:
- /test.html => /lotus.html
- /test.html?name=jack => /jack.html?name=jack
6. 完整示例
編輯 conf.d/rewrite.conf配置文件:
server {
listen 80;
server_name lotus.com www.lotus.com;
if ( $host != 'www.abc.com' ) {
rewrite ^/(.*) http://www.abc.com/$1 permanent;
}
location / {
root /data/www/www;
index index.html index.htm;
}
error_log logs/error_www.abc.com.log error;
access_log logs/access_www.abc.com.log main;
}