Mercurial > hg > nginx-site
comparison xml/cn/docs/http/server_names.xml @ 720:9934338f83af
Updated the Chinese documentation.
author | Ruslan Ermilov <ru@nginx.com> |
---|---|
date | Thu, 11 Oct 2012 10:23:05 +0000 |
parents | 130fad6dc1b4 |
children | ff357b676c2e |
comparison
equal
deleted
inserted
replaced
719:6a37df6078a1 | 720:9934338f83af |
---|---|
1 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> | 1 <!-- |
2 Copyright (C) Igor Sysoev | |
3 Copyright (C) Nginx, Inc. | |
4 --> | |
5 | |
6 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> | |
2 | 7 |
3 <article name="虚拟主机名" | 8 <article name="虚拟主机名" |
4 link="/cn/docs/http/server_names.html" | 9 link="/cn/docs/http/server_names.html" |
5 lang="cn" | 10 lang="cn" |
11 rev="2" | |
12 translator="cfsego" | |
6 author="Igor Sysoev" | 13 author="Igor Sysoev" |
7 editor="Brian Mercer"> | 14 editor="Brian Mercer"> |
8 | 15 |
9 | 16 |
10 <section> | 17 <section> |
11 | 18 |
12 <para> | 19 <para> |
13 | 20 |
14 虚拟主机名使用<literal>server_name</literal>指令定义,并用于决定某个请求应由哪台虚拟主机处理。具体请参考《<link doc="request_processing.xml">nginx如何处理一个请求</link>》。虚拟主机名可以使用确切的名字,通配符,或者是正则表达式来定义: | 21 虚拟主机名使用<link doc="ngx_http_core_module.xml" id="server_name"/>指令定义,用于决定由某台<link doc="ngx_http_core_module.xml" id="server">虚拟主机</link>来处理请求。具体请参考《<link doc="request_processing.xml">nginx如何处理一个请求</link>》。虚拟主机名可以使用确切的名字,通配符,或者是正则表达式来定义: |
15 | 22 |
16 <programlisting> | 23 <programlisting> |
17 server { | 24 server { |
18 listen 80; | 25 listen 80; |
19 server_name nginx.org www.nginx.org; | 26 server_name example.org www.example.org; |
20 ... | 27 ... |
21 } | 28 } |
22 | 29 |
23 server { | 30 server { |
24 listen 80; | 31 listen 80; |
25 server_name *.nginx.org; | 32 server_name *.example.org; |
26 ... | 33 ... |
27 } | 34 } |
28 | 35 |
29 server { | 36 server { |
30 listen 80; | 37 listen 80; |
32 ... | 39 ... |
33 } | 40 } |
34 | 41 |
35 server { | 42 server { |
36 listen 80; | 43 listen 80; |
37 server_name ~^(?<user>.+)\.nginx\.net$; | 44 server_name ~^(?<user>.+)\.example\.net$; |
38 ... | 45 ... |
39 } | 46 } |
40 </programlisting> | 47 </programlisting> |
41 | 48 |
42 nginx将以下面顺序检测名字: | 49 nginx以名字查找虚拟主机时,如果名字可以匹配多于一个主机名定义,比如同时匹配了通配符的名字和正则表达式的名字,那么nginx按照下面的优先级别进行查找,并选中第一个匹配的虚拟主机: |
43 | 50 |
44 <list type="enum"> | 51 <list type="enum"> |
45 | 52 |
46 <listitem> | 53 <listitem> |
47 确切的名字; | 54 确切的名字; |
48 </listitem> | 55 </listitem> |
49 | 56 |
50 <listitem> | 57 <listitem> |
51 以星号起始的通配符名字:<literal>*.nginx.org</literal>; | 58 最长的以星号起始的通配符名字:<literal>*.example.org</literal>; |
52 </listitem> | 59 </listitem> |
53 | 60 |
54 <listitem> | 61 <listitem> |
55 以星号结束的通配符名字:<literal>mail.*</literal>; | 62 最长的以星号结束的通配符名字:<literal>mail.*</literal>; |
56 </listitem> | 63 </listitem> |
57 | 64 |
58 <listitem> | 65 <listitem> |
59 正则表达式名字,按在配置文件中出现的顺序遍历。 | 66 第一个匹配的正则表达式名字(按在配置文件中出现的顺序)。 |
60 </listitem> | 67 </listitem> |
61 | 68 |
62 </list> | 69 </list> |
63 一旦匹配成功,停止搜索。 | |
64 </para> | 70 </para> |
65 | 71 |
66 </section> | 72 </section> |
67 | 73 |
68 | 74 |
69 <section id="wildcard_names" | 75 <section id="wildcard_names" |
70 name="通配符名字"> | 76 name="通配符名字"> |
71 | 77 |
72 <para> | 78 <para> |
73 通配符名字只可以在名字的起始处或结尾处包含一个星号,并且星号与其他字符之间用点分隔。所以,<literal>www.*.nginx.org</literal>和<literal>w*.nginx.org</literal>都是非法的。不过,上面的两个名字可以使用正则表达式描述:<literal>~^www\..+\.nginx\.org$</literal>和<literal>~^w.*\.nginx\.org$</literal>。星号可以匹配名字的多个节(各节都是以点号分隔的),比如<literal>*.nginx.org</literal>不仅匹配<literal>www.nginx.org</literal>,也匹配<literal>www.sub.nginx.org</literal>。 | 79 通配符名字只可以在名字的起始处或结尾处包含一个星号,并且星号与其他字符之间用点分隔。所以,“<literal>www.*.example.org</literal>”和“<literal>w*.example.org</literal>”都是非法的。不过,上面的两个名字可以使用正则表达式描述,即“<literal>~^www\..+\.example\.org$</literal>”和“<literal>~^w.*\.example\.org$</literal>”。星号可以匹配名字的多个节(各节都是以点号分隔的)。“<literal>*.example.org</literal>”不仅匹配<literal>www.example.org</literal>,也匹配<literal>www.sub.example.org</literal>。 |
74 </para> | 80 </para> |
75 | 81 |
76 <para> | 82 <para> |
77 有一种形如<literal>.nginx.org</literal>的特殊通配符,它可以既匹配确切的名字<literal>nginx.org</literal>,又可以匹配一般的通配符名字<literal>*.nginx.org</literal>。 | 83 有一种形如“<literal>.example.org</literal>”的特殊通配符,它可以既匹配确切的名字“<literal>example.org</literal>”,又可以匹配一般的通配符名字“<literal>*.example.org</literal>”。 |
78 </para> | 84 </para> |
79 | 85 |
80 </section> | 86 </section> |
81 | 87 |
82 | 88 |
83 <section id="regex_names" | 89 <section id="regex_names" |
84 name="正则表达式名字"> | 90 name="正则表达式名字"> |
85 | 91 |
86 <para> | 92 <para> |
87 nginx使用兼容PCRE的正则表达式。为了使用正则表达式,虚拟主机名必须以波浪线“~”起始: | 93 nginx使用的正则表达式兼容PCRE。为了使用正则表达式,虚拟主机名必须以波浪线“~”起始: |
88 | 94 |
89 <programlisting> | 95 <programlisting> |
90 server_name ~^www\d+\.nginx\.net$; | 96 server_name ~^www\d+\.example\.net$; |
91 </programlisting> | 97 </programlisting> |
92 | 98 |
93 否则该名字会被认为是确切的名字,如果表达式含星号,则会被认为是通配符名字(很可能是一个非法的通配符名字)。不要忘记设置“^”和“$”锚点,语法上不是必须的,但是逻辑上是需要的。同时需要注意的是,域名中的点“.”需要用反斜线“\”转义。正则表达式中含有“{”和“}”,那么正则表达式需要被引用,如: | 99 否则该名字会被认为是个确切的名字,如果表达式含星号,则会被认为是个通配符名字(而且很可能是一个非法的通配符名字)。不要忘记设置“<literal>^</literal>”和“<literal>$</literal>”锚点,语法上它们不是必须的,但是逻辑上是的。同时需要注意的是,域名中的点“.”需要用反斜线“\”转义。含有“<literal>{</literal>”和“<literal>}</literal>”的正则表达式需要被引用,如: |
94 | 100 |
95 <programlisting> | 101 <programlisting> |
96 server_name "~^(?<name>\w\d<b>{</b>1,3<b>}</b>+)\.nginx\.net$"; | 102 server_name "~^(?<name>\w\d<b>{</b>1,3<b>}</b>+)\.example\.net$"; |
97 </programlisting> | 103 </programlisting> |
98 | 104 |
99 否则nginx就不能启动,错误提示是: | 105 否则nginx就不能启动,错误提示是: |
100 | 106 |
101 <programlisting> | 107 <programlisting> |
162 | 168 |
163 <section id="miscellaneous_names" | 169 <section id="miscellaneous_names" |
164 name="其他类型的名字"> | 170 name="其他类型的名字"> |
165 | 171 |
166 <para> | 172 <para> |
167 如果需要用一个非默认的虚拟主机处理请求头中不含“Host”字段的请求,需要指定一个空名字: | 173 有一些主机名会被特别对待。 |
168 | 174 </para> |
169 <programlisting> | 175 |
170 server { | 176 <para> |
171 listen 80; | 177 如果需要用一个非默认的<link doc="ngx_http_core_module.xml" id="server">虚拟主机</link>处理请求头中不含<header>Host</header>字段的请求,需要指定一个空名字: |
172 server_name nginx.org www.nginx.org ""; | 178 |
173 ... | 179 <programlisting> |
174 } | 180 server { |
175 </programlisting> | 181 listen 80; |
176 </para> | 182 server_name example.org www.example.org ""; |
177 | 183 ... |
178 <para> | 184 } |
179 如果server块中没有定义<literal>server_name</literal>,nginx使用空名字作为虚拟主机名。 | 185 </programlisting> |
186 </para> | |
187 | |
188 <para> | |
189 如果<link doc="ngx_http_core_module.xml" id="server"/>块中没有定义<link doc="ngx_http_core_module.xml" id="server_name"/>,nginx使用空名字作为虚拟主机名。 | |
180 <note> | 190 <note> |
181 同样的情况下,nginx 0.8.48版本以下(含)使用<i>本地主机名</i>作为虚拟主机名。 | 191 nginx 0.8.48版本以下(含)在同样的情况下会使用机器名作为虚拟主机名。 |
182 </note> | 192 </note> |
183 </para> | 193 </para> |
184 | 194 |
185 <para> | 195 <para> |
186 如果使用IP地址而不是主机名来请求服务器,那么请求头的“HOST”字段包含的将是IP地址。可以将IP地址作为虚拟主机名来处理这种请求: | 196 如果以“<literal>$hostname</literal>”(nginx 0.9.4及以上版本)定义虚拟主机名,机器名将被使用。 |
197 </para> | |
198 | |
199 <para> | |
200 如果使用IP地址而不是主机名来请求服务器,那么请求头的<header>Host</header>字段包含的将是IP地址。可以将IP地址作为虚拟主机名来处理这种请求: | |
187 | 201 |
188 <programlisting> | 202 <programlisting> |
189 server { | 203 server { |
190 listen 80; | 204 listen 80; |
191 server_name nginx.org | 205 server_name nginx.org |
197 } | 211 } |
198 </programlisting> | 212 </programlisting> |
199 </para> | 213 </para> |
200 | 214 |
201 <para> | 215 <para> |
202 在匹配所有的服务器的例子中,可以见到一个奇怪的名字“_”: | 216 在匹配所有的服务器的例子中,可以见到一个奇怪的名字“<literal>_</literal>”: |
203 | 217 |
204 <programlisting> | 218 <programlisting> |
205 server { | 219 server { |
206 listen 80 default_server; | 220 listen 80 default_server; |
207 server_name _; | 221 server_name _; |
208 return 444; | 222 return 444; |
209 } | 223 } |
210 </programlisting> | 224 </programlisting> |
211 | 225 |
212 这没什么特别的,它只不过是成千上万的与真实的名字毫不相干的非法域名中的一个而已。当然,也可以使用“--”、“!@#”等等。 | 226 这没什么特别的,它只不过是成千上万的与真实的名字绝无冲突的非法域名中的一个而已。当然,也可以使用“<literal>--</literal>”和“<literal>!@#</literal>”等等。 |
213 </para> | 227 </para> |
214 | 228 |
215 <para> | 229 <para> |
216 nginx直到0.6.25版本还支持一个特殊的名字“*”,这个名字一直被错误地理解成是一个匹配所有的名字。但它从来没有像匹配所有的名字,或者通配符那样工作过,而是用来支持一种功能,此功能现在已经改由<literal>server_name_in_redirect</literal>指令提供支持了。所以,现在这个特殊的名字“*”就过时了,应该使用<literal>server_name_in_redirect</literal>指令。需要注意的是,使用<literal>server_name</literal>指令无法描述匹配所有的名字或者默认服务器,需要使用<literal>listen</literal>指令的一个属性来完成这项工作。具体请参考《<link doc="request_processing.xml">nginx如何处理一个请求</link>》。可以定义两个服务器都监听*:80和*:8080端口,然后指定一个作为端口*:8080的默认服务器,另一个作为端口*:80的默认服务器: | 230 nginx直到0.6.25版本还支持一个特殊的名字“<literal>*</literal>”,这个名字一直被错误地理解成是一个匹配所有的名字。但它从来没有像匹配所有的名字,或者通配符那样工作过,而是用来支持一种功能,此功能现在已经改由<link doc="ngx_http_core_module.xml" id="server_name_in_redirect"/>指令提供支持了。所以,现在这个特殊的名字“<literal>*</literal>”已经过时了,应该使用<link doc="ngx_http_core_module.xml" id="server_name_in_redirect"/>指令取代它。需要注意的是,使用<link doc="ngx_http_core_module.xml" id="server_name"/>指令无法描述匹配所有的名字或者默认服务器。这是<link doc="ngx_http_core_module.xml" id="listen"/>指令的属性,而不是<link doc="ngx_http_core_module.xml" id="server_name"/>指令的属性。具体请参考《<link doc="request_processing.xml">nginx如何处理一个请求</link>》。可以定义两个服务器都监听*:80和*:8080端口,然后指定一个作为端口*:8080的默认服务器,另一个作为端口*:80的默认服务器: |
217 | 231 |
218 <programlisting> | 232 <programlisting> |
219 server { | 233 server { |
220 listen 80; | 234 listen 80; |
221 listen 8080 default_server; | 235 listen 8080 default_server; |
222 server_name nginx.net; | 236 server_name example.net; |
223 ... | 237 ... |
224 } | 238 } |
225 | 239 |
226 server { | 240 server { |
227 listen 80 default_server; | 241 listen 80 default_server; |
228 listen 8080; | 242 listen 8080; |
229 server_name nginx.org; | 243 server_name example.org; |
230 ... | 244 ... |
231 } | 245 } |
232 </programlisting> | 246 </programlisting> |
233 </para> | 247 </para> |
234 | 248 |
238 | 252 |
239 <section id="optimization" | 253 <section id="optimization" |
240 name="优化"> | 254 name="优化"> |
241 | 255 |
242 <para> | 256 <para> |
243 确切名字和通配符名字存储在哈希表中。哈希表和监听端口关联,每个端口都最多关联到三张表:确切名字的哈希表,以星号起始的通配符名字的哈希表和以星号结束的通配符名字的哈希表。哈希表的尺寸在配置阶段进行了优化,可以以最小的CPU缓存命中失败来找到名字。nginx首先搜索确切名字的哈希表,如果没有找到,搜索以星号起始的通配符名字的哈希表,如果还是没有找到,继续搜索以星号结束的通配符名字的哈希表。因为名字是按照域名的节来搜索的,所以搜索通配符名字的哈希表比搜索确切名字的哈希表慢。注意<literal>.nginx.org</literal>存储在通配符名字的哈希表中,而不在确切名字的哈希表中。正则表达式是一个一个串行的测试,所以是最慢的,而且不可扩展。 | 257 确切名字和通配符名字存储在哈希表中。哈希表和监听端口关联。哈希表的尺寸在配置阶段进行了优化,可以以最小的CPU缓存命中失败来找到名字。设置哈希表的细节参见<link doc="../hash.xml">这篇文档</link> |
244 </para> | 258 </para> |
245 | 259 |
246 <para> | 260 <para> |
247 鉴于以上原因,请尽可能使用确切的名字。举个例子,如果使用<literal>nginx.org</literal>和<literal>www.nginx.org</literal>来访问服务器是最频繁的,那么将它们明确的定义出来就更为有效: | 261 nginx首先搜索确切名字的哈希表,如果没有找到,搜索以星号起始的通配符名字的哈希表,如果还是没有找到,继续搜索以星号结束的通配符名字的哈希表。 |
248 | 262 </para> |
249 <programlisting> | 263 |
250 server { | 264 <para> |
251 listen 80; | 265 因为名字是按照域名的节来搜索的,所以搜索通配符名字的哈希表比搜索确切名字的哈希表慢。注意特殊的通配符名字“<literal>.example.org</literal>”存储在通配符名字的哈希表中,而不在确切名字的哈希表中。 |
252 server_name nginx.org www.nginx.org *.nginx.org; | 266 </para> |
267 | |
268 <para> | |
269 正则表达式是一个一个串行的测试,所以是最慢的,而且不可扩展。 | |
270 </para> | |
271 | |
272 <para> | |
273 鉴于以上原因,请尽可能使用确切的名字。举个例子,如果使用<literal>example.org</literal>和<literal>www.example.org</literal>来访问服务器是最频繁的,那么将它们明确的定义出来就更为有效: | |
274 | |
275 <programlisting> | |
276 server { | |
277 listen 80; | |
278 server_name example.org www.example.org *.example.org; | |
253 ... | 279 ... |
254 } | 280 } |
255 </programlisting> | 281 </programlisting> |
256 | 282 |
257 下面这种方法相比更简单,但是效率也更低: | 283 下面这种方法相比更简单,但是效率也更低: |
258 | 284 |
259 <programlisting> | 285 <programlisting> |
260 server { | 286 server { |
261 listen 80; | 287 listen 80; |
262 server_name .nginx.org; | 288 server_name .example.org; |
263 ... | 289 ... |
264 } | 290 } |
265 </programlisting> | 291 </programlisting> |
266 </para> | 292 </para> |
267 | 293 |
268 <para> | 294 <para> |
269 如果定义了大量名字,或者定义了非常长的名字,那就需要在<i>http</i>配置块中调整<literal>server_names_hash_max_size</literal>和<literal>server_names_hash_bucket_size</literal>的值。<literal>server_names_hash_bucket_size</literal>的默认值可能是32,或者是64,或者是其他值,取决于CPU的缓存行的长度。如果这个值是32,那么定义“too.long.server.name.nginx.org”作为虚拟主机名就会失败,显示下面错误信息: | 295 如果定义了大量名字,或者定义了非常长的名字,那可能需要在<i>http</i>配置块中使用<link doc="ngx_http_core_module.xml" id="server_names_hash_max_size"/>和<link doc="ngx_http_core_module.xml" id="server_names_hash_bucket_size"/>指令进行调整。<link doc="ngx_http_core_module.xml" id="server_names_hash_bucket_size"/>的默认值可能是32,或者是64,或者是其他值,取决于CPU的缓存行的长度。如果这个值是32,那么定义“<literal>too.long.server.name.example.org</literal>”作为虚拟主机名就会失败,而nginx显示下面错误信息: |
270 <programlisting> | 296 <programlisting> |
271 could not build the server_names_hash, | 297 could not build the server_names_hash, |
272 you should increase server_names_hash_bucket_size: 32 | 298 you should increase server_names_hash_bucket_size: 32 |
273 </programlisting> | 299 </programlisting> |
274 | 300 |
275 出现了这种情况,那就需要将设置值扩大一倍: | 301 出现了这种情况,那就需要将指令的值扩大一倍: |
276 | 302 |
277 <programlisting> | 303 <programlisting> |
278 http { | 304 http { |
279 server_names_hash_bucket_size 64; | 305 server_names_hash_bucket_size 64; |
280 ... | 306 ... |
286 could not build the server_names_hash, | 312 could not build the server_names_hash, |
287 you should increase either server_names_hash_max_size: 512 | 313 you should increase either server_names_hash_max_size: 512 |
288 or server_names_hash_bucket_size: 32 | 314 or server_names_hash_bucket_size: 32 |
289 </programlisting> | 315 </programlisting> |
290 | 316 |
291 那么应该先尝试设置<literal>server_names_hash_max_size</literal>的值差不多等于名字列表的名字总量。如果还不能解决问题,或者服务器启动非常缓慢,再尝试提高<literal>server_names_hash_bucket_size</literal>的值。 | 317 那么应该先尝试设置<link doc="ngx_http_core_module.xml" id="server_names_hash_max_size"/>的值差不多等于名字列表的名字总量。如果还不能解决问题,或者服务器启动非常缓慢,再尝试提高<link doc="ngx_http_core_module.xml" id="server_names_hash_bucket_size"/>的值。 |
292 </para> | 318 </para> |
293 | 319 |
294 <para> | 320 <para> |
295 如果只为一个监听端口配置了唯一的主机,那么nginx就完全不会测试虚拟主机名了(也不会建立那些名字哈希表)。不过,有一个例外,如果<literal>server_name</literal>是一个含有捕获组的正则表达式,这时nginx就不得不执行这个表达式以得到捕获组。 | 321 如果只为一个监听端口配置了唯一的主机,那么nginx就完全不会测试虚拟主机名了(也不会为监听端口建立哈希表)。不过,有一个例外,如果定义的虚拟主机名是一个含有捕获组的正则表达式,这时nginx就不得不执行这个表达式以得到捕获组。 |
296 </para> | 322 </para> |
297 | 323 |
298 </section> | 324 </section> |
299 | 325 |
300 | 326 |
303 | 329 |
304 <para> | 330 <para> |
305 <list type="bullet"> | 331 <list type="bullet"> |
306 | 332 |
307 <listitem> | 333 <listitem> |
308 从0.8.48版本开始,默认的虚拟主机名是空名字“”。 | 334 从0.9.4版本开始,支持特殊的虚拟主机名“<literal>$hostname</literal>”。 |
335 </listitem> | |
336 | |
337 <listitem> | |
338 从0.8.48版本开始,默认的虚拟主机名是空名字“”。 | |
309 </listitem> | 339 </listitem> |
310 | 340 |
311 <listitem> | 341 <listitem> |
312 从0.8.25版本开始,支持虚拟主机名中使用命名的正则表达式捕获组。 | 342 从0.8.25版本开始,支持虚拟主机名中使用命名的正则表达式捕获组。 |
313 </listitem> | 343 </listitem> |
315 <listitem> | 345 <listitem> |
316 从0.7.40版本开始,支持虚拟主机名中使用正则表达式的捕获组。 | 346 从0.7.40版本开始,支持虚拟主机名中使用正则表达式的捕获组。 |
317 </listitem> | 347 </listitem> |
318 | 348 |
319 <listitem> | 349 <listitem> |
320 从0.7.12版本开始,支持空名字“”。 | 350 从0.7.12版本开始,支持空名字“”。 |
321 </listitem> | 351 </listitem> |
322 | 352 |
323 <listitem> | 353 <listitem> |
324 从0.6.25版本开始,通配符和正则表达式名字可以作为第一个虚拟主机名。 | 354 从0.6.25版本开始,通配符和正则表达式名字可以作为第一个虚拟主机名。 |
325 </listitem> | 355 </listitem> |
327 <listitem> | 357 <listitem> |
328 从0.6.7版本开始,支持正则表达式的虚拟主机名。 | 358 从0.6.7版本开始,支持正则表达式的虚拟主机名。 |
329 </listitem> | 359 </listitem> |
330 | 360 |
331 <listitem> | 361 <listitem> |
332 从0.6.0版本开始,支持形如<literal>nginx.*</literal>的通配符名字。 | 362 从0.6.0版本开始,支持形如<literal>example.*</literal>的通配符名字。 |
333 </listitem> | 363 </listitem> |
334 | 364 |
335 <listitem> | 365 <listitem> |
336 从0.3.18版本开始,支持形如<literal>.nginx.org</literal>的特殊通配符名字。 | 366 从0.3.18版本开始,支持形如<literal>.example.org</literal>的特殊通配符名字。 |
337 </listitem> | 367 </listitem> |
338 | 368 |
339 <listitem> | 369 <listitem> |
340 从0.1.13版本开始,支持形如<literal>*.nginx.org</literal>的通配符名字。 | 370 从0.1.13版本开始,支持形如<literal>*.example.org</literal>的通配符名字。 |
341 </listitem> | 371 </listitem> |
342 | 372 |
343 </list> | 373 </list> |
344 </para> | 374 </para> |
345 | 375 |