天天看點

nginx學習1.3 nginx下location配置詳解

環境:

centos6/7,nginx-1.9.15.

摘要說明:

上一篇我們對配置檔案的所有配置做了整體的詳解,及日志的配置及分割

本篇主要講述nginx下server節點中location進行詳細的解析,後續主要配置工作也在于此;

步驟:

1.虛拟主機

從上篇我們可以看到主配置檔案下nginx.conf的整體結構如下:

nginx學習1.3 nginx下location配置詳解

其中main,event,http都為全局配置;而server開始則是我們對服務的配置:

虛拟主機(server):虛拟主機使用的是特殊的軟硬體技術,它把一台運作在網際網路上的伺服器主機分成一台台“虛拟”的主機,每台虛拟主機都可以是一個獨立的網站,可以具有獨立的域名,具有完整的Intemet伺服器功能(WWW、FTP、Email等),同一台主機上的虛拟主機之間是完全獨立的。從網站通路者來看,每一台虛拟主機和一台獨立的主機完全一樣。

利用虛拟主機,不用為每個要運作的網站提供一台單獨的Nginx伺服器或單獨運作一組Nginx程序。虛拟主機提供了在同一台伺服器、同一組Nginx程序上運作多個網站的功能。

虛拟主機(server)是由監聽端口和主機名稱構成聯合主鍵的;其中主機名稱可為域名也可為ip:

舉例:基于域名的虛拟主機:

server {
#監聽端口 80
listen 80;
#監聽域名abc.com;
server_name abc.com;
location / {
# 相對路徑,相對nginx根目錄。也可寫成絕對路徑
root abc;
# 預設跳轉到index.html頁面
index index.html;
}
}
           

基于ip的虛拟主機:

server {
listen 80;
server_name 192.168.197.142;
location / {
root html;
index index.html;
}
} 
           

當然也可基于端口:

server {
listen 2022;
server_name localhost;
location / {
root /home;
index index.html;
}
}
           

注:若監聽端口隻設定其中一項(域名或ip)主機 ;其他的也會比對上

一般在nginx.conf的主配置檔案上配置将每個虛拟主機生成一個conf檔案放在conf/conf.d/下:

include /usr/local/nginx/conf/conf.d/*.conf;
           
nginx學習1.3 nginx下location配置詳解

2.location

當請求路徑确認到虛拟主機上後,後續就是根據location來進行比對路由位址;

location文法:location [=|~|~*|^~] /uri/ {… }

各符号解釋如下:

符号 含義
= = 開頭表示精确比對
^~ ^~開頭表示uri以某個正常字元串開頭,了解為比對 url路徑即可。nginx不對url做編碼,是以請求為/static/20%/aa,可以被規則^~ /static/ /aa比對到(注意是空格)
~ ~ 開頭表示區分大小寫的正則比對
~* ~* 開頭表示不區分大小寫的正則比對
/ 一般比對

注:首先比對 =,其次比對^~,其次是按檔案中順序的正則比對,最後是交給 /通用比對。當有比對成功時候,停止比對,按目前比對規則處理請求;即優先級為: 完全比對(=) ;字首比對( 

^~

);正則比對(

~

~*

 ); 普通比對(

/

規則

  • 等号類型(=)的優先級最高。一旦比對成功,則不再查找其他比對項
  • 字首普通比對(^~)優先級次之。不支援正規表達式。使用字首比對,如果有多個location比對的話,則使用表達式最長的那個
  • 正規表達式類型(~ ~*)的優先級次之。一旦比對成功,則不再查找其他比對項
  • 正常字元串比對,如果有多個location比對的話,則使用表達式最長的那個

說明

  • 先判斷精準命中,如果命中,立即傳回結果并結束解析過程
  • 若未結束,判斷字首普通命中,如果有多個命中,使用表達式“最長”的命中結果,結束解析過程
  • 若未結束,繼續判斷正規表達式的比對,按正規表達式順序為準,由上至下一旦比對成功1個,立即傳回結果,并結束解析過程
  • 若未結束,繼續普通命中,普通命中和字首普通命中相似,順序無所謂,按照location表達式的長短來确定命中結果

示例:

#基于端口
	server {
        listen       2004;
        server_name  localhost;
		#=精确比對
		location = /static/ {
            return 200 '/static/';
        }
		#^~比對url
		location ^~ /static/aa/ {
            return 200 '/static/aa/';
        }
		#~正則比對url區分大小寫
		location ~ /static/aa/bb/ {
            return 200 '/static/aa/bb/';
        }
		#~正則比對url區分大小寫
		location ~ /static/bb/ {
            return 200 '/static/bb/';
        }
		#~*正則比對url不區分大小寫
		location ~* /static/BB/ {
            return 200 '/static/BB/';
        }
		#~*正則比對url不區分大小寫
		location ~* /static/CC/ {
            return 200 '/static/CC/';
        }
		#一般比對
        location /static/cc/aa/ {
            return 200 '/static/cc/aa/';
        }
		#一般比對
		location /static/dd/ {
            return 200 '/static/dd';
        }
		#一般比對
		location /static/dd/aa/ {
            return 200 '/static/dd/aa/';
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
           

示例中的return表示直接傳回響應狀态為200,并傳回對應的字元串;更多文法為:

  • return code [text];
  • return code URL;
  • return URL;

通過curl執行結果如下:

# curl http://127.0.0.1:2004/static/aa/
/static/aa/
# curl http://127.0.0.1:2004/static/aa/bb/
/static/aa/
# curl http://127.0.0.1:2004/static/bb/
/static/bb/
# curl http://127.0.0.1:2004/static/bb/cc
/static/bb/
# curl http://127.0.0.1:2004/static/cc/
/static/CC/
# curl http://127.0.0.1:2004/static/cc/cc
/static/CC/
# curl http://127.0.0.1:2004/static/cc/aa/
/static/CC/
# curl http://127.0.0.1:2004/static/dd/
/static/dd
# curl http://127.0.0.1:2004/static/dd/aa/
/static/dd/aa/
           

繼續閱讀