Steps for configuring wildcard resolution for sub-domain names in Nginx

Source: Internet
Author: User
Tags regular expression subdomain subdomain name

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.

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.