Nginx跳轉參數(shù)與實例
Nginx跳轉參數(shù)與實例http://moko39848381.blog.163.com/blog/static/1398273312010240454593/本日志內(nèi)容來自http://iteeqg
Nginx跳轉參數(shù)與實例
http://moko39848381.blog.163.com/blog/static/1398273312010240454593/
本日志內(nèi)容來自http://iteeqg.blog.163.com/blog/static/42930654200992910456569/
Location語法語法:location [=|~|~*|^~] /uri/ { … }
location = / {
# 只匹配 / 查詢。
[ configuration A ]
}
location / {
# 匹配任何查詢,因為所有請求都已 / 開頭。但是正則表達式規(guī)則和長的塊規(guī)則將被優(yōu)先和查詢匹配。
[ configuration B ]
}
location ^~ /images/ {
# 匹配任何已 /images/ 開頭的任何查詢并且停止搜索。任何正則表達式將不會被測試。
[ configuration C ]
}
location ~* .(gif|jpg|jpeg)$ {
# 不區(qū)分大小寫匹配任何已 gif、jpg 或 jpeg 結尾的請求。然而所有 /images/ 目錄的請求將使用 Configuration C。
[ configuration D ]
}
正則表達式匹配,其中:
1.* ~ 為區(qū)分大小寫匹配
2.* ~* 為不區(qū)分大小寫匹配
3.* !~和!~*分別為區(qū)分大小寫不匹配及不區(qū)分大小寫不匹配
文件及目錄匹配,其中:
1.* -f和!-f用來判斷是否存在文件
2.* -d和!-d用來判斷是否存在目錄
3.* -e和!-e用來判斷是否存在文件或目錄
4.* -x和!-x用來判斷文件是否可執(zhí)行
flag標記有:
1.* last 相當于Apache里的[L]標記,表示完成rewrite
2.* break 終止匹配, 不再匹配后面的規(guī)則
3.* redirect 返回302臨時重定向 地址欄會顯示跳轉后的地址
4.* permanent 返回301永久重定向 地址欄會顯示跳轉后的地址
一些可用的全局變量有,可以用做條件判斷(待補全)
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
結合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;
多目錄轉成參數(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.}
目錄對換
/123456/xxxx -> /xxxx?id=123456
1.rewrite ^/(d )/(. )/ /$2?id=$1 last;
例如下面設定nginx在用戶使用ie的使用重定向到/nginx-ie目錄下:
1.if ($http_user_agent ~ MSIE) {
2.rewrite ^(.*)$ /nginx-ie/$1 break;
3.}
目錄自動加“/”
1.if (-d $request_filename){
2.rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
3.}
禁止htaccess
1.location ~/.ht { r