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