The Web server is Nginx and you want to configure wildcard domain name resolution. In fact, it is not difficult to configure wildcard domain name resolution with slight modifications.
The primary domain name is not affected. The root of domain.com and www.domain.com is still the www directory.
The root of subdomain sub.domain.com is expected to be placed in the www-sub directory.
Solution and analysis
Make the following configuration in the Nginx configuration file ):
Server {
Server_name
Domain.com
Www.domain.com
* .Domain.com
;
Set $ subdomain '';
If ($ host ~ * (B (?! Wwwb). +) .domain.com ){
Set $ subdomain-$1;
}
Root/home/user/www $ subdomain /;
}
Explanation:
First, use the wildcard * when defining server_name to enable Nginx to accept access from any subdomain name.
Then, analyze $ host and find the sub-domain name. This regular expression is copied on the Internet to match the sub-domain name without www and access without www. The preceding set is because Nginx's If has no Else, so $ subdomain is left empty by default.
Finally, use the $ subdomain variable when defining root.
Now that the wildcard resolution configuration is successful, we can configure many second-level domain names on the server.