简体   繁体   English

根据另一个目录的存在设置nginx root

[英]Set nginx root based on existance of another directory

I'm building a system which uses dynamic DNS for user accounts, so you register for a sub domain. 我正在建立一个使用动态DNS进行用户帐户注册的系统,因此您要注册一个子域。

I have a few server directives in place to catch things like api. 我有一些服务器指令来捕获诸如api之类的东西。 and www. 和www。 and other special cases, and I have a directive which reads the wildcard domain name and uses it to set a domain specific assets location. 和其他特殊情况,我有一条指令可以读取通配符域名,并使用它来设置特定于域的资产位置。

server {
  server_name "~^(?<domain>[a-z0-9]+)\.example\.com$";
  root /sites/core;
  location /assets {
    alias /site-assets/$domain;
  }
}

What I want to do is check for the existence of the assets directory and present, on the same domain, a different site. 我要做的是检查资产目录是否存在,并在同一域中显示另一个站点。

To clarify, if the assets directory in the example above exists, serve one set of files, otherwise, serve another. 为了明确起见,如果以上示例中的资产目录存在,则提供一组文件,否则提供另一组文件。 Since the site I want to serve won't be IN the directory in question, I can't use try_files or anything like that. 由于我要提供服务的站点不会在相关目录中,因此我不能使用try_files或类似的东西。

I read all the horror stories about using the if directive but I think I need something like 我阅读了有关使用if指令的所有恐怖故事,但我认为我需要

if (-d /site-assets/$domain) {
  ... Do something
}

Then change the root, but that doesn't work. 然后更改根,但这不起作用。

Any ideas how I can achieve this? 有什么想法可以实现这一目标吗?

OK. 好。 I figured this out. 我想通了。 If there's a problem with it, let me know. 如果有问题,请告诉我。

I have the following in a server directive 我在服务器指令中具有以下内容

server {
    server_name "~^(?<domain>[a-z0-9]+)\.example\.com$";
    set $site core;
    if (!-d /path/to/assets/$domain) {
        set $site join;
    }

    root /path/to/sites/$site;
}

If the assets directory for the given domain name does NOT exist, then I can serve up a joining page for my site, which can pick up the domain name and allow the user to register. 如果给定域名的资产目录不存在,那么我可以为我的站点提供一个加入页面,该页面可以获取域名并允许用户注册。 If it does exist, then serve up the main app. 如果确实存在,则启动主应用程序。

This seems to me like a simple use of the if directive so shouldn't get me in too much trouble. 在我看来,这似乎是if指令的简单用法,因此不会给我带来太多麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM