root目錄與alias目錄的區別
Nginx路徑location配置中,使用root目錄與alias目錄的區別
1)alias指定的目錄是准確的,即location匹配訪問的path目錄下的文件直接是在alias目錄下查找的;
2)root指定的目錄是location匹配訪問的path目錄的上一級目錄,這個path目錄一定要是真實存在root指定目錄下的;
1、舉例說明
比如靜態資源文件在服務器/var/www/static/目錄下
1)配置alias目錄
location /static/ {
alias /var/www/static/;
}
注意:alias指定的目錄后面必須要加上"/",即/var/www/static/不能改成/var/www/static
訪問http://IP:PORT/static/index.html時,實際訪問的是/var/www/static/index.html
2)也可改成配置root目錄
location /static/ {
root /var/www/;
}
注意:location中指定的/static/必須是在root指定的/var/www/目錄中真實存在的。
訪問http://IP:PORT/static/index.html時,實際訪問的是/var/www/static/index.html
兩者配置后的訪問效果是一樣的。
2、配置習慣
一般情況下,在nginx配置中的良好習慣是:
1)在location / 中配置root目錄
2)在location /somepath/ 中配置alias虛擬目錄
3、配置默認主頁
比如訪問 http://IP:PORT/,默認訪問服務器/var/www/static/目錄下的index.html
1)配置alias目錄方式
location / {
alias /var/www/static/;
index index.html index.htm;
}
2)配置root目錄方式
location / {
root /var/www/static/;
index index.html index.htm;
}