-
通過前一篇博客我們已經瞭解,在nginx中,我們可以通過location塊與root指令結合的方式,將"url"與"服務器路徑"建立起對應的關係,location塊負責匹配url,root指令負責將匹配到的url與服務器中的某個具體的目錄對應起來。
-
其實,location塊匹配到對應的url以後,還能幹很多別的事情,當location塊匹配到url以後具體執行什麼操作,取決於你在location塊中配置了哪些指令。
-
location其實是server的⼀個指令,爲nginx服務器提供⽐較多⽽且靈活的指令,都是在location中提現的,主要是基於nginx接受到的請求字符串,對⽤⼾請求的UIL進⾏匹配,並對特定的指令進⾏處理,包括地址重定向、數據緩存和應答控制等功能都是在這部分實現,另外很多第三⽅模塊的配置也是在location模塊中配置。
-
關於location可能還需要單獨寫一篇文章,這裏暫時放一放,本篇文章主要說的時怎樣把url與服務器路徑建立起對應的關係,既然要聊的話題是要將url和路徑建立起關聯關係,不如就從root指令開始聊起,在前文中我們已經初步接觸過root指令,但是並沒有詳細的描述過怎樣使用root指令,那麼這篇文章我們就來通過一些示例來演示怎樣使用root指令,其實,除了root指令,還有另一個指令也能實現類似的功能,他就是alias指令,root指令和alias指令能將url和服務器路徑進行對應,但是它們之間又存在一些區別,我們這就聊聊它們。
-
我們先從root指令開始,我們先看一個root指令的示例,如下:
grep -v "#" /apps/nginx/conf/nginx.conf | grep -v "^$"
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
index index.html index.htm;
location / {
root html;
}
location /demo{ #看這裏
root /opt/root_test; #看這裏
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
- 上例中,location塊匹配到的url爲"/demo",root指令的路徑爲"/opt/root_test",那麼,根據上述配置,當我們訪問"/demo"這個url的時候,實際上訪問的到底是服務器中的那個路徑呢?答案爲"/opt/root_test/demo"路徑,怎麼得到的答案呢?方法其實很簡單,我們只需要將location中的rul添加到root指令對應的路徑後面即可,得到最終服務器訪問的路徑,具體方法如下圖所示:
-
也就是說,上述配置表示,當我們訪問"/demo"這個url時,實際訪問的時服務器的"/opt/root_test/demo"目錄,那麼,讓我們來驗證一下,看看實際情況如何,首先,我們先創建"/opt/root_test/demo"目錄,並在此目錄中放置一張示例圖片以便演示,示例圖片爲ms.jpg,我已經在默認的的server塊中配置了上圖的location,然後重載配置。
-
瀏覽器訪問http://172.20.26.104/demo/ms.jpg最終效果如下:
- 通過上圖可以看到,當我們訪問"/demo/ms.jpg"時,訪問的其實就是"/opt/root_test/demo/ms.jpg"
grep -v "#" /apps/nginx/conf/nginx.conf | grep -v "^$"
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
index index.html index.htm;
location / {
root html;
}
location /demo{
root /opt/root_test;
}
location /demo/test{ #看這裏
root /opt/root_test; #看這裏
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
-
通過上面的演示,你肯定已經明白了location和root結合後建立了怎樣的對應關係,那麼,我們來舉一反三試試,配置上述location塊之後我們訪問"/demo/test/ms.jpg"這個url時,我們訪問的是哪個目錄的文件呢?你肯定已經猜到了答案,答案就是"/opt/root_test/demo/test/ms.jpg"
-
瀏覽器訪問http://172.20.26.104/demo/test/ms.jpg最終效果如下:
- 剛纔提到過,除了root指令,alias指令也能將url與服務器路徑進行關聯,那麼我們來看一個alias指令的小示例,如下:
grep -v "#" /apps/nginx/conf/nginx.conf | grep -v "^$"
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
index index.html index.htm;
location / {
root html;
}
location /demo{
root /opt/root_test;
}
location /demo/test{
root /opt/root_test;
}
location /demo1{ #看這裏
alias /opt/alias_test; #看這裏
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
-
如你所見,alias指令對應的值也是一個路徑,當alias指令與location塊結合時,它們會怎樣建立url與服務器路徑的對應關係呢?答案是:上面的配置表示,當我們訪問"/demo1/ms.jpg"時,其實我們訪問的是服務器的"/opt/alias_test/ms.jpg",也就是說,當我們使用alias時,location的url是與alias的路徑完全相等的。
-
瀏覽器訪問http://172.20.26.104/demo1/ms.jpg最終效果如下:
-
看到此處,root指令和alias指令的區別就很明顯了。
-
root指令會在location塊的"url路徑"帶入到"root指令路徑"後,將帶入後的路徑作爲"最終路徑",使用"最終路徑"與url建立對應的關係。
-
alias指令則是直接將location塊的"url路徑"與"alias指令路徑"建立對應關係。
-
其實,除了上述區別,alias指令和root指令能夠處於的上下文位置也不同,查看nginx官方手冊(查看官方文檔的方法已經上一篇介紹了,可以用於參考),可以看下alias指令和root指令的語法說明如下:
-
root
- alias
- 從官網信息可以看出,alias指令只能在location塊中使用,而root指令則不然。
以上就是root指令和alias指令之間區別簡單的描述,希望對閱讀文章的你有所幫助😉 |
---|