comparison xml/en/docs/http/server_names.xml @ 0:61e04fc01027

Initial import of the nginx.org website.
author Ruslan Ermilov <ru@nginx.com>
date Thu, 11 Aug 2011 12:19:13 +0000
parents
children 9d544687d02c
comparison
equal deleted inserted replaced
-1:000000000000 0:61e04fc01027
1 <!DOCTYPE digest SYSTEM "../../../../dtd/article.dtd">
2
3 <article title="Server names"
4 link="/en/docs/http/server_names.html"
5 lang="en"
6 author="Igor Sysoev"
7 editor="Brian Mercer">
8
9
10 <section>
11
12 <para>
13 Server names are defined using the <dirname>server_name</dirname> directive
14 and determine which server block is used for a given request.
15 See also &ldquo;<a href="/en/docs/http/request_processing.xml" />&rdquo;.
16 They may be defined using exact names, wildcard names, or regular expressions:
17
18 <programlisting>
19 server {
20 listen 80;
21 server_name nginx.org www.nginx.org;
22 ...
23 }
24
25 server {
26 listen 80;
27 server_name *.nginx.org;
28 ...
29 }
30
31 server {
32 listen 80;
33 server_name mail.*;
34 ...
35 }
36
37 server {
38 listen 80;
39 server_name ~^(?&lt;user&gt;.+)\.nginx\.net$;
40 ...
41 }
42 </programlisting>
43
44 The names are tested in the following order:
45
46 <orderedlist>
47
48 <item>
49 exact names;
50 </item>
51
52 <item>
53 wildcard names starting with an asterisk: <url>*.nginx.org</url>;
54 </item>
55
56 <item>
57 wildcard names ending with an asterisk: <url>mail.*</url>;
58 </item>
59
60 <item>
61 and regular expressions in the order listed in the configuration file.
62 </item>
63
64 </orderedlist>
65 The first match stops the search.
66 </para>
67
68 </section>
69
70
71 <section name="wildcard_names"
72 title="Wildcard names">
73
74 <para>
75 A wildcard name may contain an asterisk only on the name's start or end,
76 and only on a dot border. The names <dirname>www.*.nginx.org</dirname>
77 and <dirname>w*.nginx.org</dirname> are invalid.
78 However, these names can be specified using regular expressions,
79 for example, <dirname>~^www\..+\.nginx\.org$</dirname> and
80 <dirname>~^w.*\.nginx\.org$</dirname>.
81 An asterisk can match several name parts.
82 The name <dirname>*.nginx.org</dirname> matches not only
83 <url>www.nginx.org</url> but <url>www.sub.nginx.org</url> as well.
84 </para>
85
86 <para>
87 A special wildcard in the form <dirname>.nginx.org</dirname> can be used
88 to match both the exact name <dirname>nginx.org</dirname>
89 and the wildcard name <dirname>*.nginx.org</dirname>.
90 </para>
91
92 </section>
93
94
95 <section name="regex_names"
96 title="Regular expressions names">
97
98 <para>
99 The regular expressions used by nginx are compatible with those used
100 by the Perl programming language (PCRE).
101 To use a regular expression, the server name must start with the tilde
102 character:
103
104 <programlisting>
105 server_name ~^www\d+\.nginx\.net$;
106 </programlisting>
107
108 otherwise it will be treated as an exact name, or if the expression contains
109 an asterisk, as a wildcard name (and most likely as an invalid one).
110 Do not forget to set &ldquo;^&rdquo; and &ldquo;$&rdquo; anchors.
111 They are not required syntactically, but logically.
112 Also note that domain name dots should be escaped with a backslash.
113 A regular expression containing the characters &ldquo;{&rdquo;
114 and &ldquo;}&rdquo; should be quoted:
115
116 <programlisting>
117 server_name "~^(?&lt;name&gt;\w\d<b>{</b>1,3<b>}</b>+)\.nginx\.net$";
118 </programlisting>
119
120 otherwise nginx will fail to start and display the error message:
121
122 <programlisting>
123 directive "server_name" is not terminated by ";" in ...
124 </programlisting>
125
126 A named regular expression capture can be used later as a variable:
127
128 <programlisting>
129 server {
130 server_name ~^(www\.)?(<b>?&lt;domain&gt;</b>.+)$;
131
132 location / {
133 root /sites/<b>$domain</b>;
134 }
135 }
136 </programlisting>
137
138 The PCRE library supports named captures using the following syntax:
139
140 <table note="yes">
141
142 <tr>
143 <td><code>?&lt;<i>name</i>&gt;</code></td>
144 <td>Perl 5.10 compatible syntax, supported since PCRE-7.0</td>
145 </tr>
146
147 <tr>
148 <td><code>?'<i>name</i>'</code></td>
149 <td>Perl 5.10 compatible syntax, supported since PCRE-7.0</td>
150 </tr>
151
152 <tr>
153 <td><code>?P&lt;<i>name</i>&gt;</code></td>
154 <td>Python compatible syntax, supported since PCRE-4.0</td>
155 </tr>
156
157 </table>
158
159 If nginx fails to start and displays the error message:
160
161 <programlisting>
162 pcre_compile() failed: unrecognized character after (?&lt; in ...
163 </programlisting>
164
165 this means that the PCRE library is old
166 and you should try the syntax <dirname>?P&lt;<i>name</i>&gt;</dirname>.
167 The captures can also be used in digital form:
168
169 <programlisting>
170 server {
171 server_name ~^(www\.)?(.+)$;
172
173 location / {
174 root /sites/<b>$2</b>;
175 }
176 }
177 </programlisting>
178
179 However, such usage should be limited to simple cases (like the above),
180 since the digital references can easily be overwritten.
181 </para>
182
183
184 </section>
185
186
187 <section name="miscellaneous_names"
188 title="Miscellaneous names">
189
190 <para>
191 If you want to process requests without a &ldquo;Host&rdquo; header line
192 in a server block which is not the default, you should specify an empty name:
193
194 <programlisting>
195 server {
196 listen 80;
197 server_name nginx.org www.nginx.org "";
198 ...
199 }
200 </programlisting>
201 </para>
202
203 <para>
204 If no <dirname>server_name</dirname> is defined in a server block,
205 then nginx uses the empty name as the server name.
206 <note>
207 nginx versions up to 0.8.48 used the <i>hostname</i> as the server name
208 in this case.
209 </note>
210 </para>
211
212 <para>
213 If someone makes a request using an IP address instead of a server name,
214 the request&rsquo;s &ldquo;Host&rdquo; header line will contain the IP address
215 and you can handle the request using the IP address as the server name:
216
217 <programlisting>
218 server {
219 listen 80;
220 server_name nginx.org
221 www.nginx.org
222 ""
223 <b>192.168.1.1</b>
224 ;
225 ...
226 }
227 </programlisting>
228 </para>
229
230 <para>
231 In catch-all server examples you may see the strange name &ldquo;_&rdquo;:
232
233 <programlisting>
234 server {
235 listen 80 default_server;
236 server_name _;
237 return 444;
238 }
239 </programlisting>
240
241 There is nothing special about this name, it is just one of a myriad
242 of invalid domain names which never intersect with any real name.
243 You may also use something like &ldquo;--&rdquo;, &ldquo;!@#&rdquo;, and so on.
244 </para>
245
246 <para>
247 nginx versions up to 0.6.25 supported the special name &ldquo;*&rdquo;
248 which was erroneously interpreted to be a catch-all name.
249 It never functioned as a catch-all or wildcard server name.
250 Instead, it supplied the functionality that is now provided
251 by the <dirname>server_name_in_redirect</dirname> directive.
252 The special name &ldquo;*&rdquo; is now deprecated
253 and the <dirname>server_name_in_redirect</dirname> directive should be used.
254 Note that there is no way to specify the catch-all name or
255 the <i>default</i> server using the <dirname>server_name</dirname> directive.
256 This is a property of the <dirname>listen</dirname> directive
257 and not of the <dirname>server_name</dirname> directive.
258 See also &ldquo;<a href="/en/docs/http/request_processing.xml" />&rdquo;.
259 You can define servers listening on ports *:80 and *:8080,
260 and direct that one will be the default server for port *:8080,
261 while the other will be the default for port *:80:
262
263 <programlisting>
264 server {
265 listen 80;
266 listen 8080 default_server;
267 server_name nginx.net;
268 ...
269 }
270
271 server {
272 listen 80 default_server;
273 listen 8080;
274 server_name nginx.org;
275 ...
276 }
277 </programlisting>
278 </para>
279
280
281 </section>
282
283
284 <section name="optimization"
285 title="Optimization">
286
287 <para>
288 Exact names and wildcard names are stored in hashes.
289 The hashes are bound to the listen ports and every listen port
290 may have up to three hashes: an exact names hash, a wildcard names hash
291 starting with an asterisk, and a wildcard names hash ending with an asterisk.
292 The sizes of the hashes are optimized at the configuration phase so that
293 a name can be found with the fewest CPU cache misses.
294 The exact names hash is searched first.
295 If a name is not found using the exact name hash, then the wildcard names hash
296 starting with an asterisk is searched.
297 If the name is not found there, the wildcard names hash
298 ending with an asterisk is searched.
299 Searching wildcard names hashes is slower than searching exact name hash
300 because names are searched by domain parts.
301 Note that the special wildcard form <dirname>.nginx.org</dirname>
302 is stored in a wildcard names hash and not in an exact names hash.
303 Regular expressions are tested sequentially
304 and therefore are the slowest method and are non-scalable.
305 </para>
306
307 <para>
308 For these reasons, it is better to use exact names where possible.
309 For example, if the most frequently requested names of a server
310 are <url>nginx.org</url> and <url>www.nginx.org</url>,
311 it is more efficient to define them explicitly:
312
313 <programlisting>
314 server {
315 listen 80;
316 server_name nginx.org www.nginx.org *.nginx.org;
317 ...
318 }
319 </programlisting>
320
321 than to use the simplified form:
322
323 <programlisting>
324 server {
325 listen 80;
326 server_name .nginx.org;
327 ...
328 }
329 </programlisting>
330 </para>
331
332 <para>
333 If you have defined a large number of server names,
334 or defined unusually long server names, you may need to tune
335 the <dirname>server_names_hash_max_size</dirname>
336 and <dirname>server_names_hash_bucket_size</dirname> directives
337 at the <i>http</i> level.
338 The default value of the <dirname>server_names_hash_bucket_size</dirname>
339 may be equal to 32, or 64, or another value,
340 depending on your CPU cache line size.
341 If the default value is 32 and you define
342 &ldquo;too.long.server.name.nginx.org&rdquo; as a server name,
343 then nginx will fail to start and display the error message:
344
345 <programlisting>
346 could not build the server_names_hash,
347 you should increase server_names_hash_bucket_size: 32
348 </programlisting>
349
350 In this case, you should set the directive value to the next power of 2:
351
352 <programlisting>
353 http {
354 server_names_hash_bucket_size 64;
355 ...
356 </programlisting>
357
358 If you have defined a large number of server names,
359 you will get another error message:
360
361 <programlisting>
362 could not build the server_names_hash,
363 you should increase either server_names_hash_max_size: 512
364 or server_names_hash_bucket_size: 32
365 </programlisting>
366
367 You should first try to set <dirname>server_names_hash_max_size</dirname>
368 to a number close to the number of server names.
369 Only if this does not help,
370 or if nginx&rsquo;s start time is unacceptably long,
371 should you try to increase <dirname>server_names_hash_bucket_size</dirname>.
372 </para>
373
374 <para>
375 If a server is the only server for a listen port, then nginx will not test
376 server names at all (and will not build the hashes for the listen port).
377 However, there is one exception.
378 If a <dirname>server_name</dirname> is a regular expression with captures,
379 then nginx has to execute the expression to get the captures.
380 </para>
381
382 </section>
383
384
385 <section name="compatibility"
386 title="Compatibility">
387
388 <para>
389 <list>
390
391 <item>
392 A default server name value is an empty name &ldquo;&rdquo; since 0.8.48.
393 </item>
394
395 <item>
396 Named regular expression server name captures have been supported since 0.8.25.
397 </item>
398
399 <item>
400 Regular expression server name captures have been supported since 0.7.40.
401 </item>
402
403 <item>
404 An empty server name &ldquo;&rdquo; has been supported since 0.7.12.
405 </item>
406
407 <item>
408 A wildcard server name or regular expression has been supported for use
409 as the first server name since 0.6.25.
410 </item>
411
412 <item>
413 Regular expression server names have been supported since 0.6.7.
414 </item>
415
416 <item>
417 Wildcard form <url>nginx.*</url> has been supported since 0.6.0.
418 </item>
419
420 <item>
421 The special form <url>.nginx.org</url> has been supported since 0.3.18.
422 </item>
423
424 <item>
425 Wildcard form <url>*.nginx.org</url> has been supported since 0.1.13.
426 </item>
427
428 </list>
429 </para>
430
431 </section>
432
433 </article>