Server names服务器名称

Wildcard names通配符名称
Regular expressions names正则表达式名称
Miscellaneous names杂项名称
Internationalized names国际化名称
Optimization优化
Compatibility兼容性

Server names are defined using the server_name directive and determine which server block is used for a given request. 服务器名称是使用server_name指令定义的,并确定用于给定请求的服务器块。See also “How nginx processes a request”. 另请参见“nginx如何处理请求”。They may be defined using exact names, wildcard names, or regular expressions:可以使用精确名称、通配符名称或正则表达式定义它们:

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

server {
listen 80;
server_name *.example.org;
...
}

server {
listen 80;
server_name mail.*;
...
}

server {
listen 80;
server_name ~^(?<user>.+)\.example\.net$;
...
}

When searching for a virtual server by name, if name matches more than one of the specified variants, e.g. both wildcard name and regular expression match, the first matching variant will be chosen, in the following order of precedence:按名称搜索虚拟服务器时,如果名称匹配多个指定变量,例如通配符名称和正则表达式匹配,则将按照以下优先顺序选择第一个匹配变量:

  1. exact name确切名称
  2. longest wildcard name starting with an asterisk, e.g. “*.example.org以星号开头的最长通配符名称,例如“*.example.org
  3. longest wildcard name ending with an asterisk, e.g. “mail.*以星号结尾的最长通配符名称,例如“mail.*
  4. first matching regular expression (in order of appearance in a configuration file)第一个匹配的正则表达式(按照在配置文件中的出现顺序)

Wildcard names通配符名称

A wildcard name may contain an asterisk only on the name’s start or end, and only on a dot border. 通配符名称只能在名称的开始或结束处包含星号,并且只能在点边框上包含星号。The names “www.*.example.org” and “w*.example.org” are invalid. 名称“www.*.example.org”和“w.*.example.org”无效。However, these names can be specified using regular expressions, for example, “~^www\..+\.example\.org$” and “~^w.*\.example\.org$”. 但是,可以使用正则表达式指定这些名称,例如“~^www\..+\.example\.org$”和“~^w.*\.example\.org$”。An asterisk can match several name parts. 星号可以匹配多个名称部分。The name “*.example.org” matches not only www.example.org but www.sub.example.org as well.名称“*.example.org”不仅与www.example.org匹配,还与www.sub.example.org匹配。

A special wildcard name in the form “.example.org” can be used to match both the exact name “example.org” and the wildcard name “*.example.org”.格式为“.example.org”的特殊通配符名称可用于匹配确切名称“example.org”和通配符名称“*.example.org”。

Regular expressions names正则表达式名称

The regular expressions used by nginx are compatible with those used by the Perl programming language (PCRE). nginx使用的正则表达式与Perl编程语言(PCRE)使用的正则表达式兼容。To use a regular expression, the server name must start with the tilde character:若要使用正则表达式,服务器名称必须以波浪号字符开头:

server_name  ~^www\d+\.example\.net$;

otherwise it will be treated as an exact name, or if the expression contains an asterisk, as a wildcard name (and most likely as an invalid one). 否则,它将被视为精确名称,或者如果表达式包含星号,则将被视为通配符名称(很可能是无效名称)。Do not forget to set “^” and “$” anchors. They are not required syntactically, but logically. Also note that domain name dots should be escaped with a backslash. A regular expression containing the characters “{” and “}” should be quoted:

server_name  "~^(?<name>\w\d{1,3}+)\.example\.net$";

otherwise nginx will fail to start and display the error message:否则nginx将无法启动并显示错误消息:

directive "server_name" is not terminated by ";" in ...

A named regular expression capture can be used later as a variable:命名正则表达式捕获稍后可以用作变量:

server {
    server_name   ~^(www\.)?(?<domain>.+)$;

    location / {
        root   /sites/$domain;
    }
}

The PCRE library supports named captures using the following syntax:PCRE库支持使用以下语法的命名捕获:

?<name> Perl 5.10 compatible syntax, supported since PCRE-7.0Perl 5.10兼容语法,从PCRE-7.0开始支持
?'name' Perl 5.10 compatible syntax, supported since PCRE-7.0Perl 5.10兼容语法,从PCRE-7.0开始支持
?P<name> Python compatible syntax, supported since PCRE-4.0Python兼容语法,从PCRE-4.0开始支持
If nginx fails to start and displays the error message:如果nginx无法启动并显示错误消息:

pcre_compile() failed: unrecognized character after (?< in ...

this means that the PCRE library is old and the syntax “?P<name>” should be tried instead. 这意味着PCRE库很旧,语法为“?P<name>”应该试一试。The captures can also be used in digital form:捕获也可以以数字形式使用:

server {
    server_name   ~^(www\.)?(.+)$;

    location / {
        root   /sites/$2;
    }
}

However, such usage should be limited to simple cases (like the above), since the digital references can easily be overwritten.然而,这种用法应限于简单的情况(如上所述),因为数字参考很容易被覆盖。

Miscellaneous names杂项名称

There are some server names that are treated specially.有些服务器名称是经过特殊处理的。

If it is required to process requests without the “Host” header field in a server block which is not the default, an empty name should be specified:如果需要在服务器块(不是默认值)中处理不带“主机”标头字段的请求,则应指定空名称:

server {
    listen       80;
    server_name  example.org  www.example.org  "";
    ... }

If no server_name is defined in a server block then nginx uses the empty name as the server name.如果服务器块中未定义server_name,则nginx使用空名称作为服务器名称。

nginx versions up to 0.8.48 used the machine’s hostname as the server name in this case.在本例中,0.8.48之前的nginx版本使用机器的主机名作为服务器名称。

If a server name is defined as “$hostname” (0.9.4), the machine’s hostname is used.如果服务器名称定义为“$hostname”(0.9.4),则使用机器的主机名。

If someone makes a request using an IP address instead of a server name, the “Host” request header field will contain the IP address and the request can be handled using the IP address as the server name:如果有人使用IP地址而不是服务器名称发出请求,“主机”请求标头字段将包含IP地址,并且可以使用IP地址作为服务器名称来处理请求:

server {
    listen       80;
    server_name  example.org                  www.example.org                  ""
                 192.168.1.1 ;
    ... }

In catch-all server examples the strange name “_” can be seen:

server {
    listen       80  default_server;
    server_name  _;
    return       444;
}

There is nothing special about this name, it is just one of a myriad of invalid domain names which never intersect with any real name. 这个名字并没有什么特别之处,它只是众多无效域名中的一个,这些域名从未与任何真实名称相交。Other invalid names like “--” and “!@#” may equally be used.其他无效名称,如“--”和“!@#”可以同样使用。

nginx versions up to 0.6.25 supported the special name “*” which was erroneously interpreted to be a catch-all name. 0.6.25之前的nginx版本支持特殊名称“*”,该名称被错误地解释为一个全面的名称。It never functioned as a catch-all or wildcard server name. 它从来没有作为一个全包或通配符服务器名发挥作用。Instead, it supplied the functionality that is now provided by the server_name_in_redirect directive. 相反,它提供了现在由server_name_in_redirect指令提供的功能。The special name “*” is now deprecated and the server_name_in_redirect directive should be used. 现在不推荐使用特殊名称“*”,应该使用server_name_in_redirect指令。Note that there is no way to specify the catch-all name or the default server using the server_name directive. 请注意,无法使用server_name指令指定catch-all名称或默认服务器。This is a property of the listen directive and not of the server_name directive. 这是listen指令的属性,而不是server_name指令的属性。See also “How nginx processes a request”. 另请参见“nginx如何处理请求”。It is possible to define servers listening on ports *:80 and *:8080, and direct that one will be the default server for port *:8080, while the other will be the default for port *:80:可以定义在端口*:80和*:8080上侦听的服务器,并指示其中一个将是端口*:8080的默认服务器,而另一个将是端口*:80的默认服务器:

server {
    listen       80;
    listen       8080  default_server;
    server_name  example.net;
    ... }

server {
    listen       80  default_server;
    listen       8080;
    server_name  example.org;
    ... }

Internationalized names国际化名称

Internationalized domain names (IDNs) should be specified using an ASCII (Punycode) representation in the server_name directive:国际化域名(IDN)应使用server_name指令中的ASCII(Punycode)表示形式指定:

server {
    listen       80;
    server_name  xn--e1afmkfd.xn--80akhbyknj4f;  # пример.испытание
    ... }

Optimization优化

Exact names, wildcard names starting with an asterisk, and wildcard names ending with an asterisk are stored in three hash tables bound to the listen ports. 精确名称、以星号开头的通配符名称和以星号结尾的通配符名称存储在绑定到侦听端口的三个哈希表中。The sizes of hash tables are optimized at the configuration phase so that a name can be found with the fewest CPU cache misses. 哈希表的大小在配置阶段进行了优化,以便在CPU缓存未命中最少的情况下找到名称。The details of setting up hash tables are provided in a separate document.设置哈希表的详细信息在单独的文档中提供。

The exact names hash table is searched first. 首先搜索确切的名称哈希表。If a name is not found, the hash table with wildcard names starting with an asterisk is searched. 如果未找到名称,将搜索以星号开头的通配符名称的哈希表。If the name is not found there, the hash table with wildcard names ending with an asterisk is searched.如果在那里找不到名称,将搜索以星号结尾的通配符名称的哈希表。

Searching wildcard names hash table is slower than searching exact names hash table because names are searched by domain parts. 搜索通配符名称哈希表比搜索精确名称哈希表慢,因为名称是由域部分搜索的。Note that the special wildcard form “.example.org” is stored in a wildcard names hash table and not in an exact names hash table.请注意,特殊的通配符格式“.example.org”存储在通配符名称哈希表中,而不是精确名称哈希表中。

Regular expressions are tested sequentially and therefore are the slowest method and are non-scalable.正则表达式是按顺序测试的,因此是最慢的方法,并且不可伸缩。

For these reasons, it is better to use exact names where possible. 出于这些原因,最好尽可能使用准确的名称。For example, if the most frequently requested names of a server are example.org and www.example.org, it is more efficient to define them explicitly:例如,如果最常请求的服务器名称是example.orgwww.example.org,则显式定义它们更有效:

server {
    listen       80;
    server_name  example.org  www.example.org  *.example.org;
    ... }

than to use the simplified form:而不是使用简化形式:

server {
    listen       80;
    server_name  .example.org;
    ... }

If a large number of server names are defined, or unusually long server names are defined, tuning the server_names_hash_max_size and server_names_hash_bucket_size directives at the http level may become necessary. 如果定义了大量服务器名称,或定义了异常长的服务器名称,则可能需要在http级别调整server_names_hash_max_sizeserver_names_hash_bucket_size指令。The default value of the server_names_hash_bucket_size directive may be equal to 32, or 64, or another value, depending on CPU cache line size. server_names_hash_bucket_size指令的默认值可能等于32、64或其他值,具体取决于CPU缓存线大小。If the default value is 32 and server name is defined as “too.long.server.name.example.org” then nginx will fail to start and display the error message:如果默认值为32,且服务器名称定义为“too.long.server.name.example.org”,则nginx将无法启动并显示错误消息:

could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32

In this case, the directive value should be increased to the next power of two:在这种情况下,指令值应增加到下一个二次方:

http {
    server_names_hash_bucket_size  64;
    ...

If a large number of server names are defined, another error message will appear:如果定义了大量服务器名称,将显示另一条错误消息:

could not build the server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 32

In such a case, first try to set server_names_hash_max_size to a number close to the number of server names. 在这种情况下,首先尝试将server_names_hash_max_size设置为接近服务器名称数量的数字。Only if this does not help, or if nginx’s start time is unacceptably long, try to increase server_names_hash_bucket_size.只有在这样做没有帮助的情况下,或者如果nginx的启动时间太长,那么请尝试增加server_names_hash_bucket_size

If a server is the only server for a listen port, then nginx will not test server names at all (and will not build the hash tables for the listen port). 如果服务器是侦听端口的唯一服务器,那么nginx根本不会测试服务器名称(也不会为侦听端口构建哈希表)。However, there is one exception. 然而,有一个例外。If a server name is a regular expression with captures, then nginx has to execute the expression to get the captures.如果服务器名称是带有捕获的正则表达式,那么nginx必须执行该表达式才能获取捕获。

Compatibility兼容性

written by Igor Sysoev
edited by Brian Mercer