Nginx跳轉(zhuǎn)參數(shù)與實(shí)例
Nginx跳轉(zhuǎn)參數(shù)與實(shí)例http://moko39848381.blog.163.com/blog/static/1398273312010240454593/本日志內(nèi)容來(lái)自http://iteeqg
Nginx跳轉(zhuǎn)參數(shù)與實(shí)例
http://moko39848381.blog.163.com/blog/static/1398273312010240454593/
本日志內(nèi)容來(lái)自http://iteeqg.blog.163.com/blog/static/42930654200992910456569/
Location語(yǔ)法語(yǔ)法:location [=|~|~*|^~] /uri/ { … }
location = / {
# 只匹配 / 查詢(xún)。
[ configuration A ]
}
location / {
# 匹配任何查詢(xún),因?yàn)樗姓?qǐng)求都已 / 開(kāi)頭。但是正則表達(dá)式規(guī)則和長(zhǎng)的塊規(guī)則將被優(yōu)先和查詢(xún)匹配。
[ configuration B ]
}
location ^~ /images/ {
# 匹配任何已 /images/ 開(kāi)頭的任何查詢(xún)并且停止搜索。任何正則表達(dá)式將不會(huì)被測(cè)試。
[ configuration C ]
}
location ~* .(gif|jpg|jpeg)$ {
# 不區(qū)分大小寫(xiě)匹配任何已 gif、jpg 或 jpeg 結(jié)尾的請(qǐng)求。然而所有 /images/ 目錄的請(qǐng)求將使用 Configuration C。
[ configuration D ]
}
正則表達(dá)式匹配,其中:
1.* ~ 為區(qū)分大小寫(xiě)匹配
2.* ~* 為不區(qū)分大小寫(xiě)匹配
3.* !~和!~*分別為區(qū)分大小寫(xiě)不匹配及不區(qū)分大小寫(xiě)不匹配
文件及目錄匹配,其中:
1.* -f和!-f用來(lái)判斷是否存在文件
2.* -d和!-d用來(lái)判斷是否存在目錄
3.* -e和!-e用來(lái)判斷是否存在文件或目錄
4.* -x和!-x用來(lái)判斷文件是否可執(zhí)行
flag標(biāo)記有:
1.* last 相當(dāng)于Apache里的[L]標(biāo)記,表示完成rewrite
2.* break 終止匹配, 不再匹配后面的規(guī)則
3.* redirect 返回302臨時(shí)重定向 地址欄會(huì)顯示跳轉(zhuǎn)后的地址
4.* permanent 返回301永久重定向 地址欄會(huì)顯示跳轉(zhuǎn)后的地址
一些可用的全局變量有,可以用做條件判斷(待補(bǔ)全)
1.$args
2.$content_length
3.$content_type
4.$document_root
5.$document_uri
6.$host
7.$http_user_agent
8.$http_cookie
9.$limit_rate
10.$request_body_file
11.$request_method
12.$remote_addr
13.$remote_port
14.$remote_user
15.$request_filename
16.$request_uri
17.$query_string
18.$scheme
19.$server_protocol
20.$server_addr
21.$server_name
22.$server_port
23.$uri
結(jié)合QeePHP的例子
1.if (!-d $request_filename) {
2.rewrite ^/([a-z-A-Z] )/([a-z-A-Z] )/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
3.rewrite ^/([a-z-A-Z] )/?$ /index.php?namespace=user&controller=$1 last;
4.break;
多目錄轉(zhuǎn)成參數(shù)
abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2
1.if ($host ~* (.*).domain.com) {
2.set $sub_name $1;
3.rewrite ^/sort/(d )/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
4.}
目錄對(duì)換
/123456/xxxx -> /xxxx?id=123456
1.rewrite ^/(d )/(. )/ /$2?id=$1 last;
例如下面設(shè)定nginx在用戶(hù)使用ie的使用重定向到/nginx-ie目錄下:
1.if ($http_user_agent ~ MSIE) {
2.rewrite ^(.*)$ /nginx-ie/$1 break;
3.}
目錄自動(dòng)加“/”
1.if (-d $request_filename){
2.rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
3.}
禁止htaccess
1.location ~/.ht { r