For the bulk of the need to add a virtual host, Apache has Vhost_alias module to help. What about Nginx? In fact, the horse does not need, the same can be achieved Nginx two-level domain name matching subfolders, and does not match the "www", but can match the inclusion of "www" subdomain.
First look at the current online search methods.
Copy Code code as follows:
if ($host ~* (. *) \. (. *)\. (.*)) {
Set $subdomain $;
}
Location/{
Root html/$subdomain;
Index index.html index.php;
}
The above "~*" means not case-sensitive, and then matches any "xxx.xxx.xxx" type of URL, and finally a tragedy, not only to match the "www.yourdomian.com" or even "I 艹. $!@.com" Such URLs also match.
Oh, yes! This is a regular question. It is recommended that you read the regular expression 30-minute introductory tutorial, the Nginx Location instruction basics, and then look down ...
Perfect solution
Copy Code code as follows:
if ($host ~* (\b) (?! www\b) (\w+) \.\w+\.\w+) {
Set $subdomain/$1;
}
Location/{
Root/home/wangyan/public_html$subdomain;
Index index.html index.php;
}
effect, the following figure can be seen, has been implemented, does not match the "www" but can match the inclusion of "www" subdomain.
Use the method, copy the code above to the server {} tab, and then restart the nginx.