Home  >  Article  >  Operation and Maintenance  >  How to add second-level subdomain names in batches with Nginx

How to add second-level subdomain names in batches with Nginx

WBOY
WBOYforward
2023-05-12 20:25:041138browse

Copy code The code is as follows:

if ( $host ~* (.*)\.(.*)\.(.*)) {
set $subdomain $1;
}
location / {
root html/$subdomain;
index index.html index.php;
}

The "~*" above means that it is not case sensitive, and then it is Matching any "xxx.xxx.xxx" type of URL, in the end it is a tragedy, not only matching "www.yourdomian.com" but even URLs such as "What the fuck.$!@.com" are also matched.

Well, that’s right! This is a regular question. It is recommended to read "" and "nginx location command basics" first, and then read on...

Perfect solution

Copy code The code is 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;
}

The effect can be seen in the picture below. It has been implemented and does not match "www" but it can Matches subdomains containing "www".

How to add second-level subdomain names in batches with Nginx

To use, please copy the above code to the server {} tag, and then restart nginx.

The above is the detailed content of How to add second-level subdomain names in batches with Nginx. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete