Nginx的root和alias

nginx指定文件路徑有兩種方式rootalias

root

語法:  root path;

默認值:root html;

配置段:http、server、location、if

alias

語法:  alias path;

配置段:location

root與alias主要區(qū)別在于nginx如何解釋location后面的uri,這會使兩者分別以不同的方式將請求映射到服務器文件上。

root的處理結(jié)果是:root路徑+location路徑
alias的處理結(jié)果是:使用alias路徑替換location路徑
alias是一個目錄別名的定義,root則是最上層目錄的定義。還有一個重要的區(qū)別是alias后面必須要用"/"結(jié)束,否則會找不到文件的。。。而root則可有可無

下面我們來做個試驗:

我的目錄結(jié)構(gòu)

└── workspace
    ├── dir
    └── www
        └── test
            ├── html
            │   └── a.html     //文件內(nèi)容  I am a html file in html
            └── otherhtml
                └── a.html     //文件內(nèi)容  I am a html file in otherhtml
          

nginx配置root實例

server {
    listen 80;
    server_name test.html.com;
 
    location ^~ /test/html/ {
        root   /workspace/www;
    }
}
http://test.html.com/test/html/a.html       //I am a html file in html

nginx配置alias實例

server {
    listen 80;
    server_name test.html.com;
    
    location  ^~ /otherhtml/ {
        alias /workspace/www/test/otherhtml/;
    }
}

Otherhtml 后面的/千萬不能省略

http://test.html.com/otherhtml/a.html       //I am a html file in otherhtml
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容