Details of Nginx virtual host configuration server_name

  • 2020-05-12 06:47:34
  • OfStack

The server_name directive allows you to set up a domain-based virtual host, and one ip server can configure multiple domain names based on the content of the request header. The following server_name parameters are valid:


server_name ofstack.com;
server_name ofstack.com www.ofstack.com;
server_name *.ofstack.com;
server_name .ofstack.com;

server_name nginx.*;

server_name ofstack.comg bucknell.net brackley.org;
server_name localhost litchfield bleddington;

server_name "";

Multiple domain names are separated by Spaces. nginx allows a virtual host to have one or more names. You can also use the wildcard character "*" to set the name of the virtual host. In the above example, we see many special places:

For the first set of examples, define server_name as ofstack.com, and a request from http:// ofstack.com will be sent to that host. The second example is configured with ofstack.com and www.ofstack.com, so requests for http:// ofstack.com and // www.ofstack.com are sent to this host.
*.ofstack.com and.ofstack.com are equivalent configurations that set the host to handle all subdomains from ofstack.com, such as www.ofstack.com, blog.ofstack.com, etc
Group 2, server_name, configure nginx.*, configure the server to handle all requests beginning with nginx. For example, nginx.com, ofstack.com, nginx.net, nginx.baidu.com
Next, in the first server_name configuration, set the host to handle requests from three domain names. nginx allows setting a name that is not a valid domain name. For example, in the following configuration, we can see three examples that are not valid domain names, localhost,litchfiled and bledington. nginx only looks for the domain name in the requested HTTP header but does not determine whether the domain name is valid. In this case, these hostnames can be configured in /etc/hosts. It is sometimes better to use a non-domain host name when debugging your home.
In the last set of examples, server_name is set to empty double quotation marks, which tell nginx to catch all requests without hostname, or hostname not specified in the other server_name.

In summary 1, the matching order of server_name instruction after receiving the request is as follows:
1. Accurate server_name match, for example:


server { listen 80; server_name domain.com www.domain.com; ...}

2. A string that starts with a wildcard:


server { listen 80; server_name .domain.com;...}

3. A string ending with a wildcard:


server { listen 80; server_name www.; ...}

4. Match regular expressions:


server { listen 80; server_name ~^(?.+).domain.com$;...}

nginx will match server name in the order of 1,2,3,4, and only 1 match will stop the search, so when we use this instruction, 1 must be clear about its matching order (similar to location instruction).
One useful feature of the server_name directive is that you can use the regular expression capture feature to minimize configuration files, which are too long for routine maintenance. Here are two specific applications:

1. Configure multiple sites in one server block:


server { listen 80; server_name ~^(www.)?(.+)$; index index.php index.html; root /data/wwwsite/$2; }

The site's home directory should look something like this:

/data/wwwsite/domain.com/data/wwwsite/nginx.org/data/wwwsite/baidu.com/data/wwwsite/google.com

This allows you to configure multiple sites with just one server block.
2. Configure multiple level 2 domains for one site in one server block.
In the actual website directory structure, we usually create a separate directory for the 2-level domain name of the site. Similarly, we can use regular capture to configure multiple 2-level domain names in 1 server block:


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/;}

The directory structure of your site should look like this:


/data/wwwsite/domain.com/www//data/wwwsite/domain.com/nginx/

Such access www. domain. com root directory to/data wwwsite/domain com/www, nginx. domain. As/when com data/wwwsite/domain com nginx/and so on.
The following if statement redirects the location of domain.com to www.domain.com. This not only addresses home directory access to the site, but also increases the domain weight of www.domain.com in seo.


Related articles: