nginx location中root指令和alias指令的区别

nginx location中root指令和alias指令

功能:将url映射为文件路径,以返回静态文件内容

差别:root会将完整的url映射进文件路径中

                  alias只会将location后的url映射到文件路径中

location   /root {

                   root    html;

         }

         当访问为http://192.168.25.35/root/hello.html时

         会将url --> root/hello.html拼接在root指令的值html

         即会在路径html/root/html/hello.html下找资源,如果找到则返回资源内容

         如果找不到则返回404错误

再如

访问http://192.168.25.35/a/b/c.html

"/usr/local/nginx/html/a/b/c.html" failed (2: No such file or directory)

location /alias {

            alias   html;

            index  index.html index.htm;

    }

        

         当访问http://192.168.25.35/alias/a/hello.html时

         会将location后的url---> a/hello.html(注意不包括location后 /alias 路径)拼接在

         alias指令值html后,即html/a/hello.html下找资源

再如

http://192.168.25.35/alias/location/index/

"/usr/local/nginx/html/location/index/index.html" is not found (2: No such file or directory)

http://192.168.25.35/alias/location/index

"/usr/local/nginx/html/location/index" failed (2: No such file or directory)

通过以上报错信息得知,url后有/时,会默认找index.html资源

当url访问目录并以 / 结尾,则nginx默认会返回index指令配的文件内容

当index指定一个不存在的文件,autoindex on时,url访问目录并以 / 结尾时,返回文件目录

location /alias {
  alias html;
  index a.html;
  autoindex on;
}

当访问http://192.168.25.35/alias/时,将返回html下的文件目录结构

原文地址:https://www.cnblogs.com/yaoqingzhuan/p/10890497.html