Nginx root 和 alias 指令的区别

拟墨画扇 提交于 2019-11-28 07:01:53

Nginx root 和 alias 指令的区别

nginx 的 root 和 alias 指令都是用于访问服务器本地文件的,两者区别如下:

  • 配置语法及适用范围
[root]
语法:root path
默认值:root html
配置段:http、server、location、if

[alias]
语法:alias path
配置段:location
  • root 的处理结果是:root 路径+location 路径;
  • alias 的处理结果是:使用 alias 路径替换 location 路径;
  • alias 后面的路径结尾必须是 ‘/’,而 root 可有可无;

举例

当 location 配置为如下时:
location ^~ /documents/ {
    root /var/www-html/documents/;
}

请求:GET /documents/a.js —> 相当于请求本地路径:/var/www-html/documents/documents/a.js

请求:GET /documents/html/index.html —> 相当于请求本地路径:/var/www-html/documents/documents/html/index.html

当 location 配置为如下时:
location ^~ /documents/ {
  alias /var/www-html/documents/;
}

请求:GET /documents/a.js —> 相当于请求本地路径:/var/www-html/documents/a.js

请求:GET /documents/html/index.html —> 相当于请求本地路径:/var/www-html/documents/html/index.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!