设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 服务器 > Nginx

Nginx 配置中的 root 与 alias 指令的区别

时间:12-14来源:作者:点击数:
CDSY,CDSY.XYZ

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
  1. alias 是一个目录别名的定义,root 则是最上层目录的定义。
  2. 另一个区别是 alias 后面必须要用 / 结束,否则会找不到文件,而 root 则对 / 可有可无。
  3. 误区:认为 root 是指 /www/test 目录下,而应该是 /www/test/test 目录 。
CDSY,CDSY.XYZ
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐