在作一個項目時要用到nginx,原本配置nginx是一件不太難的事情,結果怎麼都訪問不了 (404都是淚:( )。html
server {
...
location / {
// 就是這裏,alias 與 root配置,後面要 / 結尾,並且windows下也要使用 /,不然報錯
alias D:/xc-nginx/nginx-1.16.1/html/xc-ui-pc-static-portal/;
index index.html index.htm;
}
...複製代碼
但剛開始源文件添加的是 root,因而想探究一下root與alias區別nginx
server {
...
location / {
root D:/xc-nginx/nginx-1.16.1/html/xc-ui-pc-static-portal;
index index.html index.htm;
}
...複製代碼
root和alias都是nginx指定文件路徑的方式web
[root]
語法:root path
默認值:root html
配置段:http、server、location、if
windows
[alias]
語法:alias path
配置段:locationbash
區別在於nginx如何解釋location後面的uri
root的處理結果是:root路徑+location路徑
alias的處理結果是:使用alias路徑替換location路徑
服務器
root實例:ui
1
2
3
|
location /ying/ {
root /www/root/html/;
}
|
若是一個請求的URI是/ying/a.html時,web服務器將會返回服務器上的/www/root/html/ying/a.html的文件。spa
alias實例:code
1
2
3
|
location /ying/ {
alias /www/root/html/new_t/;
}
|
若是一個請求的URI是/ying/a.html時,web服務器將會返回服務器上的/www/root/html/new_t/a.html的文件。cdn
遇到本身不熟悉的技術點屢次折騰無果後應該及時查閱配置,這樣解決問題效率會更高一些。