一、location正則寫法
location是Nginx中的關鍵字,代表目前的URL請求值。
1、一個示例
location = / {
# 精确比對 / ,主機名後面不能帶任何字元串
[ configuration A ]
}
location / {
# 因為所有的位址都以 / 開頭,是以這條規則将比對到所有請求
# 但是正則和最長字元串會優先比對
[ configuration B ]
}
location /documents/ {
# 比對任何以 /documents/ 開頭的位址,比對符合以後,還要繼續往下搜尋
# 隻有後面的正規表達式沒有比對到時,這一條才會采用這一條
[ configuration C ]
}
location ~ /documents/Abc {
# 比對任何以 /documents/Abc 開頭的位址,比對符合以後,還要繼續往下搜尋
# 隻有後面的正規表達式沒有比對到時,這一條才會采用這一條
[ configuration CC ]
}
location ^~ /images/ {
# 比對任何以 /images/ 開頭的位址,比對符合以後,停止往下搜尋正則,采用這一條。
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
# 比對所有以 gif,jpg或jpeg 結尾的請求
# 然而,所有請求 /images/ 下的圖檔會被 config D 處理,因為 ^~ 到達不了這一條正則
[ configuration E ]
}
location /images/ {
# 字元比對到 /images/,繼續往下,會發現 ^~ 存在
[ configuration F ]
}
location /images/abc {
# 最長字元比對到 /images/abc,繼續往下,會發現 ^~ 存在
# F與G的放置順序是沒有關系的
[ configuration G ]
}
location ~ /images/abc/ {
# 隻有去掉 config D 才有效:先最長比對 config G 開頭的位址,繼續往下搜尋,比對到這一條正則,采用
[ configuration H ]
}
location ~* /js/.*/\.js
- = 開頭表示精确比對。如 A 中隻比對根目錄結尾的請求,後面不能帶任何字元串;
- ^~ 開頭表示uri以某個正常字元串開頭,不是正則比對;
- ~ 開頭表示區分大小寫的正則比對;
- ~* 開頭表示不區分大小寫的正則比對;
- / 通用比對, 如果沒有其它比對,任何請求都會比對到。
2、順序 or 優先級
( location = ) > ( location 完整路徑 ) > ( location ^~ 路徑 ) > ( location ~,~* 正則順序 ) > ( location 部分起始路徑 ) > ( / )
3、實際使用建議
實際使用中,個人覺得至少有三個比對規則定義,如下:
#直接比對網站根,通過域名通路網站首頁比較頻繁,使用這個會加速處理,官網如是說。
#這裡是直接轉發給後端應用伺服器了,也可以是一個靜态首頁
# 第一個必選規則
location = / {
proxy_pass http://tomcat:8080/index
}
# 第二個必選規則是處理靜态檔案請求,這是nginx作為http伺服器的強項
# 有兩種配置模式,目錄比對或字尾比對,任選其一或搭配使用
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}
#第三個規則就是通用規則,用來轉發動态請求到後端應用伺服器
#非靜态檔案請求就預設是動态請求,自己根據實際把握
#畢竟目前的一些架構的流行,帶.php,.jsp字尾的情況很少了
location / {
proxy_pass http://tomcat:8080/
}
二、Rewrite規則
rewrite功能就是,使用nginx提供的全局變量或自己設定的變量,結合正規表達式和标志位實作url重寫以及重定向。rewrite隻能放在server{},location{},if{}中,并且隻能對域名後邊的除去傳遞的參數外的字元串起作用,例如 http://seanlook.com/a/we/index.php?id=1&u=str 隻對/a/we/index.php重寫。
文法:rewrite regex replacement [flag];
1、flag标志位
- last : 相當于Apache的[L]标記,表示完成rewrite,将rewrite後的位址重新在server标簽執行;
- break : 停止執行目前虛拟主機的後續rewrite指令集,将rewrite後位址重新在目前的location标簽執行;
- redirect : 傳回302臨時重定向,通知用戶端重定向到rewrtie後面的位址,位址欄會顯示跳轉後的位址;
- permanent : 傳回301永久重定向,通知用戶端永久重定向到rewrtie後面的位址,位址欄會顯示跳轉後的位址。
2、if指令與全局變量
if判斷指令
文法為if(condition){…},對給定的條件condition進行判斷。如果為真,大括号内的rewrite指令将被執行,if條件(conditon)可以是如下任何内容:
- 當表達式隻是一個變量時,如果值為空或任何以0開頭的字元串都會當做false;
- 直接比較變量和内容時,使用 = 或 !=;
- ~ 正規表達式比對,~* 不區分大小寫的比對,!~ 區分大小寫的不比對。
-f 和 !-f 用來判斷是否存在檔案
-d 和 !-d 用來判斷是否存在目錄
-e 和 !-e 用來判斷是否存在檔案或目錄
-x 和 !-x 用來判斷檔案是否可執行
例如:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
} //如果UA包含"MSIE",rewrite請求到/msid/目錄下
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
set $id $1;
} //如果cookie比對正則,設定變量$id等于正則引用部分
if ($request_method = POST) {
return ;
} //如果送出方法為POST,則傳回狀态(Method not allowed)。return不能傳回,
if ($slow) {
limit_rate k;
} //限速,$slow可以通過 set 指令設定
if (!-f $request_filename){
break;
proxy_pass http://.;
} //如果請求的檔案名不存在,則反向代理到localhost 。這裡的break也是停止rewrite檢查
if ($args ~ post=){
rewrite ^ http://example.com/ permanent;
} //如果query string中包含"post=140",永久重定向到example.com
location ~* \.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked www.jefflei.com www.leizhenfang.com;
if ($invalid_referer) {
return ;
} //防盜鍊
}
全局變量
下面是可以用作if判斷的全局變量
- $args : #這個變量等于請求行中的參數,同$query_string;
- $content_length : 請求頭中的Content-length字段;
- $content_type : 請求頭中的Content-Type字段;
- $document_root : 目前請求在root指令中指定的值;
- $host : 請求主機頭字段,否則為伺服器名稱;
- $http_user_agent : 用戶端agent資訊;
- $http_cookie : 用戶端cookie資訊;
- $limit_rate : 這個變量可以限制連接配接速率;
- $request_method : 用戶端請求的動作,通常為GET或POST;
- $remote_addr : 用戶端的IP位址;
- $remote_port : 用戶端的端口;
- $remote_user : 已經經過Auth Basic Module驗證的使用者名;
- $request_filename : 目前請求的檔案路徑,由root或alias指令與URI請求生成;
- $scheme : HTTP方法(如http,https);
- $server_protocol : 請求使用的協定,通常是HTTP/1.0或HTTP/1.1;
- $server_addr : 伺服器位址,在完成一次系統調用後可以确定這個值;
- $server_name : 伺服器名稱;
- $server_port : 請求到達伺服器的端口号;
- $request_uri : 包含請求參數的原始URI,不包含主機名,如:”/foo/bar.php?arg=baz”;
- $uri : 不帶請求參數的目前URI,$uri不包含主機名,如”/foo/bar.html”。
- $document_uri : 與$uri相同;
例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:/var/www/html
$request_filename:/var/www/html/test1/test2/test.php
3、常用正則
- . : 比對除換行符以外的任意字元;
- ? : 重複0次或1次;
- + : 重複1次或更多次;
- * : 重複0次或更多次;
- \d :比對數字;
- ^ : 比對字元串的開始;
- $ : 比對字元串的介紹;
- {n} : 重複n次;
- {n,} : 重複n次或更多次;
- [c] : 比對單個字元c;
- [a-z] : 比對a-z小寫字母的任意一個。
小括号 () 之間比對的内容,可以在後面通過 $1 來引用,$2 表示的是前面第二個 () 裡的内容。正則裡面容易讓人困惑的是 \ 轉義特殊字元。
4、rewrite 執行個體
例1:
http {
# 定義image日志格式
log_format imagelog '[$time_local] ' $image_file ' ' $image_type ' ' $body_bytes_sent ' ' $status;
# 開啟重寫日志
rewrite_log on;
server {
root /home/www;
location / {
# 重寫規則資訊
error_log logs/rewrite.log notice;
# 注意這裡要用‘’單引号引起來,避免{}
rewrite '^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$' /data?file=$3.$4;
# 注意不能在上面這條規則後面加上“last”參數,否則下面的set指令不會執行
set $image_file $3;
set $image_type $4;
}
location /data {
# 指定針對圖檔的日志格式,來分析圖檔類型和大小
access_log logs/images.log mian;
root /data/images;
# 應用前面定義的變量。判斷首先檔案在不在,不在再判斷目錄在不在,如果還不在就跳轉到最後一個url裡
try_files /$arg_file /image404.html;
}
location = /image404.html {
# 圖檔不存在傳回特定的資訊
return "image not found\n";
}
}
對形如 /images/ef/uh7b3/test.png 的請求,重寫到 /data?file=test.png,于是比對到 location /data,先看 /data/images/test.png 檔案存不存在,如果存在則正常響應,如果不存在則重寫tryfiles到新的image404 location,直接傳回404狀态碼。
例2:
rewrite ^/images/(.*)(\d+)x(\d+)\.(png|jpg|gif)$ /resizer/$1.$4?width=$2&height=$3? last;
對形如 /images/bla_500x400.jpg 的檔案請求,重寫到 /resizer/bla.jpg?width=500&height=400 位址,并會繼續嘗試比對location。