2012-07-19 124 views
128

与不同的根文件夹中的多个位置,我希望成为一个子域的子域和目录的根URL到我的服务器上的两个不同的文件夹。下面是简单的设置,我有和没有工作...nginx的配置与子域

server { 

    index index.html index.htm; 
    server_name test.example.com; 

    location/{ 
      root /web/test.example.com/www; 
    } 

    location /static { 
      root /web/test.example.com/static; 
    } 
} 

在这个例子中去test.example.com/会带来/web/test.example.com/索引文件WWW

,并打算test.example.com/static将使索引文件中/web/test.example.com/static

回答

156

您需要使用为location /staticalias指令:

server { 

    index index.html; 
    server_name test.example.com; 

    root /web/test.example.com/www; 

    location /static { 
    alias /web/test.example.com/static; 
    } 

} 

nginx wiki解释根和别名更好的区别比我:

注意它可能类似于一见钟情根指令,但文档根目录并没有改变,只是使用的文件系统路径请求。请求的位置部分在请求Nginx问题中被放弃。

+42

他不需要'别名'。请阅读[官方文档](http://nginx.org/r/alias),而不是用户填写的社区wiki。 Quote:*当位置匹配指令值的最后部分时,最好使用root指令*。 – VBart 2012-07-20 16:20:40

+4

这为我工作,除了它缺少一个尾部斜线。别名应为: alias /web/test.example.com/static/; – ajma 2014-02-27 05:29:13

+4

@VBart这些文档确切地说了你引用他们的话,但是他们根本没有证明这个说法是正确的 - 这看起来像是一种任意的风格选择。你看到它背后的逻辑理由吗? – 2015-05-14 09:21:35

35
server { 

    index index.html index.htm; 
    server_name test.example.com; 

    location/{ 
     root /web/test.example.com/www; 
    } 

    location /static { 
     root /web/test.example.com; 
    } 
} 

http://nginx.org/r/root

+6

@全部,无:http://nginx.org/en/docs/http/ngx_http_core_module.html#alias – Athlan 2014-05-14 16:42:49

+0

问:有什么不同? – TangMonk 2015-12-23 16:11:28

+1

@完全不同:'root /web/test.example.com;'而不是'root /web/test.example.com/static;'。nginx将* location *指定的路径映射到diretory树,并且由于路径和源目录共享相同的名称,因此它可以与'root'一起使用。 – rmoestl 2016-04-20 08:35:38

58

的Location指令系统是

就像你希望转发到开始/static和现在你的数据在/var/www/static

因此,一个简单的所有要求方法从完整路径中分离出最后一个文件夹,这意味着

完整路径:/var/www/static

最后一个路径:/static和第一条路径:/var/www

location <lastPath> { 
    root <FirstPath>; 
} 

所以,让我们看看你做了什么错的,什么是您的解决方案

你的错误:

location /static { 
    root /web/test.example.com/static; 
} 

您的解决方案:

location /static { 
    root /web/test.example.com; 
} 
+0

这帮了我的忙:让我意识到我要么重命名我的文件夹,要么设置符号链接来使事情有效。 – cjm 2016-10-19 03:30:26

+1

非常感谢你,我正在以这种方式失败:) – bobmoff 2016-12-05 13:33:55