nginx指定文件路徑有兩種方式root和alias
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