Configure SSL for Node.js: HTTPS Server Setup
To add SSL for Node.js, use the built-in https module with a certificate and its matching private key, and about ten lines of code. Node.js can speak HTTPS on its own. The harder questions are where to terminate TLS, how to run on port 443 without root, and how to pick up a renewed certificate.
SSL for Node.js, option 1: terminate TLS in the app
Get a certificate first. If you have a domain pointing at the server, a free certificate from Let's Encrypt works; see Free SSL Certificate with Certbot: Step by Step. Certbot writes the files to /etc/letsencrypt/live/example.com/, and the code below reads copies in /etc/ssl/app/ so the Node process can open them. Then create the server:
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('/etc/ssl/app/privkey.pem'),
cert: fs.readFileSync('/etc/ssl/app/fullchain.pem'),
minVersion: 'TLSv1.2',
};
https.createServer(options, (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('secure hello\n');
}).listen(8443);Use fullchain.pem for the cert option, not just the leaf certificate. If you leave out the intermediate certificate, desktop browsers may cope but many API clients and mobile apps will report an incomplete chain. The same code works with Express: pass app to https.createServer(options, app).
Running on port 443
Ports below 1024 are privileged on Linux. Running Node as root to bind port 443 is a bad idea. Better options:
- Listen on 8443 and redirect 443 to it with the firewall.
- Grant only the bind capability:
sudo setcap 'cap_net_bind_service=+ep' $(which node). - Use a reverse proxy on 443 and keep Node on localhost (Option 2).
Redirect HTTP to HTTPS
Run a second tiny server on port 80 that sends a permanent redirect:
require('http').createServer((req, res) => {
const host = (req.headers.host || '').split(':')[0];
res.writeHead(301, { Location: 'https://' + host + req.url });
res.end();
}).listen(80);Reload certificates without a restart
Let's Encrypt certificates last 90 days today, 64 days from 10 February 2027 and 45 days from 16 February 2028 (Let's Encrypt announcement), and Certbot renews them in place. Node reads the files only at startup, so it keeps serving the old certificate until it restarts. You can avoid a restart with setSecureContext (Node.js TLS docs):
const server = https.createServer(options, handler);
function reload() {
server.setSecureContext({
key: fs.readFileSync('/etc/ssl/app/privkey.pem'),
cert: fs.readFileSync('/etc/ssl/app/fullchain.pem'),
});
}
process.on('SIGHUP', reload);Then add a Certbot deploy hook that copies the new files and sends SIGHUP to the process. Hook details are in automating SSL renewal with Certbot. Do not make the /etc/letsencrypt/live directory world-readable just so Node can open it; copy the files to a location owned by the service account instead.
SSL for Node.js, option 2: terminate TLS at a reverse proxy
Most production Node apps sit behind Nginx, Caddy, HAProxy or a cloud load balancer. The proxy handles certificates, renewals and protocol settings, and Node serves plain HTTP on 127.0.0.1. Your code then needs to trust the forwarded scheme. In Express:
const app = require('express')();
app.set('trust proxy', 1); // trust the first proxy hop
app.use((req, res, next) => {
if (req.secure) return next();
res.redirect(301, 'https://' + req.headers.host + req.url);
});Only set trust proxy when a proxy really sits in front, or a client could spoof X-Forwarded-Proto. For proxy setups, see configuring free SSL for a reverse proxy. For protocol choices, Best TLS Configuration for Nginx and Apache is a good baseline.
Outbound HTTPS from Node
Node verifies server certificates by default. Do not set NODE_TLS_REJECT_UNAUTHORIZED=0 or rejectUnauthorized: false to silence an error; that switches off the check that makes TLS useful. If a private CA is involved, point NODE_EXTRA_CA_CERTS at its certificate file instead.
Verify the result
| Command | What it shows |
|---|---|
| curl -vI https://example.com | Handshake, protocol and certificate subject |
| openssl s_client -connect example.com:443 | Full chain sent by the server |
| curl -I http://example.com | Whether the 301 redirect works |
A full walkthrough of the OpenSSL checks is in testing SSL configuration with OpenSSL.
Common mistakes
- Serving only
cert.pemwithout the intermediate chain, which breaks some clients. - Running the whole app as root to reach port 443.
- Committing
privkey.pemto a repository. Keep keys out of version control and out of container images. - Forgetting that a renewal on disk does nothing until Node reloads the context or restarts.
- Turning off certificate verification on outbound calls to get past a development error and shipping it.
Each of these is easy to catch with a short review before launch, and none of them needs more than a few lines to fix.
Related reading: free SSL on nginx, renewing a free SSL certificate and mTLS for APIs.
Frequently Asked Questions
Does Node.js need a certificate if Nginx is in front?
No. Let the proxy terminate TLS and serve Node on 127.0.0.1 over plain HTTP, then tell the app to trust the forwarded scheme.
Why does Node keep serving the old certificate after renewal?
It read the files at startup. Either reload the context with setSecureContext or restart the process from a Certbot deploy hook.
Can Node.js bind port 443 without root?
Not directly on Linux. Put a proxy or a load balancer in front, or grant the capability to the binary.
✅ Post-Installation Verification & SEO Checklist
After applying the steps above, run through this 5-point checklist to prevent browser warnings and protect your Google SEO rankings:
- Verify full CA chain trust and TLS 1.3 support using our Live SSL Checker.
- Scan for Mixed Content HTTP links and check HSTS/CSP headers on Security Headers Checker.
- Enforce a permanent 301 HTTP-to-HTTPS redirect with our HTTPS Redirect Generator.
- Confirm both root and
wwwversions resolve cleanly to one canonical HTTPS URL. - Verify your HTTPS property and
sitemap.xmlinside Google Search Console and Bing Webmaster Tools.