How does nginx load the configuration dynamically?

Let"s start with why you need to load the configuration dynamically:

We are working on a multi-site service. For example, user username1 owns the site https://username1.example.com. Now I have solved this step by username under the folder with the same name:

   if ($http_host ~* "^(.*)\.example\.com$") {
        set $root /var/html/subdomains/$1;
    }

    root $root;

as you can see above, all the subsites are under the / var/html/subdomains folder.

but there is a new problem. My subdomain name system requires support for cname resolution, that is, the subdomain https://username1.example.com can be opened through the user"s own registered domain name https://username1-domain.com.

therefore, users can parse username1-domain.com to username1.example.com through cname, but this is a user behavior, so it is necessary to dynamically resolve the request of username1-domain.com to the server configuration of / var/html/subdomains/username1 in the system, and it is best to provide https support, that is, users upload their own https certificate in the background and automatically configure it to the configuration of nginx. So that https://username1-domain.com can be accessed normally.

at this time, the domain name registered by the user has no rules, and the folder cannot be mapped through the above code block.

so what should I do if the above dynamic resolves any domain name to a folder on the server?

Thank you.

Aug.30,2021

use the nginx lua module


Nginx can't do this on its own (I'm talking about Nginx Community Edition, which is supported by Enterprise Nginx-plus). Apache has a .htaccess file for dynamic configuration, but Nginx does not have this setting.

but Nginx can configure hot loading through reload operations, with almost no business interruption. So you can actually achieve this requirement in disguise through the configuration management + reload solution. Without strict dynamic support, lua can consider (for example, switching to an openresty project)

Menu