nginx的location规则(一)
nginx的location規則(一)
nginx的url配置是使用nginx最基本功能。nginx作為服務器,它可以接收請求, 處理請求,都是基于客戶端url訪問。掌握url的配置要了解配置的幾個指令。熟悉每個匹配模式的特點。
之前對于nginx的location匹配規則,我是一塌糊涂,最近認真學了一下,稍微清晰一點了,先記錄下來,方便以后再來查看。
1、location的介紹
location用來根據URI來進行不同的定位。通過它可以把網站的不同部分,定位到不同的處理方式上,比如碰到.php,調用php解釋器。
2、location的語法
location [=|~|~*|^~] patt {} 中括號可以不寫任何參數,此時稱為一般匹配。
符號說明:
1) = 表示精確匹配
2) ^~ 表示uri以指定字符或字符串開頭的前綴匹配,不是正則匹配。一旦匹配成功,則不再查找其他匹配項
3) ~ 表示區分大小寫的正則匹配
4) ~* 表示不區分大小寫的正則匹配
5) / 表示通用匹配, 如果沒有其它匹配,任何請求都會匹配到
規則優先級:
= 高于 ^~ 高于 ~* 等于 ~ 高于 /
因此,大類型可以分為3種
1)location = patt{} [精準匹配]
2)location patt{} [一般匹配]
3)location ~ patt{} [正則匹配]
3、匹配規則
首先看有沒有精準匹配,如果有,則停止匹配過程。
location = patt{
config A
}
如果 $uri == patt,匹配成功,使用config A
4、舉例
在下面舉例開始之前,先說明一個事實:訪問域名test.php7.isee.wang和test.php7.isee.wang/效果是一樣。
原因:1)訪問域名,其實是會由DNS解析服務,解析成對應服務器的ip地址。在這個ip地址,我們所要訪問的是服務器目錄下的文件。
2)因此,直接訪問域名或者域名后面跟”/“,都是在根目錄/處進行訪問,所以效果是一樣的。
4.1 配置舉例1
1 location = / {
2
3 root /www/wwwroot/test.php7.isee.wang/html/; #目錄
4
5 index index.htm index.html; #索引引導頁
6
7 }
訪問域名test.php7.isee.wang
報404,那么我們就來查看下錯誤日志
執行命令:tail -f /www/wwwlogs/test.php7.isee.wang.error.log
分析:location 后面的 ”= /“表示精確匹配/,但是斜線只是根目錄,后面沒有緊跟要訪問的實際目錄或者文件,所以這里的root沒發揮作用,最終引導到索引頁index.htm=>test.php7.isee.wang/index.htm
最終結果,訪問了/www/server/nginx/html/index.htm(nginx全局服務器默認目錄)
定位流程:
1、精準匹配中 “/”,定位到引導頁index.htm
2、再次訪問/index.htm,此次內部跳轉已經是“/index.htm”,根目錄為/www/server/nginx/html,但是我這里該目錄下沒有index.htm,所以報404
4.2 配置舉例2
1 location = /index.htm {
2
3 root /www/wwwroot/test.php7.isee.wang/html/;
4
5 index index.htm index.html;
6
7 }
訪問test.php7.isee.wang/index.htm
分析:1)這里精確匹配index.htm,這個時候root就發揮作用了,此時會定位到/www/wwwroot/test.php7.isee.wang/html/index.htm
2)為了方便直觀查看,我在做此配置之前就現在對應的index.htm中的內容就是寫的所在目錄。
3)如果匹配不到相應的location,則會繼承整體nginx服務器的配置,即定位到/www/server/nginx/html/index.html
4.3 配置舉例3
location中如果不配置root,就會繼承nginx全局服務器的配置/www/server/nginx/html/index.html
1 #定位1
2 location = /index.htm {
3 root /www/wwwroot/test.php7.isee.wang/html/;
4 index index.htm index.html;
5 }
6
7 #定位2
8 location = / {
9 root /var/www/html/;
10 index index.htm index.html;
11 }
12 #定位3
13 location /index.htm {
14 root html;
15 index index.html index.htm;
16 }
訪問網址:
定位流程見下圖:首先是精確匹配到規則2,然后訪問索引引導頁index.htm,然后精確匹配到規則1
4.4配置舉例4
1 #定位1
2 location = /index.htm {
3 root /www/wwwroot/test.php7.isee.wang/html/;
4 index index.htm index.html;
5 }
6 #定位2
7 location = / {
8 root /var/www/html;
9 index index.html index.htm;
10 }
11 #定位3
12 location /index.htm {
13 root html;
14 index index.html index.htm;
15 }
訪問test.php7.isee.wang
匹配規則:
定位流程:訪問域名->匹配規則2->索引引導頁index.html->匹配規則3
注意:
1、規則3是普通匹配,index.html包含了index.htm這9個字母,所以能匹配上
2、規則3的root對應的html目錄,其全名應該是nginx全局默認目錄:/www/server/nginx/html/
4.5 配置舉例5
1 location = /index.html {
2 root /www/wwwroot/test.php7.isee.wang/html/;
3 index index.htm index.html;
4 }
5
6 location = / {
7 root /var/www/html;
8 index index.html index.htm;
9 }
10
11 location /index.htm {
12 root html;
13 index index.html index.htm;
14 }
訪問 test.php7.isee.wang
匹配規則:
定位流程:訪問域名->匹配規則2->索引引導頁index.html->匹配規則1(因為精確匹配的優先級是最高,因此這里優先匹配規則1,所以訪問的是規則1中root目錄下的index.html)
總結
以上是生活随笔為你收集整理的nginx的location规则(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自动写代码?Google Colab叫板
- 下一篇: Oracle Golden Gate 系