Mercurial > hg > nginx-site
comparison xml/en/docs/http/server_names.xml @ 621:656dfb085020
Unified server names matching description with server_name directive, improved
wording and various typesetting-related changes
author | Vladimir Homutov <vl@nginx.com> |
---|---|
date | Thu, 09 Aug 2012 10:36:48 +0000 |
parents | 2300e4c1a231 |
children | 13f64b371d2c |
comparison
equal
deleted
inserted
replaced
620:32f50b2f9f0c | 621:656dfb085020 |
---|---|
6 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> | 6 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> |
7 | 7 |
8 <article name="Server names" | 8 <article name="Server names" |
9 link="/en/docs/http/server_names.html" | 9 link="/en/docs/http/server_names.html" |
10 lang="en" | 10 lang="en" |
11 rev="1" | 11 rev="2" |
12 author="Igor Sysoev" | 12 author="Igor Sysoev" |
13 editor="Brian Mercer"> | 13 editor="Brian Mercer"> |
14 | 14 |
15 | 15 |
16 <section> | 16 <section> |
17 | 17 |
18 <para> | 18 <para> |
19 Server names are defined using the | 19 Server names are defined using the |
20 <link doc="ngx_http_core_module.xml" id="server_name"/> | 20 <link doc="ngx_http_core_module.xml" id="server_name"/> |
21 directive | 21 directive |
22 and determine which server block is used for a given request. | 22 and determine which <link doc="ngx_http_core_module.xml" id="server"/> block |
23 See also “<link doc="request_processing.xml"/>”. | 23 is used for a given request. |
24 See also “<link doc="request_processing.xml"/>”. | |
24 They may be defined using exact names, wildcard names, or regular expressions: | 25 They may be defined using exact names, wildcard names, or regular expressions: |
25 | 26 |
26 <programlisting> | 27 <programlisting> |
27 server { | 28 server { |
28 listen 80; | 29 listen 80; |
46 listen 80; | 47 listen 80; |
47 server_name ~^(?<user>.+)\.example\.net$; | 48 server_name ~^(?<user>.+)\.example\.net$; |
48 ... | 49 ... |
49 } | 50 } |
50 </programlisting> | 51 </programlisting> |
51 | 52 </para> |
52 The names are tested in the following order: | 53 |
53 | 54 <para> |
55 When searching for a virtual server by name, if name matches more than one of | |
56 the specified variants, e.g. both wildcard name and regular expression match, | |
57 the first matching variant will be chosen, in the following order of precedence: | |
54 <list type="enum"> | 58 <list type="enum"> |
55 | 59 |
56 <listitem> | 60 <listitem> |
57 exact names; | 61 exact name |
58 </listitem> | 62 </listitem> |
59 | 63 |
60 <listitem> | 64 <listitem> |
61 wildcard names starting with an asterisk: <literal>*.example.org</literal> | 65 longest wildcard name starting with an asterisk, e.g. |
62 (longest first); | 66 “<literal>*.example.org</literal>” |
63 </listitem> | 67 </listitem> |
64 | 68 |
65 <listitem> | 69 <listitem> |
66 wildcard names ending with an asterisk: <literal>mail.*</literal> | 70 longest wildcard name ending with an asterisk, e.g. “<literal>mail.*</literal>” |
67 (longest first); | 71 </listitem> |
68 </listitem> | 72 |
69 | 73 <listitem> |
70 <listitem> | 74 first matching regular expression |
71 and regular expressions in the order listed in the configuration file. | 75 (in order of appearance in a configuration file). |
72 </listitem> | 76 </listitem> |
73 | 77 |
74 </list> | 78 </list> |
75 The first match stops the search. | |
76 </para> | 79 </para> |
77 | 80 |
78 </section> | 81 </section> |
79 | 82 |
80 | 83 |
92 The name “<literal>*.example.org</literal>” matches not only | 95 The name “<literal>*.example.org</literal>” matches not only |
93 <literal>www.example.org</literal> but <literal>www.sub.example.org</literal> as well. | 96 <literal>www.example.org</literal> but <literal>www.sub.example.org</literal> as well. |
94 </para> | 97 </para> |
95 | 98 |
96 <para> | 99 <para> |
97 A special wildcard in the form “<literal>.example.org</literal>” can be used | 100 A special wildcard name in the form “<literal>.example.org</literal>” can be |
98 to match both the exact name “<literal>example.org</literal>” | 101 used to match both the exact name “<literal>example.org</literal>” |
99 and the wildcard name “<literal>*.example.org</literal>”. | 102 and the wildcard name “<literal>*.example.org</literal>”. |
100 </para> | 103 </para> |
101 | 104 |
102 </section> | 105 </section> |
103 | 106 |
115 server_name ~^www\d+\.example\.net$; | 118 server_name ~^www\d+\.example\.net$; |
116 </programlisting> | 119 </programlisting> |
117 | 120 |
118 otherwise it will be treated as an exact name, or if the expression contains | 121 otherwise it will be treated as an exact name, or if the expression contains |
119 an asterisk, as a wildcard name (and most likely as an invalid one). | 122 an asterisk, as a wildcard name (and most likely as an invalid one). |
120 Do not forget to set “^” and “$” anchors. | 123 Do not forget to set “<literal>^</literal>” and “<literal>$</literal>” anchors. |
121 They are not required syntactically, but logically. | 124 They are not required syntactically, but logically. |
122 Also note that domain name dots should be escaped with a backslash. | 125 Also note that domain name dots should be escaped with a backslash. |
123 A regular expression containing the characters “{” | 126 A regular expression containing the characters “<literal>{</literal>” |
124 and “}” should be quoted: | 127 and “<literal>}</literal>” should be quoted: |
125 | 128 |
126 <programlisting> | 129 <programlisting> |
127 server_name "~^(?<name>\w\d<b>{</b>1,3<b>}</b>+)\.example\.net$"; | 130 server_name "~^(?<name>\w\d<b>{</b>1,3<b>}</b>+)\.example\.net$"; |
128 </programlisting> | 131 </programlisting> |
129 | 132 |
170 | 173 |
171 <programlisting> | 174 <programlisting> |
172 pcre_compile() failed: unrecognized character after (?< in ... | 175 pcre_compile() failed: unrecognized character after (?< in ... |
173 </programlisting> | 176 </programlisting> |
174 | 177 |
175 this means that the PCRE library is old | 178 this means that the PCRE library is old and the syntax |
176 and you should try the syntax “<literal>?P<<value>name</value>></literal>”. | 179 “<literal>?P<<value>name</value>></literal>” should be tried instead. |
177 The captures can also be used in digital form: | 180 The captures can also be used in digital form: |
178 | 181 |
179 <programlisting> | 182 <programlisting> |
180 server { | 183 server { |
181 server_name ~^(www\.)?(.+)$; | 184 server_name ~^(www\.)?(.+)$; |
196 | 199 |
197 <section id="miscellaneous_names" | 200 <section id="miscellaneous_names" |
198 name="Miscellaneous names"> | 201 name="Miscellaneous names"> |
199 | 202 |
200 <para> | 203 <para> |
201 If you want to process requests without a “Host” header line | 204 There are some server names that are treated specially. |
202 in a server block which is not the default, you should specify an empty name: | 205 </para> |
206 | |
207 <para> | |
208 If it is required to process requests without the <header>Host</header> | |
209 header field in a <link doc="ngx_http_core_module.xml" id="server"/> | |
210 block which is not the default, an empty name should be specified: | |
203 | 211 |
204 <programlisting> | 212 <programlisting> |
205 server { | 213 server { |
206 listen 80; | 214 listen 80; |
207 server_name example.org www.example.org ""; | 215 server_name example.org www.example.org ""; |
211 </para> | 219 </para> |
212 | 220 |
213 <para> | 221 <para> |
214 If no | 222 If no |
215 <link doc="ngx_http_core_module.xml" id="server_name"/> | 223 <link doc="ngx_http_core_module.xml" id="server_name"/> |
216 is defined in a server block, | 224 is defined in a <link doc="ngx_http_core_module.xml" id="server"/> block |
217 then nginx uses the empty name as the server name. | 225 then nginx uses the empty name as the server name. |
218 <note> | 226 <note> |
219 nginx versions up to 0.8.48 used the <i>hostname</i> as the server name | 227 nginx versions up to 0.8.48 used the <i>hostname</i> as the server name |
220 in this case. | 228 in this case. |
221 </note> | 229 </note> |
222 </para> | 230 </para> |
223 | 231 |
224 <para> | 232 <para> |
225 If someone makes a request using an IP address instead of a server name, | 233 If someone makes a request using an IP address instead of a server name, |
226 the request’s “Host” header line will contain the IP address | 234 the <header>Host</header> request header field will contain the IP address |
227 and you can handle the request using the IP address as the server name: | 235 and the request can be handled using the IP address as the server name: |
228 | 236 |
229 <programlisting> | 237 <programlisting> |
230 server { | 238 server { |
231 listen 80; | 239 listen 80; |
232 server_name example.org | 240 server_name example.org |
238 } | 246 } |
239 </programlisting> | 247 </programlisting> |
240 </para> | 248 </para> |
241 | 249 |
242 <para> | 250 <para> |
243 In catch-all server examples you may see the strange name “_”: | 251 In catch-all server examples the strange name “<literal>_</literal>” can |
252 be seen: | |
244 | 253 |
245 <programlisting> | 254 <programlisting> |
246 server { | 255 server { |
247 listen 80 default_server; | 256 listen 80 default_server; |
248 server_name _; | 257 server_name _; |
250 } | 259 } |
251 </programlisting> | 260 </programlisting> |
252 | 261 |
253 There is nothing special about this name, it is just one of a myriad | 262 There is nothing special about this name, it is just one of a myriad |
254 of invalid domain names which never intersect with any real name. | 263 of invalid domain names which never intersect with any real name. |
255 You may also use something like “--”, “!@#”, and so on. | 264 Other invalid names like “<literal>--</literal>” and “<literal>!@#</literal>” |
256 </para> | 265 may equally be used. |
257 | 266 </para> |
258 <para> | 267 |
259 nginx versions up to 0.6.25 supported the special name “*” | 268 <para> |
269 nginx versions up to 0.6.25 supported the special name “<literal>*</literal>” | |
260 which was erroneously interpreted to be a catch-all name. | 270 which was erroneously interpreted to be a catch-all name. |
261 It never functioned as a catch-all or wildcard server name. | 271 It never functioned as a catch-all or wildcard server name. |
262 Instead, it supplied the functionality that is now provided | 272 Instead, it supplied the functionality that is now provided |
263 by the | 273 by the |
264 <link doc="ngx_http_core_module.xml" id="server_name_in_redirect"/> | 274 <link doc="ngx_http_core_module.xml" id="server_name_in_redirect"/> |
265 directive. | 275 directive. |
266 The special name “*” is now deprecated | 276 The special name “<literal>*</literal>” is now deprecated |
267 and the | 277 and the |
268 <link doc="ngx_http_core_module.xml" id="server_name_in_redirect"/> | 278 <link doc="ngx_http_core_module.xml" id="server_name_in_redirect"/> |
269 directive should be used. | 279 directive should be used. |
270 Note that there is no way to specify the catch-all name or | 280 Note that there is no way to specify the catch-all name or |
271 the <i>default</i> server using the | 281 the default server using the |
272 <link doc="ngx_http_core_module.xml" id="server_name"/> | 282 <link doc="ngx_http_core_module.xml" id="server_name"/> |
273 directive. | 283 directive. |
274 This is a property of the | 284 This is a property of the |
275 <link doc="ngx_http_core_module.xml" id="listen"/> | 285 <link doc="ngx_http_core_module.xml" id="listen"/> |
276 directive | 286 directive |
277 and not of the | 287 and not of the |
278 <link doc="ngx_http_core_module.xml" id="server_name"/> | 288 <link doc="ngx_http_core_module.xml" id="server_name"/> |
279 directive. | 289 directive. |
280 See also “<link doc="request_processing.xml"/>”. | 290 See also “<link doc="request_processing.xml"/>”. |
281 You can define servers listening on ports *:80 and *:8080, | 291 It is possible to define servers listening on ports *:80 and *:8080, |
282 and direct that one will be the default server for port *:8080, | 292 and direct that one will be the default server for port *:8080, |
283 while the other will be the default for port *:80: | 293 while the other will be the default for port *:80: |
284 | 294 |
285 <programlisting> | 295 <programlisting> |
286 server { | 296 server { |
305 | 315 |
306 <section id="optimization" | 316 <section id="optimization" |
307 name="Optimization"> | 317 name="Optimization"> |
308 | 318 |
309 <para> | 319 <para> |
310 Exact names and wildcard names are stored in hashes. | 320 Exact names, wildcard names starting with an asterisk, |
311 The hashes are bound to the listen ports and every listen port | 321 and wildcard names ending with an asterisk are stored |
312 may have up to three hashes: an exact names hash, a wildcard names hash | 322 in three hash tables bound to the listen ports. |
313 starting with an asterisk, and a wildcard names hash ending with an asterisk. | 323 The sizes of hash tables are optimized at the configuration phase |
314 The sizes of the hashes are optimized at the configuration phase so that | 324 so that a name can be found with the fewest CPU cache misses. |
315 a name can be found with the fewest CPU cache misses. | 325 See also “<link doc="../hash.xml"/>”. |
316 </para> | 326 </para> |
317 | 327 |
318 <para> | 328 <para> |
319 The exact names hash is searched first. | 329 The exact names hash table is searched first. |
320 If a name is not found using the exact name hash, then the wildcard names hash | 330 If a name is not found, the hash table with wildcard names |
321 starting with an asterisk is searched. | 331 starting with an asterisk is searched. |
322 If the name is not found there, the wildcard names hash | 332 If the name is not found there, the hash table with wildcard names |
323 ending with an asterisk is searched. | 333 ending with an asterisk is searched. |
324 </para> | 334 </para> |
325 | 335 |
326 <para> | 336 <para> |
327 Searching wildcard names hashes is slower than searching exact name hash | 337 Searching wildcard names hash table is slower than searching exact names hash |
328 because names are searched by domain parts. | 338 table because names are searched by domain parts. |
329 Note that the special wildcard form “<literal>.example.org</literal>” | 339 Note that the special wildcard form “<literal>.example.org</literal>” |
330 is stored in a wildcard names hash and not in an exact names hash. | 340 is stored in a wildcard names hash table and not in an exact names hash table. |
331 </para> | 341 </para> |
332 | 342 |
333 <para> | 343 <para> |
334 Regular expressions are tested sequentially | 344 Regular expressions are tested sequentially |
335 and therefore are the slowest method and are non-scalable. | 345 and therefore are the slowest method and are non-scalable. |
359 } | 369 } |
360 </programlisting> | 370 </programlisting> |
361 </para> | 371 </para> |
362 | 372 |
363 <para> | 373 <para> |
364 If you have defined a large number of server names, | 374 If a large number of server names are defined, |
365 or defined unusually long server names, you may need to tune | 375 or unusually long server names are defined, tuning |
366 the <link doc="ngx_http_core_module.xml" id="server_names_hash_max_size"/> | 376 the <link doc="ngx_http_core_module.xml" id="server_names_hash_max_size"/> |
367 and <link doc="ngx_http_core_module.xml" id="server_names_hash_bucket_size"/> | 377 and <link doc="ngx_http_core_module.xml" id="server_names_hash_bucket_size"/> |
368 directives at the <i>http</i> level. | 378 directives at the <i>http</i> level may become necessary. |
369 The default value of the | 379 The default value of the |
370 <link doc="ngx_http_core_module.xml" id="server_names_hash_bucket_size"/> | 380 <link doc="ngx_http_core_module.xml" id="server_names_hash_bucket_size"/> |
371 may be equal to 32, or 64, or another value, | 381 may be equal to 32, or 64, or another value, |
372 depending on your CPU cache line size. | 382 depending on CPU cache line size. |
373 If the default value is 32 and you define | 383 If the default value is 32 and server name is defined as |
374 “too.long.server.name.example.org” as a server name, | 384 “<literal>too.long.server.name.example.org</literal>” |
375 then nginx will fail to start and display the error message: | 385 then nginx will fail to start and display the error message: |
376 | 386 |
377 <programlisting> | 387 <programlisting> |
378 could not build the server_names_hash, | 388 could not build the server_names_hash, |
379 you should increase server_names_hash_bucket_size: 32 | 389 you should increase server_names_hash_bucket_size: 32 |
380 </programlisting> | 390 </programlisting> |
381 | 391 |
382 In this case, you should set the directive value to the next power of 2: | 392 In this case, the directive value should be increased to the next power of two: |
383 | 393 |
384 <programlisting> | 394 <programlisting> |
385 http { | 395 http { |
386 server_names_hash_bucket_size 64; | 396 server_names_hash_bucket_size 64; |
387 ... | 397 ... |
388 </programlisting> | 398 </programlisting> |
389 | 399 |
390 If you have defined a large number of server names, | 400 If a large number of server names are defined, |
391 you will get another error message: | 401 another error message will appear: |
392 | 402 |
393 <programlisting> | 403 <programlisting> |
394 could not build the server_names_hash, | 404 could not build the server_names_hash, |
395 you should increase either server_names_hash_max_size: 512 | 405 you should increase either server_names_hash_max_size: 512 |
396 or server_names_hash_bucket_size: 32 | 406 or server_names_hash_bucket_size: 32 |
397 </programlisting> | 407 </programlisting> |
398 | 408 |
399 You should first try to set | 409 In such a case, first try to set |
400 <link doc="ngx_http_core_module.xml" id="server_names_hash_max_size"/> | 410 <link doc="ngx_http_core_module.xml" id="server_names_hash_max_size"/> |
401 to a number close to the number of server names. | 411 to a number close to the number of server names. |
402 Only if this does not help, | 412 Only if this does not help, |
403 or if nginx’s start time is unacceptably long, | 413 or if nginx’s start time is unacceptably long, try to increase |
404 should you try to increase | |
405 <link doc="ngx_http_core_module.xml" id="server_names_hash_bucket_size"/>. | 414 <link doc="ngx_http_core_module.xml" id="server_names_hash_bucket_size"/>. |
406 </para> | 415 </para> |
407 | 416 |
408 <para> | 417 <para> |
409 If a server is the only server for a listen port, then nginx will not test | 418 If a server is the only server for a listen port, then nginx will not test |
410 server names at all (and will not build the hashes for the listen port). | 419 server names at all (and will not build the hash tables for the listen port). |
411 However, there is one exception. | 420 However, there is one exception. |
412 If a | 421 If a |
413 <link doc="ngx_http_core_module.xml" id="server_name"/> | 422 <link doc="ngx_http_core_module.xml" id="server_name"/> |
414 is a regular expression with captures, | 423 is a regular expression with captures, |
415 then nginx has to execute the expression to get the captures. | 424 then nginx has to execute the expression to get the captures. |
423 | 432 |
424 <para> | 433 <para> |
425 <list type="bullet"> | 434 <list type="bullet"> |
426 | 435 |
427 <listitem> | 436 <listitem> |
428 A default server name value is an empty name “” since 0.8.48. | 437 A default server name value is an empty name “” since 0.8.48. |
429 </listitem> | 438 </listitem> |
430 | 439 |
431 <listitem> | 440 <listitem> |
432 Named regular expression server name captures have been supported since 0.8.25. | 441 Named regular expression server name captures have been supported since 0.8.25. |
433 </listitem> | 442 </listitem> |
435 <listitem> | 444 <listitem> |
436 Regular expression server name captures have been supported since 0.7.40. | 445 Regular expression server name captures have been supported since 0.7.40. |
437 </listitem> | 446 </listitem> |
438 | 447 |
439 <listitem> | 448 <listitem> |
440 An empty server name “” has been supported since 0.7.12. | 449 An empty server name “” has been supported since 0.7.12. |
441 </listitem> | 450 </listitem> |
442 | 451 |
443 <listitem> | 452 <listitem> |
444 A wildcard server name or regular expression has been supported for use | 453 A wildcard server name or regular expression has been supported for use |
445 as the first server name since 0.6.25. | 454 as the first server name since 0.6.25. |