一、location用法總結
location可以把不同方式的請求,定位到不同的處理方式上.
1.location的用法
location ~* /js/.*/\.js 以 = 開頭,表示精確匹配;如只匹配根目錄結尾的請求,后面不能帶任何字符串。 以^~ 開頭,表示uri以某個常規字符串開頭,不是正則匹配 以~ 開頭,表示區分大小寫的正則匹配; 以~* 開頭,表示不區分大小寫的正則匹配 以/ 開頭,通用匹配, 如果沒有其它匹配,任何請求都會匹配到
location 的匹配順序是“先匹配正則,再匹配普通”。
矯正: location 的匹配順序其實是“先匹配普通,再匹配正則”。我這么說,大家一定會反駁我,因為按“先匹配普通,再匹配正則”解釋不了大家平時習慣的按“先匹配正則,再匹配普通”的實踐經驗。這里我只能暫時解釋下,造成這種誤解的原因是:正則匹配會覆蓋普通匹配。
2.location用法舉例
-
# 精確匹配 / ,主機名后面不能帶任何字符串
location = / { [ configuration A ] }

2.# 所有的地址都以 / 開頭,所以這條規則將最后匹配到默認請求
# 但是正則和最長字符串會優先匹配
location / { [ configuration B ] }
例:
location / { proxy_pass http://server_pools; } #這條規則只有其他不符合要求才能匹配到;將是最后匹配到的,匹配度最低,上面實現的功能是:比如網站是www.blog.com;后面什么都不輸入的時候,
其他的規則也不匹配的時候,最后交給負載均衡池的服務器
3.# 匹配任何以 /documents/ 開頭的地址,匹配符合以后,還要繼續往下搜索
# 只有后面的正則表達式沒有匹配到時,這一條才會采用這一條
location /documents/ {
[ configuration C ]
}
例:
location /static/ { rewrite ^ http://www.abc.com ; } #上面實現的功能:假設網站域名為www.blog.com;那么配置上面的功能是輸入www.blog.com/static/時,不管static后面是什么頁面(頁面也可以不存在), 那么最終會同樣跳轉到www.abc.com這個網站。
4.# 匹配任何以 /documents/ 開頭的地址,匹配符合以后,還要繼續往下搜索
# 只有后面的正則表達式沒有匹配到時,這一條才會采用這一條
location ~ /documents/Abc { [ configuration CC ] }
5.# 匹配任何以 /images/ 開頭的地址,匹配符合以后,停止往下搜索正則,采用這一條。
location ^~ /images/ {
[ configuration D ]
}
6.# 匹配所有以 gif,jpg或jpeg 結尾的請求
# 然而,所有請求 /images/ 下的圖片會被 config D 處理,因為 ^~ 到達不了這一條正則
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
例:
7.# 字符匹配到 /images/,繼續往下,會發現 ^~ 存在
location /images/ {
[ configuration F ]
}
8.# 最長字符匹配到 /images/abc,繼續往下,會發現 ^~ 存在
# F與G的放置順序是沒有關系的
location /images/abc {
[ configuration G ]
}
9.# 只有去掉 config D 才有效:先最長匹配 config G 開頭的地址,繼續往下搜索,匹配到這一條正則,采用
location ~ /images/abc/ {
[ configuration H ]
}
按照上面的location寫法,以下的匹配示例成立: / -> config A 精確完全匹配,即使/index.html也匹配不了 /downloads/download.html -> config B 匹配B以后,往下沒有任何匹配,采用B /images/1.gif -> configuration D 匹配到F,往下匹配到D,停止往下 /images/abc/def -> config D 最長匹配到G,往下匹配D,停止往下 你可以看到 任何以/images/開頭的都會匹配到D並停止,FG寫在這里是沒有任何意義的,H是永遠輪不到的,這里只是為了說明匹配順序 /documents/document.html -> config C 匹配到C,往下沒有任何匹配,采用C /documents/1.jpg -> configuration E 匹配到C,往下正則匹配到E /documents/Abc.jpg -> config CC 最長匹配到C,往下正則順序匹配到CC,不會往下到E
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/ } http://tengine.taobao.org/book/chapter_02.html http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
二、Rewrite用法總結
1.rewrite的定義
rewrite只能放在 server{}, location{}, if{}中,並且只能對域名后邊的除去傳遞的參數外的字符串起作用。 例如 http://seanlook.com/a/we/index.php?id=1&u=str 只對/a/we/index.php重寫。
2.rewirte的 語法
1 執行server塊的rewrite指令 2 執行location匹配 3 執行選定的location中的rewrite指令
flag標志位
- last : 相當於Apache的[L]標記,表示完成rewrite
- break : 停止執行當前虛擬主機的后續rewrite指令集
- redirect : 返回302臨時重定向,地址欄會顯示跳轉后的地址
- permanent : 返回301永久重定向,地址欄會顯示跳轉后的地址
- last一般寫在server和if中,而break一般使用在location中
- last不終止重寫后的url匹配,即新的url會再從server走一遍匹配流程,而break終止重寫后的匹配
- break和last都能組織繼續執行后面的rewrite指令
3.rewrite常用正則
- . : 匹配除換行符以外的任意字符
- ? : 重復0次或1次
- + : 重復1次或更多次
- * : 重復0次或更多次
- \d :匹配數字
- ^ : 匹配字符串的開始
- $ : 匹配字符串的結束
- {n} : 重復n次
- {n,} : 重復n次或更多次
- [c] : 匹配單個字符c
- [a-z] : 匹配a-z小寫字母的任意一個
rewrite實例
1 例1:
2 http { 3 # 定義image日志格式 4 log_format imagelog '[$time_local] ' $image_file ' ' $image_type ' ' $body_bytes_sent ' ' $status; 5 # 開啟重寫日志 6 rewrite_log on; 7 8 server { 9 root /home/www; 10 11 location / { 12 # 重寫規則信息 13 error_log logs/rewrite.log notice; 14 # 注意這里要用‘’單引號引起來,避免{} 15 rewrite '^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$' /data?file=$3.$4; 16 # 注意不能在上面這條規則后面加上“last”參數,否則下面的set指令不會執行 17 set $image_file $3; 18 set $image_type $4; 19 } 20 21 location /data { 22 # 指定針對圖片的日志格式,來分析圖片類型和大小 23 access_log logs/images.log mian; 24 root /data/images; 25 # 應用前面定義的變量。判斷首先文件在不在,不在再判斷目錄在不在,如果還不在就跳轉到最后一個url里 26 try_files /$arg_file /image404.html; 27 } 28 location = /image404.html { 29 # 圖片不存在返回特定的信息 30 return 404 "image not found\n"; 31 } 32 } 33 34 對形如/images/ef/uh7b3/test.png的請求,重寫到/data?file=test.png,於是匹配到location /data,先看/data/images/test.png文件存不存在,如果存在則正常響應,如果不存在則重寫tryfiles到新的image404 location,直接返回404狀態碼。 35 36 例2: 37 rewrite ^/images/(.*)_(\d+)x(\d+)\.(png|jpg|gif)$ /resizer/$1.$4?width=$2&height=$3? last; 38 對形如/images/bla_500x400.jpg的文件請求,重寫到/resizer/bla.jpg?width=500&height=400地址,並會繼續嘗試匹配location。
if指令與全局變量
if判斷指令語法
當表達式只是一個變量時,如果值為空或任何以0開頭的字符串都會當做false 直接比較變量和內容時,使用=或!= ~ 正則表達式匹配 ~* 不區分大小寫的匹配 !~ 區分大小寫的不匹配 -f和!-f 用來判斷是否存在文件 -d和!-d 用來判斷是否存在目錄 -e和!-e 用來判斷是否存在文件或目錄 -x和!-x 用來判斷文件是否可執行
1 如果用戶設備為IE瀏覽器的時候,重定向 2 if ($http_user_agent ~ MSIE) { 3 rewrite ^(.*)$ /msie/$1 break; 4 } //如果UA包含"MSIE",rewrite請求到/msid/目錄下 5 6 if ($http_cookie ~* "id=([^;]+)(?:;|$)") { 7 set $id $1; 8 } //如果cookie匹配正則,設置變量$id等於正則引用部分 9 10 if ($request_method = POST) { 11 return 405; 12 } //如果提交方法為POST,則返回狀態405(Method not allowed)。return不能返回301,302 13 14 if ($slow) { 15 limit_rate 10k; 16 } //限速,$slow可以通過 set 指令設置 17 18 if (!-f $request_filename){ 19 break; 20 proxy_pass http://127.0.0.1; 21 } //如果請求的文件名不存在,則反向代理到localhost 。這里的break也是停止rewrite檢查 22 23 if ($args ~ post=140){ 24 rewrite ^ http://example.com/ permanent; 25 } //如果query string中包含"post=140",永久重定向到example.com 26 27 location ~* \.(gif|jpg|png|swf|flv)$ { 28 valid_referers none blocked www.jefflei.comwww.leizhenfang.com; 29 if ($invalid_referer) { 30 return 404; 31 } //防盜鏈 32 }
全局變量
- $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相同。
例:
1 http://localhost:88/test1/test2/test.php 2 $host:localhost 3 $server_port:88 4 $request_uri:http://localhost:88/test1/test2/test.php 5 $document_uri:/test1/test2/test.php 6 $document_root:/var/www/html 7 $request_filename:/var/www/html/test1/test2/test.php