Install an SSL certificate on nginx

12 min readUpdated

To install an SSL certificate on nginx, put your certificate and its intermediates in one file, point ssl_certificate at that file and ssl_certificate_key at the private key, and turn on TLS with the ssl parameter of listen in the server block [1][2]. Then run nginx -t and reload. Each step comes with what nginx printed in our runs when it went wrong [9]. The steps common to every server are on installing SSL certificates on any server.

Certificate file
PEM, your certificate first, then the intermediates, in ssl_certificate [1]
Private key
a PEM file in ssl_certificate_key, readable by the nginx master process [1][2]
TLS on the port
listen 443 ssl [3]
HTTP/2
the http2 on directive, from nginx 1.25.1 [4]
Apply
nginx -t, then nginx -s reload; a configuration that fails to load leaves the old one running [5][9]

Before you start

  • The certificate and its intermediates, as separate files or already combined into one. A PFX file, as used when you install an SSL certificate on IIS, has to be converted to PEM files first: nginx reads PEM [1]. The OpenSSL commands for that split are part of exporting an SSL certificate to another server.

  • The private key generated with the request, covered on CSR.

  • The configuration file that serves this site. On a server with several sites there are several, and editing the wrong one changes nothing.

To get the certificate and its renewals from an ACME client instead, see ACME clients. Certbot's nginx plugin, certbot --nginx, proves control of the name over HTTP-01 and edits the server block itself; what it changes and how to roll it back is under how Certbot gets and installs a certificate.

Prepare the certificate, chain and key

Combine the certificate and chain

nginx reads your certificate and its intermediates from one file, your certificate first and the intermediate certificates after it, as the nginx documentation specifies [1]. Its HTTPS guide builds the file with the same cat command [2]. Apache handles the chain its own way, covered under how to install an SSL certificate on Apache.

$ cat example.com.crt intermediate.crt > example.com.chained.crt

In the reversed order, nginx refuses to start with key values mismatch, because it tried the private key against the first certificate in the file [2]. Leaving the intermediate out produces no error from nginx. In our runs nginx -t passed, a client that already held the intermediate connected, and curl, which held only the root, failed with unable to get local issuer certificate [9]. The root can be left out of the file; see certificate chains.

Place the files

Keep the certificate and the private key outside the web root and restrict the key:

$ chmod 600 example.com.key
$ chown root:root example.com.key

Warning. The nginx documentation asks for a key file with restricted access that the master process can read [2]; the user directive sets the account of the worker processes [8]. Making the key readable by everyone to clear a permissions error turns a configuration problem into an exposed key. Check the path and ownership instead.

Configure the server block

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    server_name example.com www.example.com;

    ssl_certificate     /etc/ssl/example.com.chained.crt;
    ssl_certificate_key /etc/ssl/example.com.key;

    # the rest of the site configuration
}
  • listen 443 ssl makes every connection on the port work in SSL mode [3]. Without the ssl parameter nginx answers on port 443 in plain HTTP: in our runs curl over https:// got wrong version number, plain HTTP on the same port got 200, and browsers showed ERR_SSL_PROTOCOL_ERROR or, in Firefox, SSL_ERROR_RX_RECORD_TOO_LONG [9]. See port 443.

  • http2 on enables HTTP/2. The directive appeared in nginx 1.25.1 [4]; the older http2 parameter of listen is deprecated [3], and nginx 1.30.6 accepted it with a warning in our runs [9]. On versions before 1.25.1, keep listen 443 ssl http2; and leave out the directive.

  • server_name lists every hostname this block serves, and the certificate must cover each of them. nginx -t does not catch a gap: with a name missing from the certificate, it passed in our runs [9]. See Subject Alternative Names.

Set TLS versions

ssl_protocols TLSv1.2 TLSv1.3;

The line states the default of current nginx; the documentation says setting it explicitly is generally not needed, and that the defaults have changed several times [2]. In our runs a TLS 1.1 client got a protocol version alert, and TLS 1.2 and 1.3 connected [9]. If a configuration copied from an old guide lists TLSv1 or TLSv1.1, remove them. The directive works in the http block and in a server block [1]; a server block gets its own list only with a library that supports it (OpenSSL 1.1.1 or later, BoringSSL, AWS-LC); with other libraries the list of the default server applies [6]. The default cipher list is HIGH:!aNULL:!MD5 [2]; for a written-out list, use the TLSRef Configurator, the former Mozilla SSL Configuration Generator. Why the versions matter is on TLS versions, and what a bad cipher list produces is on cipher mismatch.

Redirect HTTP to HTTPS

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

The redirect sends visitors to HTTPS, and the browser checks the certificate on the connection that follows, so a redirect cannot help a name the certificate does not cover. Once HTTPS works for every name, HSTS tells browsers to skip the HTTP step on later visits.

Test and reload

$ nginx -t
$ nginx -s reload

nginx -t checks the syntax and then tries to open the files the configuration refers to [5], so a missing or unreadable private key shows up here. In our runs it named the key file it could not load, and the file and line of a deprecated parameter [9].

On reload, nginx checks the new configuration and tries to apply it. If that fails, it rolls back and keeps the old one; if it succeeds, new worker processes start and the old ones finish serving their clients [5]. In our runs, a reload pointing at a missing key wrote cannot load certificate key to the error log, and the old certificate stayed in service [9].

Files changed on disk are not picked up by themselves: when we replaced both files with another pair, the old certificate was served until nginx -s reload [9]. A renewal that writes new files therefore needs a reload after it.

The certificate and key matcher checks a pair before the reload. A wrong pair either fails nginx -t with key values mismatch or passes it and then fails the handshake, depending on the key type; both cases are under common mistakes below.

Verify from outside

Check from another machine, for each name in server_name, with the OpenSSL commands for a live host. Your own browser is the weakest test, because it may already hold the intermediate the server fails to send [2].

Several sites on one server

nginx picks the server block during the TLS handshake by the name the client sends, called SNI [6]. If no block matches, the default server for the port answers: the block marked default_server, or the first one with that address and port [3].

In our runs, shop.example.net, with no block of its own, got the first block's example.com certificate; with default_server on the example.org block it received that one instead. A client that sent no SNI got the default server's certificate as well [9]. The visitor sees a name mismatch that looks like a certificate problem and is a configuration one. Adding a site means adding both its server_name and its certificate.

The certificate is set per virtual server [1], so one block can serve several names that share a certificate, and names with separate certificates go in separate blocks, unless the file names are built from variables [1]. Two blocks with the same name on one port are a mistake: nginx ignores the name in the second block, so its certificate is not presented for it [9].

Nginx in front of an application

When nginx sits in front of an application server, it handles TLS with the configuration above and forwards plain HTTP to the application on the same machine:

location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
}

$scheme holds the scheme of the visitor's request, http or https [3], and the header passes it on to the application.

When the application is on another machine and that connection must be encrypted too, use proxy_pass https://. By default nginx does not verify the upstream certificate and does not send the upstream name in SNI, as the proxy module documents [7]. Without verification, the connection to the application is encrypted but not authenticated. In our runs, with the defaults, a certificate nginx had no reason to trust was accepted, and the upstream saw an empty server name [9]. Turn both on:

proxy_ssl_verify on;
proxy_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;
proxy_ssl_server_name on;

The name checked and sent comes from the proxy_pass host unless proxy_ssl_name sets another [7]. With a trust file that does not hold the upstream's root, nginx answered 502 and logged upstream SSL certificate verify error: (20:unable to get local issuer certificate) [9].

Common mistakes on nginx

What nginx -t and a client reported for each mistake in our runs [9]:

Common mistakes on nginx
Mistakenginx -tWhat the client gets
Intermediate left outtest is successfulunable to get local issuer certificate, unless the client already holds the intermediate
Intermediate first in the filekey values mismatch, test failednothing: nginx does not start
listen 443 without ssltest is successfulwrong version number over https://
Name missing from the certificatetest is successfulno alternative certificate subject name matches target host name
Two blocks claim the same nameconflicting server name … ignoredthe first block's certificate
New files, no reloadnot runthe old certificate

When the key does not match the certificate

OpenSSL returns an error when a private key does not belong to a certificate already set [10]; with the bundle in the wrong order, the key meets the intermediate, and nginx stops with key values mismatch [2][9].

An ECDSA key next to an RSA certificate behaved differently. nginx -t passed and nginx started; each handshake in our runs then failed, and clients got a handshake failure alert. With error_log at the info level, nginx logged no suitable signature algorithm for TLS 1.3 and no shared cipher for TLS 1.2 [9]. At the default level, error, those lines are not written [8].

nginx accepts several certificates of different types in one block, each with its own key [1]: in our runs each client received the certificate of the type it accepted [9].

Automate this with Bridge SSL

Bridge SSL is the certificate layer of Bridge CDN: the certificate is issued in the layer that terminates TLS for your site, and it is used there. There is no copy to make, no chain to assemble and no service to reload, and no file on your server to go stale. Renewal is attempted ahead of expiry; the platform does not publish a guaranteed time for issuing or renewal, so keep an outside check on what your hostnames present. See Bridge CDN.

FAQ

How do I install an SSL certificate on nginx?

Combine your certificate and its intermediates into one file, certificate first. In a server block with listen 443 ssl, point ssl_certificate at it and ssl_certificate_key at the key, then run nginx -t and nginx -s reload. [1][3][5]

Does nginx need the chain in the same file as the certificate?

Yes: your certificate first, then the intermediates. Without them, clients that already hold the intermediate connect, and others fail with unable to get local issuer certificate. [1][9]

Why does my nginx site answer with plain HTTP on port 443?

The listen directive lacks the ssl parameter. Browsers then report ERR_SSL_PROTOCOL_ERROR or SSL_ERROR_RX_RECORD_TOO_LONG, and curl wrong version number. [3][9]

Do I need to restart nginx after installing a certificate?

No. Run nginx -t, then nginx -s reload: old worker processes finish serving their clients, and a configuration that fails to load leaves the old one running. [5][9]

How do I enable HTTP/2 with SSL on nginx?

On nginx 1.25.1 and later, add http2 on; to the server block. Before 1.25.1, use listen 443 ssl http2;, a form newer versions accept with a deprecation warning. [3][4][9]

Why is nginx serving the wrong certificate?

No server block matches the hostname, so the default server for the port answers: the block marked default_server, or the first one. Add the name to server_name in the right block, or give it a block of its own. [3][9]

Sources

  1. nginx, Module ngx_http_ssl_module: ssl_certificate, ssl_certificate_key, ssl_protocols.

  2. nginx, Configuring HTTPS servers.

  3. nginx, Module ngx_http_core_module: listen (ssl, default_server, http2), the $scheme variable.

  4. nginx, Module ngx_http_v2_module: http2.

  5. nginx, Command-line parameters and Controlling nginx: changing configuration.

  6. nginx, Server names: virtual server selection.

  7. nginx, Module ngx_http_proxy_module: the proxy_ssl_* directives.

  8. nginx, Core functionality: user, error_log.

  9. Our runs: nginx 1.30.6 built from source with OpenSSL 3.0.13 on Linux, test certificates from our own lab root; Chrome and Firefox against a plain HTTP listener, from the Secure Connection Failed page.

  10. OpenSSL 3.0 manual, SSL_CTX_use_certificate: SSL_CTX_use_PrivateKey.