root 和 alias 都可以定义在 location 模块中,都是用来指定请求资源的真实路径,比如:
location /i/ {
root /data/w3;
}
请求 http://foofish.net/i/top.gif 这个网址时,那么在服务器里面对应的真正的资源是 /data/w3/i/top.gif文件,注意真实的路径是root指定的值加上location指定的值。
而 alias 正如其名,alias 指定的路径是 location 的别名,不管 location 的值怎么写,资源的真实路径都是 alias 指定的路径,比如:
location /i/ {
alias /data/w3/images/;
}
同样请求 http://foofish.net/i/top.gif 时,在服务器查找的资源路径是:/data/w3/images/top.gif,也就是说不管 location 的值怎么指定,只要 alias 设置的 /data/w3/images/,就会在 /data/w3/images/ 目录找,比如:
location /i/xxx/ {
alias /data/w3/images/;
}
请求 http://foofish.net/i/xxx/top.gif,查找的资源路径还是 /data/w3/images/top.gif。
Nginx 配置文件 server 中指定两个 location 执行,分别为root 和 alias 指令:
location /test/ {
root /www/test;
}
按照这种配置,则访问 /test/ 目录下的文件时,nginx 会去 /www/test/test/ 目录下找文件:
location /test/ {
alias /www/test/;
}
按照上述配置,则访问 /test/ 目录里面的文件时,nginx 会去 /www/test/ 目录找文件:
http://nginx.org/en/docs/http/ngx_http_core_module.html#root
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias