Leif160519的blog
本文摘自:https://blog.csdn.net/myNameIssls/article/details/82633117
root和alias都用于指定请求URL的映射路径
alias指向的映射路径便是资源的根目录。
root指向的映射地址需要通过root映射地址+location地址访问。
# 访问http://host/img/0.jpeg,实际的请求地址是/home/resources/a/img/0.jpeg。(注意root映射路径+location路径才是资源的真实路径)
location /img {
root /home/resources/a;
}
# 访问http://host/image/0.jpeg,实际的请求地址是/home/resources/b/0.jpeg
location /image {
alias /home/resources/b;
}
上述配置中分别指定了两个精确匹配路径:/img和/image,/img通过root指定映射路径/home/resources/a,/image通过alias指定映射路径/home/resources/b, 同时我们在/home/resources/a和/home/resources/b两个目录下放同一张图片0.jpeg。
这时候,通过浏览器访问http://host/image/0.jpeg是可以访问到0.jpeg的,即alias指向的映射路径便是资源的根目录。
但是通过
http://host/img/0.jpeg是访问不到0.jpeg的,会报404错误。通过查看nginx的日志文件,发现该请求实际访问的服务器路径是/home/resources/a/img/0.jpeg,即浏览器访问通过root指向的映射地址时,需要拼接location目录,即root映射路径+location路径。
若想通过/img访问0.jpg,只需要将a文件夹改为img或者将location后的img改为a。
“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill
标 题:nginx配置文件中root和alias的区别