通過nginx訪問靜態(tài)文件配置,均是在server模塊中配置,有兩種方式:
1、alias
通過alias關(guān)鍵字,重定義路徑,如
server{
listen 7001;
server_name 127.0.0.1;
location /file/ {
alias /home/china/areas/;
}
}
此時(shí),通過瀏覽器訪問http://127.0.0.1:7001/file/t.txt,則訪問服務(wù)器的文件是/home/china/areas/t.txt
alias可以使用正則表達(dá)式,如
location ~ ^/test/(\w+).(\w+){ alise /home/china/2/1.2;
}
訪問/test/t.conf,則實(shí)際訪問的是/home/china/conf/t.conf
2、root
通過root關(guān)鍵字,重定義路徑,如
server{
listen 7002;
server_name 127.0.0.1;
location /test/ {
root /home/china/areas/;
}
}
此時(shí),通過瀏覽器訪問http://127.0.0.1:7001/test/t.txt,則訪問服務(wù)器的文件是/home/china/areas/test/t.txt
上述兩種方法均可達(dá)到目的,區(qū)別是它們對(duì)路徑的解析方式不同,alas會(huì)把指定路徑當(dāng)作文件路徑,
而root會(huì)把指定路徑拼接到文件路徑后,再進(jìn)行訪問。