Putting a Node.js application online handles SSL a little differently from a PHP site. With PHP, the web server or hosting panel manages the certificate, while a Node.js app is often its own HTTP server listening on a port such as 3000. That raises a choice: put Nginx in front, or load the certificate directly inside Node.js. This article walks through both with working configuration and explains when each one fits.
Two approaches
With a reverse proxy, Nginx owns ports 80 and 443, handles the certificate and forwards requests to the app over plain HTTP on an internal port. With the direct approach, Node's built-in https module reads the certificate and listens on 443 itself, which removes a layer but makes port permissions, key access and renewal your responsibility.
| Criterion | Via Nginx | Directly in Node.js |
|---|---|---|
| Renewal | Handled by Certbot | Needs a hook and reload |
| Several apps per server | Easy, split by name | Hard, 443 is one process |
| Static files, compression, limits | Built into Nginx | Must be coded |
For most projects Nginx is easier and safer; direct TLS suits small internal services or places where Nginx cannot be installed.
Option one: Nginx reverse proxy
Point an A record for app.example.uz to the server and create a plain HTTP block. The Upgrade and Connection headers keep WebSockets working, and X-Forwarded-Proto tells the app the original request was HTTPS.
server {
listen 80;
server_name app.example.uz;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Certbot's Nginx plugin then obtains the certificate, adds the 443 block and offers an HTTP to HTTPS redirect. Renewal runs from Certbot's system timer and reloads Nginx automatically.
sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d app.example.uz
Behind the proxy, the app sees every request from 127.0.0.1 over HTTP. Without trust proxy in Express, req.secure is wrong, secure cookies may not be sent and the proxy address replaces the client IP. Bind the app to localhost so port 3000 is not reachable from the internet unencrypted.
const express = require('express');
const app = express();
app.set('trust proxy', 'loopback');
app.listen(3000, '127.0.0.1');
Option two: TLS inside Node.js
setSecureContext loads a new certificate without stopping the server, so we tie it to SIGHUP.
const https = require('https');
const fs = require('fs');
const app = require('./app');
const dir = '/etc/myapp/ssl';
const load = () => ({
key: fs.readFileSync(`${dir}/privkey.pem`),
cert: fs.readFileSync(`${dir}/fullchain.pem`),
});
const server = https.createServer(load(), app);
server.listen(443);
process.on('SIGHUP', () => server.setSecureContext(load()));
Two hurdles remain. On Linux only root can bind ports below 1024, and running the app as root is risky; granting the Node binary just that capability is one solution, which may need repeating after Node upgrades:
sudo setcap 'cap_net_bind_service=+ep' "$(readlink -f "$(which node)")"
Let's Encrypt keys under /etc/letsencrypt are readable by root only. Rather than opening that directory, use a Certbot deploy hook to copy the key into a directory owned by the app user and signal the process:
#!/bin/sh
# /etc/letsencrypt/renewal-hooks/deploy/myapp.sh
D=/etc/letsencrypt/live/app.example.uz
[ "$RENEWED_LINEAGE" = "$D" ] || exit 0
install -o myapp -g myapp -m 600 "$D/privkey.pem" /etc/myapp/ssl/privkey.pem
install -o myapp -g myapp -m 644 "$D/fullchain.pem" /etc/myapp/ssl/fullchain.pem
systemctl kill -s HUP myapp.service
Example: a delivery service API
A small Tashkent delivery service built a Node.js API for its courier app at app.misol.uz. At first Express ran on 443 as root and the certificate was renewed by hand; three months later it expired and the courier app stopped connecting. The team moved the API to 127.0.0.1:3000 under a normal user, put Nginx in front with Certbot auto-renewal and enabled trust proxy. When an admin panel was later added to the same server, it only needed a new server block.
Verifying the result
curl -I https://app.example.uz
openssl s_client -connect app.example.uz:443 -servername app.example.uz </dev/null 2>/dev/null \
| openssl x509 -noout -subject -dates
On shared hosting, Node.js support and SSL options depend on the plan; sayt.uz customers can check this in the cabinet or through support.