2017-09-13 53 views
0

我想要做的是使用nginx让它在我的文件系统上的不同位置使用两个不同的URL来服务两个不同的目录。所以,给我的文件系统/ path/to/dir1和/ path/to/dir2上的两个目录,我希望我的网站上的用户能够访问mysite/d1和mysite/d2,并让每个URL都服务于dir1和dir 2分别。这是我曾尝试:Nginx为2个不同的目录服务2个不同的目录

server { 

     listen  80; 

     location /d1/ { 
      root /path/to/dir1; 
      autoindex on; 
     } 

     location /d2/ { 
      root /path/to/dir2; 
      autoindex on; 
     } 

    } 

我有点困惑,为什么这是行不通的,因为当我使用config

server { 

    listen  80; 

    location/{ 
     root /path/to/dir1; 
     autoindex on; 
    } 

} 

,并导航到mysite的预期/我可以访问DIR1

回答

0

问题是追加请求的URI,当你使用root

location /d1/ { 
     root /path/to/dir1; 

这意味着你要搜索对于/path/to/dir1/d1/中的文件。所以你需要的是一个别名,因为在别名的情况下request_uri只在声明的位置后被采用

server { 

     listen  80; 

     location /d1/ { 
      alias /path/to/dir1; 
      autoindex on; 
     } 

     location /d2/ { 
      alias /path/to/dir2; 
      autoindex on; 
     } 

    }