Detailed explanation of server_name in Nginx virtual host configuration _nginx

Source: Internet
Author: User

The server_name directive can set up a domain based virtual host, depending on the content of the requested header, an IP server can configure multiple domain names. The following server_name parameters are valid:

server_name jb51.net;
server_name jb51.net www.jb51.net;
server_name *.jb51.net;
server_name. jb51.net;

server_name nginx.*;

server_name JB51.NETG 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, or you can use the wildcard "*" to set the name of the virtual host. The above example we see a lot of special places:

The first set of examples first defines server_name as Jb51.net, then requests from Http://jb51.net are sent to that host. The second example is configured with Jb51.net and www.jb51.net, so http://jb51.net and http://www.jb51.net requests are sent to this host.
*.jb51.net and. Jb51.net are equivalent configurations that set up the host to handle all the subdomains from jb51.net, such as Www.jb51.net,blog.jb51.net
The second group of server_name configures nginx.* to configure the server to handle all requests that begin with Nginx. For example, nginx.com,jb51.net,nginx.net,nginx.baidu.com
The next set of first server_name configuration sets the host to handle requests from three domain names. Nginx allows you to set a name that is not a valid domain name. For example, in this configuration we can see three examples that are not valid domain names, localhost,litchfiled and Bledington. Nginx only looks up the domain name in the HTTP header of the request but does not determine whether the domain name is valid, in this case the host names can be configured in/etc/hosts. It is sometimes more appropriate to use a non domain name hostname when you are debugging native.
The last set of examples, server_name, is set to empty double quotes that tell Nginx to catch all requests without hostname, or hostname not specified in other server_name.

To sum up, the order of server_name orders is matched by the following:
1, accurate server_name matching, for example:

server {Listen server_name domain.com www.domain.com;}

2. String to start with wildcard character:

server {Listen server_name. domain.com;.}

3. String ending with wildcard character:

server {Listen server_name www.;.}

4. Matching Regular Expressions:

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

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:

1. Configure multiple sites in one server block:

Copy Code code as follows:

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

The site's home directory should resemble such a structure:
Copy Code code as follows:

/data/wwwsite/domain.com/data/wwwsite/nginx.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.
2. Configure multiple level two domain names for a site in a 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 ~^ (. +)? 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 the site should be as follows:

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

This is/data/wwwsite/domain.com/nginx/when the root directory is/data/wwwsite/domain.com/www/,nginx.domain.com when the www.domain.com is accessed. Analogy
The function of the following if statement is to redirect the domain.com orientation to the www.domain.com, which solves both the home directory access of the Web site and the weighting of the domain name of www.domain.com in the SEO.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.