提问者:小点点

在子域上使用不同根文件夹的多个位置配置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;
    }
}

在这个例子中,转到test.example.com/会将索引文件放在/web/test.example.com/www

转到test.example.com/static会将索引文件放在/web/test.example.com/static


共3个答案

匿名用户

您需要使用alias指令来 /static位置:

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比我更好地解释了root和别名之间的区别:

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

请注意,root别名处理尾随斜杠的方式不同。

匿名用户

位置指令系统是

就像你想转发所有以/sight开头的请求以及你在/var/www/sight中的数据一样

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

完整路径:/var/www/静态

最后路径:/静态和第一路径:/var/www

location <lastPath> {
    root <FirstPath>;
}

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

你的错误:

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

您的解决方案:

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

匿名用户

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;
    }
}

https://nginx.org/en/docs/http/ngx_http_core_module.html#root