Nginx的一个server中,每个location配置一个资源目录

在配置文件中增加多个location,每个location对应一个项目(资源目录)

   location / 
   {
    
    
         root   yundisk; // 此目录与默认的资源目录 /usr/local/nginx/html/ 是同级目录
         index  index.html index.html;
    }
    
    location /reg/
    {
    
    
         root   register; // 此目录与默认的资源目录 /usr/local/nginx/html/ 是同级目录
         index  register.html;
    }

配置完以后访问。直接访问http://ip/reg提示404

解决:使用alias指定确定的资源目录,(root是指定目录的上级目录),改动后即可正常访问

    location /reg/
    {
    
    
         alias  /usr/local/nginx/register/;
         index  register.html;
    }

root与alias主要区别在于nginx如何解释location后面的路径/指令,这会使两者分别以不同的方式将请求映射到服务器文件上。

root的处理结果是:root路径+location路径;alias的处理结果是:使用alias路径替换location路径 。

alias是一个目录别名的定义,root则是最上层目录的定义。

还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的;而root则可有可无。

猜你喜欢

转载自blog.csdn.net/liangwenhao1108/article/details/109263672
今日推荐