1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
|
=encoding utf-8
=head1 Name
server_names - Server names
=head1
Server names are defined using the
L<ngx_http_core_module>
directive
and determine which L<ngx_http_core_module> block
is used for a given request.
See also “L<request_processing>”.
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:
=over
=item 1.
exact name
=item 2.
longest wildcard name starting with an asterisk, e.g.
“C<*.example.org>”
=item 3.
longest wildcard name ending with an asterisk, e.g. “C<mail.*>”
=item 4.
first matching regular expression
(in order of appearance in a configuration file)
=back
=head1 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 “C<www.*.example.org>”
and “C<w*.example.org>” are invalid.
However, these names can be specified using regular expressions,
for example, “C<~^www\..+\.example\.org$>” and
“C<~^w.*\.example\.org$>”.
An asterisk can match several name parts.
The name “C<*.example.org>” matches not only
C<www.example.org> but C<www.sub.example.org> as well.
A special wildcard name in the form “C<.example.org>” can be
used to match both the exact name “C<example.org>”
and the wildcard name “C<*.example.org>”.
=head1 Regular expressions names
The regular expressions used by nginx are compatible with those used
by the Perl programming language (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 “C<^>” and “C<$>” 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 “C<{>”
and “C<}>” should be quoted:
server_name "~^(?<name>\w\d<b>{</b>1,3<b>}</b>+)\.example\.net$";
otherwise nginx will fail to start and display the error message:
directive "server_name" is not terminated by ";" in ...
A named regular expression capture can be used later as a variable:
server {
server_name ~^(www\.)?(<b>?<domain></b>.+)$;
location / {
root /sites/<b>$domain</b>;
}
}
The PCRE library supports named captures using the following syntax:
If nginx fails to start and displays the error message:
pcre_compile() failed: unrecognized character after (?< in ...
this means that the PCRE library is old and the syntax
“C<?PE<lt>I<C<name>>E<gt>>” should be tried instead.
The captures can also be used in digital form:
server {
server_name ~^(www\.)?(.+)$;
location / {
root /sites/<b>$2</b>;
}
}
However, such usage should be limited to simple cases (like the above),
since the digital references can easily be overwritten.
=head1 Miscellaneous names
There are some server names that are treated specially.
If it is required to process requests without the C<Host>
header field in a L<ngx_http_core_module>
block which is not the default, an empty name should be specified:
server {
listen 80;
server_name example.org www.example.org "";
...
}
If no
L<ngx_http_core_module>
is defined in a L<ngx_http_core_module> block
then nginx uses the empty name as the server name.
B<NOTE>
nginx versions up to 0.8.48 used the machine’s hostname as the server name
in this case.
If a server name is defined as “C<$hostname>” (0.9.4), the
machine’s hostname is used.
If someone makes a request using an IP address instead of a server name,
the C<Host> request header field will contain the IP address
and the request can be handled using the IP address as the server name:
server {
listen 80;
server_name example.org
www.example.org
""
<b>192.168.1.1</b>
;
...
}
In catch-all server examples the strange name “C<_>” 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 “C<-->” and “C<!@#>”
may equally be used.
nginx versions up to 0.6.25 supported the special name “C<*>”
which was erroneously interpreted to be a catch-all name.
It never functioned as a catch-all or wildcard server name.
Instead, it supplied the functionality that is now provided
by the
L<ngx_http_core_module>
directive.
The special name “C<*>” is now deprecated
and the
L<ngx_http_core_module>
directive should be used.
Note that there is no way to specify the catch-all name or
the default server using the
L<ngx_http_core_module>
directive.
This is a property of the
L<ngx_http_core_module>
directive
and not of the
L<ngx_http_core_module>
directive.
See also “L<request_processing>”.
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:
server {
listen 80;
listen 8080 default_server;
server_name example.net;
...
}
server {
listen 80 default_server;
listen 8080;
server_name example.org;
...
}
=head1 Internationalized names
Internationalized domain names
(L<IDNs|https://en.wikipedia.org/wiki/Internationalized_domain_name>)
should be specified using an ASCII (Punycode) representation
in the L<ngx_http_core_module> directive:
server {
listen 80;
server_name xn--e1afmkfd.xn--80akhbyknj4f; # пример.испытание
...
}
=head1 Virtual server selection
First, a connection is created in a default server context.
Then, the server name can be determined
in the following request processing stages,
each involved in server configuration selection:
=over
=item *
during SSL handshake, in advance, according to
L<SNI|configuring_https_servers>
=item *
after processing the request line
=item *
after processing the C<Host> header field
=item *
if the server name was not determined after processing the request line or
from the C<Host> header field,
nginx will use the empty name as the server name.
=back
At each of these stages, different server configurations can be applied.
As such, certain directives should be specified with caution:
=over
=item *
in case of the
L<ngx_http_ssl_module> directive,
the protocol list is set by the OpenSSL library before
the server configuration could be applied according to the name
requested through SNI,
thus, protocols should be specified only for a default server;
=item *
the
L<ngx_http_core_module>
and
L<ngx_http_core_module> directives
are involved before reading the request line,
thus, such directives use a default server configuration or
the server configuration chosen by SNI;
=item *
in case of the
L<ngx_http_core_module>,
L<ngx_http_core_module>,
and
L<ngx_http_core_module> directives
involved in processing request header fields,
it additionally depends
whether the server configuration was updated
according to the request line or the C<Host> header field;
=item *
an error response will be handled with
the L<ngx_http_core_module> directive
in the server that currently fulfills the request.
=back
=head1 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.
The details of setting up hash tables are provided in a separate
L<document|hash>.
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 “C<.example.org>”
is stored in a wildcard names hash table and not in an exact names hash table.
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 C<example.org> and C<www.example.org>,
it is more efficient to define them explicitly:
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 L<ngx_http_core_module>
and L<ngx_http_core_module>
directives at the I<http> level may become necessary.
The default value of the
L<ngx_http_core_module>
directive may be equal to 32, or 64, or another value,
depending on CPU cache line size.
If the default value is 32 and server name is defined as
“C<too.long.server.name.example.org>”
then nginx will fail to start and display the error message:
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
L<ngx_http_core_module>
to a number close to the number of server names.
Only if this does not help,
or if nginx’s start time is unacceptably long, try to increase
L<ngx_http_core_module>.
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).
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.
=head1 Compatibility
=over
=item *
The special server name “C<$hostname>” has been supported
since 0.9.4.
=item *
A default server name value is an empty name “” since 0.8.48.
=item *
Named regular expression server name captures have been supported since 0.8.25.
=item *
Regular expression server name captures have been supported since 0.7.40.
=item *
An empty server name “” has been supported since 0.7.12.
=item *
A wildcard server name or regular expression has been supported for use
as the first server name since 0.6.25.
=item *
Regular expression server names have been supported since 0.6.7.
=item *
Wildcard form C<example.*> has been supported since 0.6.0.
=item *
The special form C<.example.org> has been supported since 0.3.18.
=item *
Wildcard form C<*.example.org> has been supported since 0.1.13.
=back
|