Skip to content

How to Install an SSL Certificate on Nginx

A practical walkthrough for installing an SSL certificate on Nginx, from preparing the files to testing the configuration, redirecting HTTP and verifying the result.

10 min readUpdated Nov 21, 2026By XPASS Editorial Team

How to Install an SSL Certificate on Nginx

Installing an SSL certificate on Nginx is a small job once you know which files are involved and where each directive goes. It is also a job where small mistakes show up in front of visitors as a browser warning, so it pays to work in a clear order: prepare the files, edit the configuration, test it, reload, and then verify from the outside.

This guide walks through that order on a typical Linux server. It assumes you already have a certificate issued for your domain and the private key that was created with your certificate request. If you are still at the earlier stage, the article on generating a CSR and private key securely is a good place to begin, although the closest related reading for the web server itself is the guide to installing SSL on Apache, which covers the same ideas with different directives.

What you need before you start

Before you touch the server, gather four things and check each one.

  1. The server certificate. This is the file issued for your hostname. It is usually a text file that begins with -----BEGIN CERTIFICATE-----.
  2. The intermediate certificates. Certificate authorities normally sign with an intermediate certificate, not directly with a root. Browsers trust the root, so your server must also send the intermediate that links your certificate to it. The issuer supplies this as a "CA bundle" or "chain" file.
  3. The private key. This file was created on your server (or on your computer) at the same time as the certificate request. Nginx cannot serve HTTPS without it, and nobody else should ever have a copy.
  4. Shell access with administrator rights. You need to edit files under /etc/nginx and reload the service. On most distributions that means sudo.

Also confirm that the hostname on the certificate matches the name visitors type. A certificate for example.com does not automatically cover www.example.com or shop.example.com; the names the certificate covers are listed inside it. You can print them with openssl x509 -in cert.pem -noout -text and read the "Subject Alternative Name" section.

Finally, make sure port 443 is open in the server firewall and in any cloud security group. A perfectly configured Nginx is still unreachable if the port is closed.

Step 1: Build the certificate file Nginx expects

Nginx reads a single certificate file for the ssl_certificate directive. That file must contain your server certificate first, followed by the intermediate certificate or certificates, in order. The root certificate is normally left out, because clients already hold it.

If your issuer gave you separate files, combine them:

cat example.com.crt intermediate.crt > fullchain.pem

The order matters. The server certificate goes first, then the intermediate that signed it, then any further intermediate above that. Putting the intermediate first is one of the most common causes of "chain incomplete" errors in online checkers.

Open the combined file in a text editor and check that each block starts with a BEGIN CERTIFICATE line and ends with an END CERTIFICATE line, with a line break between blocks. A missing line break can silently merge two blocks into one unreadable block.

Choose a tidy location for the files. Many administrators use /etc/nginx/ssl/ or /etc/ssl/private/ for the key. What matters is that the paths in your configuration match the real locations exactly.

Step 2: Place the files and set safe permissions

Copy the files to the server with a secure method such as scp or sftp, never by email or chat. Then set permissions so that only the right accounts can read the key.

A common pattern looks like this:

  • The certificate chain file is readable by everyone (mode 644), because it is public by design.
  • The private key is readable only by root (mode 600) or by root and a dedicated group.

Nginx reads certificates as root when it starts or reloads, before the worker processes drop privileges, so a key owned by root with mode 600 works on a normal installation. If you run Nginx in an unusual way, for example inside a container with a non-root user, adjust ownership to match.

Do not leave a copy of the private key in a web-accessible folder, in a shared download directory or in a backup that other people can open. A leaked private key means the certificate must be replaced, even if it has not expired.

Step 3: Edit the server block

Find the configuration file for your site. On Debian and Ubuntu it is often in /etc/nginx/sites-available/ and linked from sites-enabled/. On other systems it may live in /etc/nginx/conf.d/. Open it and look for the server block that handles your domain.

A minimal HTTPS server block looks like this:

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

    ssl_certificate     /etc/nginx/ssl/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;

    ssl_protocols TLSv1.2 TLSv1.3;

    root /var/www/example.com;
    index index.html;
}

What each line does:

Directive Purpose
listen 443 ssl; Accepts TLS connections on the standard HTTPS port. The second line does the same for IPv6.
server_name Lists the hostnames this block answers. It should match the names on the certificate.
ssl_certificate Path to the file with your certificate followed by the intermediates.
ssl_certificate_key Path to the matching private key.
ssl_protocols Which TLS versions are allowed. Modern versions only is the sensible default.

Newer Nginx releases also accept a separate http2 on; directive to enable HTTP/2, while older ones use listen 443 ssl http2;. Check which version you run with nginx -v and follow its documentation rather than copying a line that may be deprecated.

For cipher suites, the defaults shipped with a current Nginx and OpenSSL are usually reasonable. Avoid pasting long cipher lists from old tutorials, because they can disable modern options or enable outdated ones. If you need guidance on protocol versions, the TLS 1.3 specification explains what the current protocol requires.

Step 4: Redirect HTTP to HTTPS

Installing a certificate does not by itself move visitors to HTTPS. People who type the bare domain or follow an old link will still arrive on port 80. Add a second server block that redirects them:

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

The 301 status marks the move as permanent, and $request_uri keeps the path and query string so deep links continue to work. Using return is simpler and faster than a rewrite rule for this purpose.

Be careful with one detail: if you use a certificate authority validation method that fetches a file over HTTP from your site, a blanket redirect can interfere with it on some setups. In that case keep the validation path reachable on port 80 and redirect everything else.

Step 5: Test, reload and verify

Never reload Nginx without testing first. Run:

sudo nginx -t

A healthy result reports that the syntax is ok and the test is successful. If it prints an error, it names the file and line number. Typical messages and their meanings:

Message Likely cause
cannot load certificate ... No such file The path is wrong or the file is not readable.
PEM_read_bio ... no start line The file is not in PEM format, or has stray characters before the BEGIN line.
key values mismatch The key and certificate do not belong together.
bind() to 0.0.0.0:443 failed Another process already uses port 443, or a duplicate listen line exists.

When the test passes, apply the change:

sudo systemctl reload nginx

A reload loads the new configuration while keeping existing connections alive, so it is the right choice on a site that is in use. A full restart briefly drops connections.

Then verify from outside the server. Open the site in a browser and check the padlock details, which show the issuer and the validity dates. From a terminal you can run openssl s_client -connect example.com:443 -servername example.com and look at the certificate chain it prints; you should see your certificate followed by the intermediate, with a verification result that reports success. Do this from a different machine if possible, so you see what visitors see and not what the server sees.

A plain example, start to finish

Imagine a small shop on a virtual server with the domain shop.example.com. The owner receives three files from the issuer: shop.example.com.crt, ca-bundle.crt and keeps shop.example.com.key from the earlier request.

  1. They join the certificate and bundle into fullchain.pem, with the server certificate first.
  2. They copy both files to /etc/nginx/ssl/ and set the key to mode 600.
  3. They add the listen 443 ssl; server block with the two path directives, plus a port 80 block that returns a 301 redirect.
  4. They run nginx -t, which passes, and reload the service.
  5. They open the site, see the padlock, click through to a product page to confirm it is secure, and run the openssl s_client command from a laptop to confirm the chain.

The whole job takes a few minutes. Most of the time saved comes from the checks done in step one, not from clever commands.

Limits and what to watch for

A few points catch people out after a technically successful installation.

Mixed content. If a page loads images, scripts or stylesheets over http://, browsers may block them or show a warning even though the certificate is fine. Search your templates and database for hard-coded http:// URLs and switch them to https:// or to relative paths. Guides such as installing SSL on WordPress cover this problem for that platform in more detail.

Names that the certificate does not cover. If you add a new subdomain later, check whether the existing certificate lists it. A single-name certificate will not cover it, and the choice between single-name, multi-name and wildcard certificates depends on how many hosts you run. For API setups with several hostnames, see how to choose a TLS certificate for a multi-host API.

Expiry. Certificates have a limited lifetime. Put the expiry date in a calendar, or set up monitoring that warns you well before the date. An expired certificate produces a full-page browser warning that most visitors will not click past.

Renewal replaces files, not just dates. When you renew, you receive a new certificate file, and often a new intermediate. Rebuild the chain file, keep the key that matches, test with nginx -t and reload again. If you generated a fresh key for the renewal, update the key path too.

HSTS. The Strict-Transport-Security header tells browsers to use only HTTPS for your domain for a stated period. It is a useful hardening step, but it is hard to undo if something is wrong. Turn it on only when every page and subdomain you care about works over HTTPS, and begin with a short period.

Multiple sites on one IP. Nginx uses Server Name Indication to choose the right certificate when several server blocks share an address. All modern clients support this. If a visitor sees the wrong certificate, check server_name and make sure each block has its own ssl_certificate lines.

Backups. Keep an encrypted copy of the certificate, chain and key somewhere safe. If the server is rebuilt, you will not have to ask the issuer to reissue the certificate.

A short troubleshooting checklist

If something looks wrong after the reload, go through these checks in order:

  1. Run sudo nginx -t again and read the message carefully.
  2. Check sudo tail /var/log/nginx/error.log for certificate or permission errors.
  3. Confirm the chain file starts with the server certificate and contains the intermediate.
  4. Compare the public key in the certificate and in the private key. If the two fingerprints differ, the files are not a pair.
  5. Confirm port 443 is open from the outside, for example with a connection test from another network.
  6. Clear the browser cache or try a private window, since browsers sometimes cache a previous certificate error.
  7. Check the system clock. A server with a wrong date can make a valid certificate look expired or not yet valid.

Working through the list once usually isolates the fault within a few minutes.

Summary

To install an SSL certificate on Nginx, build a chain file with your certificate first and the intermediates after it, store the private key with strict permissions, point ssl_certificate and ssl_certificate_key at them inside a listen 443 ssl server block, redirect port 80 with a 301, run nginx -t, reload, and verify from another machine. Afterwards, watch for mixed content, names the certificate does not cover, and the expiry date. When you are ready to choose or renew a certificate, the SSL certificates page shows the options that are currently available.

Frequently asked questions

Which files does Nginx need for HTTPS?

Nginx needs two files: a certificate file that contains your server certificate followed by the intermediate certificates, and the private key that was created together with your certificate request. The two directives are ssl_certificate and ssl_certificate_key.

Why does Nginx say 'key values mismatch' when I reload?

The private key does not belong to the certificate you pointed at. This usually happens when a new key was generated for a renewal but the old certificate file is still referenced, or the wrong file was copied. Compare the public key fingerprints of both files to confirm.

Do I restart or reload Nginx after changing the certificate?

Run nginx -t first to test the syntax, then reload the service. A reload applies the new certificate without dropping existing connections, so it is the safer choice on a live site.

Why do browsers still warn me after installing the certificate?

The usual causes are a missing intermediate certificate in the chain file, a certificate issued for a different hostname, a certificate that has expired, or a page that loads some resources over plain HTTP.

Should I turn on HSTS straight away?

No. HSTS tells browsers to refuse plain HTTP for your domain for a period of time. Enable it only after HTTPS works on every page and subdomain you care about, and start with a short period.

About the author

Bob Kanokphon Thongdee, founder of XPASS VPN

Bob Kanokphon Thongdee

  • Founder of XPASS VPN
  • Co-founder of eSIMonline.asia
  • More than 7 years in online marketing
  • Founder of Cojourney Visa and Cojourney Travel

Need an SSL certificate?

Pick the certificate type that fits your site and see the price before you check out.

SSL from $14.99

See SSL certificates

Keep reading

SSL Guides

How to Install SSL on a WordPress Website

A practical walkthrough for moving WordPress to HTTPS: installing the certificate, updating URLs, fixing mixed content, redirecting and checking the result.

10 min read

SSL Guides

How to Install an SSL Certificate on Apache

A clear walkthrough for installing an SSL certificate on Apache, covering mod_ssl, the HTTPS virtual host, the certificate chain, redirects and verification.

10 min read

SSL Guides

How to Choose a TLS Certificate for an API With Many Hostnames

APIs often serve many hostnames across regions, tenants and versions. Learn how to compare single, SAN and wildcard certificates and plan rotation for API clients.

10 min read