以前只知道Nginx的location块中的root用法,用起来总是感觉满足不了自己的一些想法。然后终于发现了alias这个东西。
先看root的用法
location /request_path/image/ {
root /local_path/image/;
}
- 1
- 2
- 3
这样配置的结果就是当客户端请求 /request_path/image/cat.png 的时候,
Nginx把请求映射为/local_path/image/request_path/image/cat.png
再看alias的用法
location /request_path/image/ {
alias /local_path/image/;
}
- 1
- 2
- 3
这时候,当客户端请求 /request_path/image/cat.png 的时候,
Nginx把请求映射为/local_path/image/cat.png
对比root就可以知道怎么用了吧~~ :)
root的处理结果是:root路径+location路径
alias的处理结果是:使用alias路径替换location路径
还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的。。。而root则可有可无~~