Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/en/docs/http/configuring_https_servers.xml
1 views
1
<!--
2
Copyright (C) Igor Sysoev
3
Copyright (C) Nginx, Inc.
4
-->
5
6
<!DOCTYPE article SYSTEM "../../../../dtd/article.dtd">
7
8
<article name="Configuring HTTPS servers"
9
link="/en/docs/http/configuring_https_servers.html"
10
lang="en"
11
rev="19"
12
author="Igor Sysoev"
13
editor="Brian Mercer">
14
15
<section>
16
17
<para>
18
To configure an HTTPS server, the <literal>ssl</literal> parameter
19
must be enabled on
20
<link doc="ngx_http_core_module.xml" id="listen">listening sockets</link>
21
in the <link doc="ngx_http_core_module.xml" id="server"/> block,
22
and the locations of the
23
<link doc="ngx_http_ssl_module.xml" id="ssl_certificate">server certificate</link>
24
and
25
<link doc="ngx_http_ssl_module.xml" id="ssl_certificate_key">private key</link>
26
files should be specified:
27
28
<programlisting>
29
server {
30
listen 443 <b>ssl</b>;
31
server_name www.example.com;
32
ssl_certificate <b>www.example.com.crt</b>;
33
ssl_certificate_key <b>www.example.com.key</b>;
34
ssl_protocols TLSv1.2 TLSv1.3;
35
ssl_ciphers HIGH:!aNULL:!MD5;
36
...
37
}
38
</programlisting>
39
40
The server certificate is a public entity.
41
It is sent to every client that connects to the server.
42
The private key is a secure entity and should be stored in a file with
43
restricted access, however, it must be readable by nginx’s master process.
44
The private key may alternately be stored in the same file as the certificate:
45
46
<programlisting>
47
ssl_certificate www.example.com.cert;
48
ssl_certificate_key www.example.com.cert;
49
</programlisting>
50
51
in which case the file access rights should also be restricted.
52
Although the certificate and the key are stored in one file,
53
only the certificate is sent to a client.
54
</para>
55
56
<para>
57
The directives <link doc="ngx_http_ssl_module.xml" id="ssl_protocols"/> and
58
<link doc="ngx_http_ssl_module.xml" id="ssl_ciphers"/>
59
can be used to limit connections
60
to include only the strong versions and ciphers of SSL/TLS.
61
By default nginx uses
62
<literal>ssl_protocols TLSv1.2 TLSv1.3</literal>
63
and “<literal>ssl_ciphers HIGH:!aNULL:!MD5</literal>”,
64
so configuring them explicitly is generally not needed.
65
Note that default values of these directives were
66
<link id="compatibility">changed</link> several times.
67
</para>
68
69
</section>
70
71
72
<section id="optimization" name="HTTPS server optimization">
73
74
<para>
75
SSL operations consume extra CPU resources.
76
On multi-processor systems several
77
<link doc="../ngx_core_module.xml" id="worker_processes">worker processes</link>
78
should be run,
79
no less than the number of available CPU cores.
80
The most CPU-intensive operation is the SSL handshake.
81
There are two ways to minimize the number of these operations per client:
82
the first is by enabling
83
<link doc="ngx_http_core_module.xml" id="keepalive_timeout">keepalive</link>
84
connections to send several
85
requests via one connection and the second is to reuse SSL session
86
parameters to avoid SSL handshakes for parallel and subsequent connections.
87
The sessions are stored in an SSL session cache shared between workers
88
and configured by the
89
<link doc="ngx_http_ssl_module.xml" id="ssl_session_cache"/>
90
directive.
91
One megabyte of the cache contains about 4000 sessions.
92
The default cache timeout is 5 minutes.
93
It can be increased by using the
94
<link doc="ngx_http_ssl_module.xml" id="ssl_session_timeout"/>
95
directive.
96
Here is a sample configuration optimized for a multi-core system
97
with 10 megabyte shared session cache:
98
99
<programlisting>
100
<b>worker_processes auto</b>;
101
102
http {
103
<b>ssl_session_cache shared:SSL:10m</b>;
104
<b>ssl_session_timeout 10m</b>;
105
106
server {
107
listen 443 ssl;
108
server_name www.example.com;
109
<b>keepalive_timeout 70</b>;
110
111
ssl_certificate www.example.com.crt;
112
ssl_certificate_key www.example.com.key;
113
ssl_protocols TLSv1.2 TLSv1.3;
114
ssl_ciphers HIGH:!aNULL:!MD5;
115
...
116
</programlisting>
117
</para>
118
119
</section>
120
121
122
<section id="chains" name="SSL certificate chains">
123
124
<para>
125
Some browsers may complain about a certificate signed by a well-known
126
certificate authority, while other browsers may accept the certificate
127
without issues.
128
This occurs because the issuing authority has signed the server certificate
129
using an intermediate certificate that is not present in the certificate
130
base of well-known trusted certificate authorities which is distributed
131
with a particular browser.
132
In this case the authority provides a bundle of chained certificates
133
which should be concatenated to the signed server certificate.
134
The server certificate must appear before the chained certificates
135
in the combined file:
136
137
<programlisting>
138
$ cat www.example.com.crt bundle.crt > www.example.com.chained.crt
139
</programlisting>
140
141
The resulting file should be used in the
142
<link doc="ngx_http_ssl_module.xml" id="ssl_certificate"/> directive:
143
144
<programlisting>
145
server {
146
listen 443 ssl;
147
server_name www.example.com;
148
ssl_certificate www.example.com.chained.crt;
149
ssl_certificate_key www.example.com.key;
150
...
151
}
152
</programlisting>
153
154
If the server certificate and the bundle have been concatenated in the wrong
155
order, nginx will fail to start and will display the error message:
156
157
<programlisting>
158
SSL_CTX_use_PrivateKey_file(" ... /www.example.com.key") failed
159
(SSL: error:05800074:x509 certificate routines::key values mismatch)
160
</programlisting>
161
162
because nginx has tried to use the private key with the bundle’s
163
first certificate instead of the server certificate.
164
</para>
165
166
<para>
167
Browsers usually store intermediate certificates which they receive
168
and which are signed by trusted authorities, so actively used browsers
169
may already have the required intermediate certificates and
170
may not complain about a certificate sent without a chained bundle.
171
To ensure the server sends the complete certificate chain,
172
the <command>openssl</command> command-line utility may be used, for example:
173
174
<programlisting>
175
$ openssl s_client -connect www.godaddy.com:443
176
...
177
Certificate chain
178
0 s:/C=US/ST=Arizona/L=Scottsdale/1.3.6.1.4.1.311.60.2.1.3=US
179
/1.3.6.1.4.1.311.60.2.1.2=AZ/O=GoDaddy.com, Inc
180
/OU=MIS Department/<b>CN=www.GoDaddy.com</b>
181
/serialNumber=0796928-7/2.5.4.15=V1.0, Clause 5.(b)
182
i:/C=US/ST=Arizona/L=Scottsdale/O=GoDaddy.com, Inc.
183
/OU=http://certificates.godaddy.com/repository
184
/CN=Go Daddy Secure Certification Authority
185
/serialNumber=07969287
186
1 s:/C=US/ST=Arizona/L=Scottsdale/O=GoDaddy.com, Inc.
187
/OU=http://certificates.godaddy.com/repository
188
/CN=Go Daddy Secure Certification Authority
189
/serialNumber=07969287
190
i:/C=US/O=The Go Daddy Group, Inc.
191
/OU=Go Daddy Class 2 Certification Authority
192
2 s:/C=US/O=The Go Daddy Group, Inc.
193
/OU=Go Daddy Class 2 Certification Authority
194
i:/L=ValiCert Validation Network/O=<b>ValiCert, Inc.</b>
195
/OU=ValiCert Class 2 Policy Validation Authority
196
/CN=http://www.valicert.com//[email protected]
197
...
198
</programlisting>
199
200
In this example the subject (“<i>s</i>”) of the
201
<literal>www.GoDaddy.com</literal> server certificate #0 is signed by an issuer
202
(“<i>i</i>”) which itself is the subject of the certificate #1,
203
which is signed by an issuer which itself is the subject of the certificate #2,
204
which signed by the well-known issuer <i>ValiCert, Inc.</i>
205
whose certificate is stored in the browsers’ built-in
206
certificate base (that lay in the house that Jack built).
207
</para>
208
209
<para>
210
If a certificate bundle has not been added, only the server certificate #0
211
will be shown.
212
</para>
213
214
</section>
215
216
217
<section id="single_http_https_server" name="A single HTTP/HTTPS server">
218
219
<para>
220
It is possible to configure a single server that handles both HTTP
221
and HTTPS requests:
222
223
<programlisting>
224
server {
225
listen 80;
226
listen 443 ssl;
227
server_name www.example.com;
228
ssl_certificate www.example.com.crt;
229
ssl_certificate_key www.example.com.key;
230
...
231
}
232
</programlisting>
233
234
<note>
235
Prior to 0.7.14 SSL could not be enabled selectively for
236
individual listening sockets, as shown above.
237
SSL could only be enabled for the entire server using the
238
<link doc="ngx_http_ssl_module.xml" id="ssl"/> directive,
239
making it impossible to set up a single HTTP/HTTPS server.
240
The <literal>ssl</literal> parameter of the
241
<link doc="ngx_http_core_module.xml" id="listen"/> directive
242
was added to solve this issue.
243
The use of the
244
<link doc="ngx_http_ssl_module.xml" id="ssl"/> directive
245
in modern versions is thus discouraged;
246
it was removed in 1.25.1.
247
</note>
248
</para>
249
250
</section>
251
252
253
<section id="name_based_https_servers" name="Name-based HTTPS servers">
254
255
<para>
256
A common issue arises when configuring two or more HTTPS servers
257
listening on a single IP address:
258
259
<programlisting>
260
server {
261
listen 443 ssl;
262
server_name www.example.com;
263
ssl_certificate www.example.com.crt;
264
...
265
}
266
267
server {
268
listen 443 ssl;
269
server_name www.example.org;
270
ssl_certificate www.example.org.crt;
271
...
272
}
273
</programlisting>
274
275
With this configuration a browser receives the default server’s certificate,
276
i.e. <literal>www.example.com</literal> regardless of the requested server name.
277
This is caused by SSL protocol behaviour.
278
The SSL connection is established before the browser sends an HTTP request
279
and nginx does not know the name of the requested server.
280
Therefore, it may only offer the default server’s certificate.
281
</para>
282
283
<para>
284
The oldest and most robust method to resolve the issue
285
is to assign a separate IP address for every HTTPS server:
286
287
<programlisting>
288
server {
289
listen 192.168.1.1:443 ssl;
290
server_name www.example.com;
291
ssl_certificate www.example.com.crt;
292
...
293
}
294
295
server {
296
listen 192.168.1.2:443 ssl;
297
server_name www.example.org;
298
ssl_certificate www.example.org.crt;
299
...
300
}
301
</programlisting>
302
</para>
303
304
305
<section id="certificate_with_several_names"
306
name="An SSL certificate with several names">
307
308
<para>
309
There are other ways that allow sharing a single IP address
310
between several HTTPS servers.
311
However, all of them have their drawbacks.
312
One way is to use a certificate with several names in
313
the SubjectAltName certificate field, for example,
314
<literal>www.example.com</literal> and <literal>www.example.org</literal>.
315
However, the SubjectAltName field length is limited.
316
</para>
317
318
<para>
319
Another way is to use a certificate with a wildcard name, for example,
320
<literal>*.example.org</literal>.
321
A wildcard certificate secures all subdomains of the specified domain,
322
but only on one level.
323
This certificate matches <literal>www.example.org</literal>, but does not match
324
<literal>example.org</literal> and <literal>www.sub.example.org</literal>.
325
These two methods can also be combined.
326
A certificate may contain exact and wildcard names in the
327
SubjectAltName field, for example,
328
<literal>example.org</literal> and <literal>*.example.org</literal>.
329
</para>
330
331
<para>
332
It is better to place a certificate file with several names and
333
its private key file at the <i>http</i> level of configuration
334
to inherit their single memory copy in all servers:
335
336
<programlisting>
337
ssl_certificate common.crt;
338
ssl_certificate_key common.key;
339
340
server {
341
listen 443 ssl;
342
server_name www.example.com;
343
...
344
}
345
346
server {
347
listen 443 ssl;
348
server_name www.example.org;
349
...
350
}
351
</programlisting>
352
</para>
353
354
</section>
355
356
357
<section id="sni" name="Server Name Indication">
358
359
<para>
360
A more generic solution for running several HTTPS servers on a single
361
IP address is
362
<link url="http://en.wikipedia.org/wiki/Server_Name_Indication">TLS
363
Server Name Indication extension</link> (SNI, RFC 6066),
364
which allows a browser to pass a requested server name during the SSL handshake
365
and, therefore, the server will know which certificate it should use
366
for the connection.
367
SNI is currently
368
<link url="http://en.wikipedia.org/wiki/Server_Name_Indication#Support">supported</link>
369
by most modern browsers
370
and is a mandatory-to-implement extension in TLSv1.3,
371
though may not be used by some old or special clients.
372
<note>
373
Only domain names can be passed in SNI,
374
however some browsers may erroneously pass an IP address of the server
375
as its name if a request includes literal IP address.
376
One should not rely on this.
377
</note>
378
</para>
379
380
<para>
381
In order to use SNI in nginx, it must be supported in both the
382
OpenSSL library with which the nginx binary has been built as well as
383
the library to which it is being dynamically linked at run time.
384
OpenSSL supports SNI since 0.9.8f version if it was built with config option
385
<nobr>“--enable-tlsext”.</nobr>
386
Since OpenSSL 0.9.8j this option is enabled by default.
387
If nginx was built with SNI support, then nginx will show this
388
when run with the “-V” switch:
389
390
<programlisting>
391
$ nginx -V
392
...
393
TLS SNI support enabled
394
...
395
</programlisting>
396
397
However, if the SNI-enabled nginx is linked dynamically to
398
an OpenSSL library without SNI support, nginx displays the warning:
399
400
<programlisting>
401
nginx was built with SNI support, however, now it is linked
402
dynamically to an OpenSSL library which has no tlsext support,
403
therefore SNI is not available
404
</programlisting>
405
</para>
406
407
</section>
408
409
</section>
410
411
412
<section id="compatibility" name="Compatibility">
413
414
<para>
415
<list type="bullet">
416
417
<listitem>
418
The SNI support status has been shown by the “-V” switch
419
since 0.8.21 and 0.7.62.
420
</listitem>
421
422
<listitem>
423
The <literal>ssl</literal> parameter of the
424
<link doc="ngx_http_core_module.xml" id="listen"/>
425
directive has been supported since 0.7.14.
426
Prior to 0.8.21 it could only be specified along with the
427
<literal>default</literal> parameter.
428
</listitem>
429
430
<listitem>
431
SNI has been supported since 0.5.23.
432
</listitem>
433
434
<listitem>
435
The shared SSL session cache has been supported since 0.5.6.
436
</listitem>
437
438
</list>
439
</para>
440
441
<para>
442
<list type="bullet">
443
444
<listitem>
445
Version 1.27.3 and later: the default SSL protocols are
446
TLSv1.2 and TLSv1.3 (if supported by the OpenSSL library).
447
Otherwise, when OpenSSL 1.0.0 or older is used,
448
the default SSL protocols are TLSv1 and TLSv1.1.
449
</listitem>
450
451
<listitem>
452
Version 1.23.4 and later: the default SSL protocols are TLSv1,
453
TLSv1.1, TLSv1.2, and TLSv1.3 (if supported by the OpenSSL library).
454
</listitem>
455
456
<listitem>
457
Version 1.9.1 and later: the default SSL protocols are TLSv1,
458
TLSv1.1, and TLSv1.2 (if supported by the OpenSSL library).
459
</listitem>
460
461
<listitem>
462
Version 0.7.65, 0.8.19 and later: the default SSL protocols are SSLv3, TLSv1,
463
TLSv1.1, and TLSv1.2 (if supported by the OpenSSL library).
464
</listitem>
465
466
<listitem>
467
Version 0.7.64, 0.8.18 and earlier: the default SSL protocols are SSLv2,
468
SSLv3, and TLSv1.
469
</listitem>
470
471
</list>
472
</para>
473
474
<para>
475
<list type="bullet">
476
477
<listitem>
478
Version 1.0.5 and later: the default SSL ciphers are
479
<literal>HIGH:!aNULL:!MD5</literal>”.
480
</listitem>
481
482
<listitem>
483
Version 0.7.65, 0.8.20 and later: the default SSL ciphers are
484
<literal>HIGH:!ADH:!MD5</literal>”.
485
</listitem>
486
487
<listitem>
488
Version 0.8.19: the default SSL ciphers are
489
<literal>ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM</literal>”.
490
</listitem>
491
492
<listitem>
493
Version 0.7.64, 0.8.18 and earlier: the default SSL ciphers are<br/>
494
<literal>ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP</literal>”.
495
</listitem>
496
497
</list>
498
</para>
499
500
501
</section>
502
503
504
</article>
505
506