server { listen 80; server_name domain.com www.domain.com; ... }
server { listen 80; server_name *.domain.com;
... }
server { listen 80; server_name www.*; ... }
server { listen 80; server_name ~^(?.+)\.domain\.com$;
... }
nginx will match the server name in the order of 1, 2, 3, 4, and the search will stop after only one match, so we must distinguish its matching order when using this instruction (similar to the location instruction) .
A very useful function of the server_name directive is that it can use the capture function of regular expressions, which can simplify the configuration file as much as possible. After all, the daily maintenance of the configuration file that is too long is also very inconvenient. Here are 2 specific applications:
1. Configure multiple sites in a server block:
server { listen 80; server_name ~^(www\.)?(.+)$; index index.php index.html; root /data/wwwsite/$2; }
/data/wwwsite/domain.com
/data/wwwsite/nginx.org
/data/wwwsite/baidu.com
/data/wwwsite/google.com
server { listen 80; server_name ~^(.+)?\.domain\.com$;
index index.html; if ($host = domain.com){
rewrite ^ http://www.domain.com permanent;
} root /data/wwwsite/domain.com/$1/;
}
/data/wwwsite/domain.com/www/
/data/wwwsite/domain
.com/nginx/