在子域上配置具有不同根文件夹的多个位置的nginx

sim*_*oes 186 webserver nginx

我希望将子域的根URL和子域的目录提供给我服务器上的两个不同文件夹.这是我拥有的简单设置,并且不起作用......

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在这个例子test.example.com/中将带入索引文件/web/test.example.com/www

并将去test.example.com/static带上索引文件/web/test.example.com/static

fur*_*urq 218

您需要使用该alias指令location /static:

server {

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

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

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

}
Run Code Online (Sandbox Code Playgroud)

nginx的维基解释比我的根,别名更好的区别:

请注意,它看起来可能与root指令类似,但文档根目录不会更改,只是用于请求的文件系统路径.在请求Nginx问题中删除请求的位置部分.

  • 他不需要"别名".请阅读[官方文档](http://nginx.org/r/alias),而不是用户填写的社区维基.Quote:*当location匹配指令值的最后一部分时,最好使用root指令*. (63认同)
  • 这对我有用,除了它缺少一个尾随斜线.别名应为:alias /web/test.example.com/static/; (8认同)
  • @VBart文档确切地说出你引用它们的内容,但它们根本不能证明这种说法 - 这似乎是一种随意的风格选择.你看到它背后的任何逻辑原因吗? (7认同)

小智 97

Location指令系统是

就像你想转发所有开始的请求/static和你的数据/var/www/static

所以一个简单的方法是将最后一个文件夹与完整路径分开,这意味着

完整路径 : /var/www/static

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

location <lastPath> {
    root <FirstPath>;
}
Run Code Online (Sandbox Code Playgroud)

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

你的错误:

location /static {
    root /web/test.example.com/static;
}
Run Code Online (Sandbox Code Playgroud)

您的解决方案

location /static {
    root /web/test.example.com;
}
Run Code Online (Sandbox Code Playgroud)

  • 这似乎对“自由做我想做的”有相当严格的限制。我希望从某个路径开始的URI从其物理文件路径中不包含该URI路径的目录提供。使用此解决方案,我被迫将文档放在磁盘中以“ / static”结尾的路径下。我一点都不喜欢。我想要将文件放到所需位置的绝对和完全的自由。 (5认同)
  • 非常感谢,我正是以这种方式失败:) (3认同)
  • 这帮助了我:使我意识到我需要重命名文件夹或设置符号链接以使事情正常进行。 (2认同)
  • @SzczepanHołyszewski 然后使用“alias”指令 (2认同)

VBa*_*art 46

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

http://nginx.org/r/root

  • @All,no:http://nginx.org/en/docs/http/ngx_http_core_module.html#alias (10认同)
  • @区别在于:`root /web/test.example.com;`而不是`root /web/test.example.com/static;`。nginx将* location *指定的路径映射到目录树,并且由于该路径和源目录共享相同的名称,因此它可以与`root`一起使用。 (3认同)
  • 不鼓励使用这种格式。最好在“server”下有一个父“root”。请参阅 [Nginx 配置陷阱](https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/?highlight=alias#root-inside-location-block)。 (3认同)
  • 问有什么不同? (2认同)

erm*_*off 13

更详细一点的例子。

设置:您有一个网站,example.com并且您有一个网络应用程序example.com/webapp

...
server {
  listen 443 ssl;
  server_name example.com;

  root   /usr/share/nginx/html/website_dir;
  index  index.html index.htm;
  try_files $uri $uri/ /index.html;

  location /webapp/ {
    alias  /usr/share/nginx/html/webapp_dir/;
    index  index.html index.htm;
    try_files $uri $uri/ /webapp/index.html;
  }
}
...
Run Code Online (Sandbox Code Playgroud)

我是故意命名webapp_dirwebsite_dir。如果您有匹配的名称和文件夹,则可以使用该root指令。

此设置有效并通过 Docker 进行了测试。

注意!小心斜线。完全按照示例中的方式放置它们。