location教程
示例:
location = / {
# 精確匹配 /,主機名后面不能帶任何字串
[ configuration A ]
}
location / {
# 因為所有的地址都以/開頭,所有這條規則將匹配到所有請求
# 但是正則和最長字串會優先匹配
[ configuration B ]
}
location /documents/ {
# 匹配任何以/documents/開頭的地址,匹配符合以后,還要繼續往下搜索
# 只有后面的正則運算式沒有匹配到時,這一條才會采用
[ configuration C ]
}
location ~ /documents/Abc {
# 匹配任何以 /documents/開頭的地址,匹配符合以后,還要繼續往下搜索
# 只有后面的正則運算式沒有匹配到時,才會采用這一條
[ 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以某個常規字串開頭,不是正則匹配
- ~開頭表示區分大小寫的正則匹配
- ~*開頭表示不區分大小寫的正則匹配
- /通用匹配,如果沒有其它匹配,任何請求都會匹配到
順序 && 優先級
(location =)> (location 完整路徑) > (location ^~路徑) > (location ~,~*正則順序) > (location 部分起始路徑) > (/)
實際使用建議
#至少有三個匹配規則定義,如下:
#直接匹配網站根,通過域名訪問網站首頁比較頻繁,使用這個會加速處理
#直接轉發給后端應用服務器,也可以是一個靜態首頁
# 第一個必選規則
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/;
}
# 第三個規則是通用規則,用來轉發動態請求道后端應用服務器
location / {
proxy_pass http://tomcat:8080/
}
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];
如果想對域名或引數字串起作用,可以使用全域變數匹配,也可以使用proxy_pass反向代理,
rewrite和location異同:同:都能實作跳轉;異:rewrite是在同一域名內更改獲取資源的路徑,而location是對另一類路徑做控制訪問或反向代理,可以proxy_pass到其他機器,
執行順序:
- server塊的rewrite指令
- location匹配
- 選定location中的rewrite指令,如果其中某步url被重寫,則重寫回圈執行1-3,直到找到真是存在的檔案;回圈超過10次,則回傳500 Internal Server Error錯誤
flag標志位
- last:相當于Apache的[L]標記,表示完成rewrite
- break:停止執行當前虛擬主機的后續rewrite指令集
- redirect:回傳302臨時重定向,地址欄會顯示跳轉后的地址
- permanent:回傳301永久重定向,地址欄會顯示跳轉后的地址
因為301和302不能簡單的只回傳狀態碼,還必須有重定向的URL,這就是return指令無法回傳301,302的原因
last和break的異同:
- last一般寫在server和if中,而break一般使用在location中
- last不終止重寫后的url匹配,即新的url會再從server走一遍匹配流程,而break終止重寫后的匹配
- break和last都能組織繼續執行后面的rewrite指令
if指令與全域變數
if判斷指令
語法:if(condition){...},對給定的條件condition進行判斷,如果為真,大括號內的rewrite指令將被執行,if條件(condition)可以是如下任何內容:
當運算式只是一個變數時,如果值為慷訓任何以0開頭的字串都會當做false
直接比較變數和內容是,使用=或!=
~ 正則運算式匹配,~* 不區分大小寫的匹配,!~ 區分大小寫的不匹配
- -f 和!-f 用來判斷是否存在檔案
- -d 和 !-d 用來判斷是否存在目錄
- -e 和 !-e 用來判斷是否存在檔案或目錄
- -x 和 !-x 用來判斷檔案是否可以執行
例如:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
} #如果UA包含”MSIE“,rewrite請求到/msie/目錄下
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
set $id $1;
} #如果cookie匹配正則,設定變數$id等于正則參考部分
if ($request_method =POST) {
return 405;
} #如果提及方法為POST,則回傳狀態405(Method not allowed),return不能回傳301,302
if ($slow) {
limit_rate 10k;
} #限速,$slow可以通過set指令設定
if (!-f $request_filename){
break;
proxy_pass http://127.0.01;
} #如果請求的檔案名不存在,則反向代理到localhost,這里的break也是停止rewrite檢查
if ($args ~ post=140){
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 404;
} #防盜鏈
}
全域變數
下面是可用作if判斷的全域變數
- $args: 這個變數等于請求行中的引數,同$query_string
- $content_length : 請求頭中的Conten-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指令與URL請求生成
- $scheme:HTTP方法(如http,https)
- $server_protocol:請求使用的協議,通常是HTTP/1.0或HTTP/1.1
- $server_addr:服務器地址,在完成一次系統呼叫后可以確定這個值
- $server_name:服務器名稱
- $server_port:請求到達服務器的埠號
- $request_url:包含請求引數的原始url,不包含主機名,如“/foo/bar.php?arg=baz”
- $url:不帶請求引數的當前url,$url不包含主機名,如“/foo/bar.html”
- $document_url:與$url相同
示例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_url:http://localhost:88/test1/test2/test.php
$document_url:/test1/test2/test.php
$document_root:/var/www/html
$request_filename:/var/www/html/test1/test2/test.php
常用正則
- .:匹配除換行符以外的任意字符
- ?:重復0次或1次
- +:重復1次或更多次
- *:重復1次或更多次
- \d:匹配數字
- ^:匹配字串的開始
- $:匹配字符的結尾
- {n}:重復n次
- {n,}:重復n次或更多次
- [c]:匹配單個字符c
- [a-z]:匹配a-z小寫字母的任意一個小括號()之間匹配的內容,可以再后面通過$1來參考,$2表示的前面第二個()里的內容,正則中容易讓人困惑的是\轉義特殊字符
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 main;
root /data/images;
# 應用前面定義的變數,首先判斷檔案在不在,不在再判斷目錄在不在,如果也不在酒跳轉到最后一個url里
try_files /$arg_file /image404.html;
}
location = /image404.html {
# 圖片不存在回傳特定的資訊
return 404 "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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/144537.html
標籤:Linux
上一篇:【原創】(十四)Linux記憶體管理之page fault處理
下一篇:手冊
