root目录与alias目录的区别 Nginx路径location配置中,使用root目录与alias目录的区别

   1)alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alias目录下查找的;
    2)root指定的目录是location匹配访问的path目录的上一级目录,这个path目录一定要是真实存在root指定目录下的;

举例说明

    比如静态资源文件在服务器/var/www/static/目录下

    1)配置alias目录

    location /static/ {
        alias  /var/www/static/;
    }

    注意:alias指定的目录后面必须要加上"/",即/var/www/static/不能改成/var/www/static
    
    访问http://IP:PORT/static/index.html时,实际访问的是/var/www/static/index.html
    
    2)也可改成配置root目录

    location /static/ {
        root  /var/www/;
    }

    注意:location中指定的/static/必须是在root指定的/var/www/目录中真实存在的。
      
    两者配置后的访问效果是一样的。 

配置习惯  
  
    一般情况下,在nginx配置中的良好习惯是:
    1)在location / 中配置root目录
    2)在location /somepath/ 中配置alias虚拟目录
    
配置默认主页

    比如访问 http://IP:PORT/,默认访问服务器/var/www/static/目录下的index.html
    
    1)配置alias目录方式
    
    location / {
        alias  /var/www/static/;
        index  index.html index.htm;
    }
    
    2)配置root目录方式
    
    location / {
        root  /var/www/static/;
        index  index.html index.htm;
    }
 

猜你喜欢

转载自blog.csdn.net/qq_36826506/article/details/83110800
今日推荐