Nginx Batch Add two level sub domain name perfect scheme _nginx

Source: Internet
Author: User
Tags sub domain subdomain

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.