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

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

WBOY
Release: 2023-05-12 20:25:04
forward
1204 people have browsed it

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template