Nginx Location配置詳解

目錄

  • 一、語法
  • 二、匹配順序
  • 三、root 與 alias 的區別
  • 四、server 和 location 中的 root


一、語法

Location 是 Nginx 中一個非常核心的配置,關于Location,舉個簡單的配置例子:

server {listen 80;server_name 10.0.7.115;location / {root /data/app/;index index.html;}
}

當訪問 http://10.0.7.115:80 的時候,返回的是 /data/app/index.html 文件。

Location具體語法:

location [ = | ~ | ~* | ^~ ] uri { ... }

重點看方括號中的 [ = | ~ | ~* | ^~ ],其中 | 分隔的內容表示你可能會用到的語法,其中:

  • = 表示精確匹配:
location = /test {return 200 "hello";
}

例如:

/test              ok
/test/             not ok
/test2             not ok
/test/2            not ok

  • ~ 表示區分大小寫的正則匹配:
location ~ ^/test$ {[configuration] 
}

例如:

/test              ok
/Test              not ok
/test/             not ok
/test2             not ok

  • ~* 表示不區分大小寫的正則匹配:
location ~* ^/test$ {     [configuration] 
}

例如:

/test               ok
/Test               ok
/test/              not ok
/test2              not ok

  • ^~ 表示 uri 以某個字符串開頭:
location ^~ /images/ {    [configuration] 
}

例如:

/images/1.gif        ok

  • / 表示通用匹配:
location / {     [configuration] 
}

例如:

/index.html           ok
location /test {[configuration] 
}

例如:

/test                 ok
/test2                ok
/test/                ok

二、匹配順序

Location的定義分為兩種:

  • 前綴字符串(prefix string)

  • 正則表達式(regular expression),具體為前面帶 ~* 和 ~ 修飾符

當存在多個 Location 的時候,匹配的順序為:

  • 檢查使用前綴字符串的 locations,在使用前綴字符串的 locations 中選擇最長匹配的,并將結果進行儲存;

  • 如果符合帶有 = 修飾符的URI,則立刻停止匹配

  • 如果符合帶有 ^~ 修飾符的URI,則也立刻停止匹配

  • 然后按照定義文件的順序,檢查正則表達式,匹配到就停止

  • 當正則表達式匹配不到的時候,使用之前儲存的前綴字符串;

總結:

  • 順序上

    • 前綴字符串順序不重要,按照匹配長度來確定

    • 正則表達式按照定義順序

  • 優先級上

    • = 修飾符最高,^~ 次之,再者是正則,最后是前綴字符串匹配。

我們舉幾個簡單的例子進行說明

請求URI如下:

/document

示例一:

配置:

server {location /doc {[ configuration A ] }location /docu {[ configuration B ] }
}

匹配結果:

configuration B

注:雖然 /doc 也能匹配到,但 在順序上,前綴字符串順序不重要,按照匹配長度來確定


示例二:

server {location ~ ^/doc {[ configuration A ] }location ~ ^/docu {[ configuration B ] }
}

匹配結果:

configuration A

注:雖然 ~ ^/docu 也能匹配到,但 正則表達式則按照定義順序


示例三:

server {location ^~ /doc {[ configuration A ] }location ~ ^/docu {[ configuration B ] }
}

匹配結果:

configuration A

注:雖然 ~ ^/docu 也能匹配到,但 ^~優先級更高


示例四:

server {location /document {[ configuration A ] }location ~ ^/docu {[ configuration B ] }
}

匹配結果:

configuration B

注:雖然 /document 也能匹配到,但 正則的優先級更高


三、root 與 alias 的區別

當我們這樣設置 root 的時候:

location /i/ {root /data/w3;
}

當請求 /i/top.gif/data/w3/i/top.gif 會被返回。

當我們這樣設置 alias 的時候:

location /i/ {alias /data/w3/images/;
}

當請求 /i/top.gif/data/w3/images/top.gif 會被返回。


兩者的區別:

  • root 是直接拼接 root + location

  • alias 是用 alias 替換 location


四、server 和 location 中的 root

server 和 location 中都可以使用 root,舉個例子:

server {listen 80;server_name 10.0.7.115;root /data/app/;location / {root /data/web/;index index.html;}
}

如果兩者都出現,是怎樣的優先級呢?

簡單的來說,就是 就近原則,如果 location 中能匹配到,就是用 location 中的 root 配置,忽略 server 中的 root,當 location 中匹配不到的時候,則使用 server 中的 root 配置。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/446656.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/446656.shtml
英文地址,請注明出處:http://en.pswp.cn/news/446656.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

英語口語-文章朗讀Week9 Wednesday

英語文章 Birds of the same species flock together, People tend to look for someone like themselves to be friends. But having the same interests is not the only standard when we are seeking friends. In most cases, especially for adults, people l…

C++primer 第 4 章 表達式 4.7條件運算符 4.8位運算符 4.9 sizeof運算符 4.10逗號運算符 4.11類型轉換 4 . 1 2 運算符優先級表

4.7條件運算符 條件運算符(?:)允許我們把簡單的if else邏輯嵌入到單個表達式當中,條件運算符按照如下形式使用:cond ? expr1 : expr2;其中cond是判斷條件的表達式,而expr1和expr2是兩個類型相同或可能轉換為某個公共類型的表達…

Git 之 git tag標簽使用

目錄一、簡介二、本地tag操作1、創建tag標簽(1)創建輕量標簽(2)創建附注標簽2、查看tag標簽(1)查看標簽列表(2)查看標簽提交信息(3)在提交歷史中查看標簽3、刪…

codeforces 110A-C語言解題報告

110A題目網址 題目解析 1.輸入一個數字,如果數字中包含的4,7的數量是4或7的倍數,則輸出YES,否則輸出NO 舉例: 輸入: 40047 輸出: NO 2.注意點: 1)由于數字很長,所以使用long long int類型,使用scanf("%lld",&n)接收輸入 2)整型轉字符串,使用sprintf(字符串,“…

C++primer 第 5 章語句 5.2語句作用域 5.3條件語句 5 . 4 迭代語句 5.5跳轉語句 5.6 try語句塊和異常處理

5 . 1 簡單語句 C語言中的大多數語句都以分號結束,一個表達式,比如ival 5 , 末尾加上分號就變成了表達式語句(expression statement)。表達式語句的作用是執行表達式并丟棄掉求值結果:ival 5; // 一條沒什么實際用處的表達式語…

英語口語-文章朗讀Week9Thursday

英語文章 Everyone has his or her own dreams. Some people wants to be millionaires so they can give many generous donations later; some people want to be scientists so they can bring many conveniences to the world; some people only want to be bus-drivers s…

操作系統 內存管理相關知識

cpu執行程序的基本過程 譯碼器 輸入為n管腳,輸出為2^n根管腳,編號為從0到2^(n-1),用少的輸入端控制更多的輸出端最常用的是三八譯碼器AD(Address bus)地址總線: 選中一行數據每一行 8bit 組成8吧B cpu輸入端32根線,輸出端就可以控…

2000年考研英語閱讀理解文章四

文章詳細解析網址 注意點 1.注意But,however等表示觀點看法轉折的詞語 2.全篇都在提及moral decline 道德下降,最后一段寫that may have more to do with life-style所以造成現象的原因應該是life-style.(主要) 前面都是在分析,最后一段點名原因 知識點 ----單詞 envy n/v…

Chrome瀏覽器必裝插件!尤其程序猿!

Chrome 瀏覽器有一個好處,就是插件極其豐富,只有你想不到的,沒有你找不到的,這恐怕是 Chrome 瀏覽器被眾多愛好者鐘愛的原因吧。 言歸正傳,今天來給大家推薦 10 款我自己珍藏的 Chrome 瀏覽器插件。 1、crxMouse Ch…

codeforces 160A-C語言解題報告

160A題目網址 題目解析 1.輸入硬幣的個數,分配硬幣,使拿最小的硬幣數比剩下的硬幣金額大 舉例: 輸入: 2 3 3 輸出 2 2.注意點: 1)接收整型數組時要使用&,因為只有字符數組是使用指針傳遞首地址的 scanf("%d",&a[i]); 2)使用冒泡排序,將數組從大到小排序…

英語口語-文章朗讀Week10 Monday

英語文章 Here are some valuable suggestions which may assist you in landing good job First, make your resume clear and associate it with the position you are applying for. Try to add details like your temporary jobs at college or your former jobs Second, …

論文遇到的格式問題和修正方式

word空格出現小圓點怎么辦論文參考文獻批量改為上角標,簡單好用!!!字數統計網站word封面下劃線怎么對齊(非常簡單徹底解決)

codeforces 41A-C語言解題報告

41A題目網址 題目解析 1.輸入一個字符串,如果第二行是倒序輸入這個字符串的,就輸出YES,否則輸出NO 舉例: 輸入: abb aba 輸出: NO 2.倒序輸出時,使用int jstrlen(t)-1;,因為strlen()是計算字符個數,而字符串是從0開始,最后一位是字符串長度減一 3.在接收第二個字符串輸入時…

Linux查看文件的首個字母 文件屬性字段

-rw-r–r– 1 root root 762 07-29 18:19 exit文件屬性字段總共有10個字母組成;第一個字符代表文件的類型。 文件屬性字段 字母“-”表示該文件是一個普通文件字母“d”表示該文件是一個目錄,字母”d”,是dirtectory(目錄)的縮寫&#xff1b…

英語口語-文章朗讀Week10 Wednesday

英語文章 Everyone needs sleep for survival, but how much? It is said that eight hours of sleep is fundamental to a healthy person. But today, many people are sacrificing their sleep time。 Modern people have so many alternatives: cell phones, PCs, TVs, g…

嵌入式Linux多任務編程 進程 管道 命名管道

進程 進程是一個可并發執行的具有獨立功能的程序關于某個數據集合的一次執行過程,也是操作系統執行資源分配和保護的基本單位。程序的一次執行就是一個進程一個程序可以派生多個進程多個不同程序運行的時候,也會有多個相對應的進程與其相互對應進程是動…

2000年考研英語閱讀理解文章五

文章詳細解析網址 知識點 ----單詞 vitality n生命力 hypocritical adj偽善的 pushing adj有進取心的 acquisitive adj渴望獲得的,貪得無厭的 confess v供認,坦白 vulgar adj粗俗的 spectacle n場面,奇觀 participatory adj供分享的 democracy n民主,民主制,民主國家 stir v攪…

英語口語-文章朗讀Week10 Thursday

英語文章 There are many customs and traditions in Chinese civilization. Here, we will talk about the development of the way people greet each other: In ancient times, people had to kneel to those who were superior to them. This custom remained until the …