server_http://www.aliyun.com/zixun/aggregation/11696.html ">name Matching order
The server_name directives in Nginx are primarily used to configure a name-based virtual host, and the order in which server_name orders are received is:
1, accurate server_name matching, for example:
server {Listen server_name howtocn.org www.howtocn.org;}
2. A string starting with the * wildcard character:
server {Listen server_name *.howtocn.org;}
3. The string ending with the * wildcard character:
server {Listen server_name www.*;}
4. Matching Regular Expressions:
server {Listen server_name ~^ (? <www>.+) \.howtocn\.org$;.}
Nginx will match the server name in the order of 1,2,3,4, and only one match will stop the search, so we need to be aware of its matching order (similar to the location instruction) when using this instruction.
A useful feature of the server_name directive is that you can use the capture function of regular expressions to minimize the configuration files, after all, too long configuration files are not easy to maintain daily maintenance. Here are 2 specific applications:
Configuring multiple sites in a single server block
server {Listen server_name ~^ (www\.)? (.+)$; Index index.php index.html; root/data/wwwsite/$2; }
The site's home directory should resemble such a structure:
/data/wwwsite/howtocn.org/data/wwwsite/linuxtone.org/data/wwwsite/baidu.com/data/wwwsite/google.com
This allows you to use only one server block to complete the configuration of multiple sites.
Configure multiple level Two domain names for a site in a single server block
In the actual site directory structure, we usually create a directory for the two-level domain name independently of the site, and we can use regular captures to configure multiple level two domain names in a single server block:
server {Listen server_name ~^ (. +)? \.howtocn\.org$; index index.html; if ($host = howtocn.org) {Rewrite ^ http:// www.howtocn.org permanent; } root/data/wwwsite/howtocn.org/$1/; }
The directory structure of the site should be as follows:
/data/wwwsite/howtocn.org/www//data/wwwsite/howtocn.org/nginx/
This is/data/wwwsite/howtocn.org/nginx/when the root directory is/data/wwwsite/howtocn.org/www/,http://nginx.howtocn.org when the http://www.howtocn.org is accessed, and so on.
The function of the following if statement is to redirect the howtocn.org direction to the http://www.howtocn.org, so as to solve the site's main directory access, but also to increase the SEO in the http://www.howtocn.org domain name weight.
Multiple regular expressions
If you use a regular in server_name, and the following location field uses a regular match, you will not be able to use a reference like $1,$2, which is assigned to a named variable through the set instruction:
server {Listen server_name ~^ (. +)? \.howtocn\.org$; set $www _root $ root/data/wwwsite/howtocn.org/$www _root/; Location ~ *\.php?$ {fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; Fastcgi_param script_filename/data/wwwsite /howtocn.org/$fastcgi _script_name; Include Fastcgi_params; } }