Detailed explanation of SSL configuration methods for different Node.js versions, including native HTTPS and Express.js framework configuration
| Node.js Version | Status | TLS 1.2 | TLS 1.3 | OpenSSL Version | Recommended Use |
|---|---|---|---|---|---|
| Node.js 12.x | Deprecated | ✅ | ⚠️ Needs Configuration | 1.1.1 | ❌ |
| Node.js 14.x | Stable | ✅ | ✅ | 1.1.1 | ✅ |
| Node.js 16.x | Stable | ✅ | ✅ | 1.1.1 | ✅ |
| Node.js 18.x | LTS | ✅ | ✅ | 3.0.x | ✅ Recommended |
| Node.js 20.x | LTS | ✅ | ✅ | 3.0.x | ✅ |
Node.js can directly use .crt and .key files, or use merged certificate chain files.
# # Merge main certificate and intermediate certificatecat your_domain.crt intermediate_ca.crt > fullchain.crt
# # Ensure private key file exists# your_domain.key
# # Set private key file permissions (owner read-only)chmod 600 your_domain.key
# # Set certificate file permissionschmod 644 fullchain.crt
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('/path/to/your_domain.key'),
cert: fs.readFileSync('/path/to/fullchain.crt')
};
const server = https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('Hello HTTPS!');
});
server.listen(443, () => {
console.log('HTTPS server running on port 443');
});
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('/path/to/your_domain.key'),
cert: fs.readFileSync('/path/to/your_domain.crt'),
ca: [
fs.readFileSync('/path/to/intermediate_ca.crt'),
fs.readFileSync('/path/to/root_ca.crt')
]
};
const server = https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('Hello HTTPS!');
});
server.listen(443);
key:key: Private key file pathcert:cert: Certificate file path (can be merged fullchain.crt)ca:ca: Intermediate certificate array (optional, not needed if cert already contains full chain)const express = require('express');
const https = require('https');
const fs = require('fs');
const app = express();
app.get('/', (req, res) => {
res.send('Hello HTTPS!');
});
const options = {
key: fs.readFileSync('/path/to/your_domain.key'),
cert: fs.readFileSync('/path/to/fullchain.crt')
};
https.createServer(options, app).listen(443, () => {
console.log('Express HTTPS server running on port 443');
});
const express = require('express');
const http = require('http');
const https = require('https');
const fs = require('fs');
const app = express();
app.get('/', (req, res) => {
res.send('Hello!');
});
// HTTP 服务器(重定向到 HTTPS)
http.createServer((req, res) => {
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
res.end();
}).listen(80);
// HTTPS 服务器
const options = {
key: fs.readFileSync('/path/to/your_domain.key'),
cert: fs.readFileSync('/path/to/fullchain.crt')
};
https.createServer(options, app).listen(443, () => {
console.log('HTTPS server running on port 443');
});
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('/path/to/your_domain.key'),
cert: fs.readFileSync('/path/to/fullchain.crt'),
secureProtocol: 'TLSv1_2_method', // 或 TLSv1_3_method
ciphers: 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384'
};
const server = https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('Hello HTTPS!');
});
server.listen(443);
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('/path/to/your_domain.key'),
cert: fs.readFileSync('/path/to/fullchain.crt'),
minVersion: 'TLSv1.2',
maxVersion: 'TLSv1.3',
ciphers: 'TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384'
};
const server = https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('Hello HTTPS!');
});
server.listen(443);
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('/path/to/your_domain.key'),
cert: fs.readFileSync('/path/to/fullchain.crt'),
minVersion: 'TLSv1.2',
maxVersion: 'TLSv1.3',
ciphers: [
'TLS_AES_256_GCM_SHA384',
'TLS_AES_128_GCM_SHA256',
'ECDHE-RSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES256-GCM-SHA384',
'ECDHE-RSA-CHACHA20-POLY1305'
].join(':'),
honorCipherOrder: true
};
const server = https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('Hello HTTPS!');
});
server.listen(443);
const options = {
key: fs.readFileSync('/path/to/your_domain.key'),
cert: fs.readFileSync('/path/to/fullchain.crt'),
minVersion: 'TLSv1.2',
maxVersion: 'TLSv1.3'
};
const options = {
key: fs.readFileSync('/path/to/your_domain.key'),
cert: fs.readFileSync('/path/to/fullchain.crt'),
minVersion: 'TLSv1.2', // // Disable TLS 1.0 and 1.1 rejectUnauthorized: true // // Reject invalid certificates};
const options = {
key: fs.readFileSync('/path/to/your_domain.key'),
cert: fs.readFileSync('/path/to/fullchain.crt'),
minVersion: 'TLSv1.2',
maxVersion: 'TLSv1.3',
ciphers: [
'TLS_AES_256_GCM_SHA384', // // TLS 1.3 'TLS_AES_128_GCM_SHA256', // // TLS 1.3 'ECDHE-RSA-AES128-GCM-SHA256', // // TLS 1.2 'ECDHE-RSA-AES256-GCM-SHA384', // // TLS 1.2 'ECDHE-RSA-CHACHA20-POLY1305' // // TLS 1.2 ].join(':'),
honorCipherOrder: true
};
const express = require('express');
const app = express();
// // Force HTTPS middlewareapp.use((req, res, next) => {
if (req.header('x-forwarded-proto') !== 'https') {
res.redirect(`https://${req.header('host')}${req.url}`);
} else {
next();
}
});
app.get('/', (req, res) => {
res.send('Hello HTTPS!');
});
// // Check protocol (for reverse proxy)app.listen(3000);
const express = require('express');
const enforce = require('express-enforces-ssl');
const app = express();
// 强制 HTTPS
app.use(enforce());
app.get('/', (req, res) => {
res.send('Hello HTTPS!');
});
app.listen(3000);
const http = require('http');
const https = require('https');
const fs = require('fs');
// HTTP 服务器(重定向)
http.createServer((req, res) => {
res.writeHead(301, {
'Location': 'https://' + req.headers['host'] + req.url
});
res.end();
}).listen(80);
// HTTPS 服务器
const options = {
key: fs.readFileSync('/path/to/your_domain.key'),
cert: fs.readFileSync('/path/to/fullchain.crt')
};
https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('Hello HTTPS!');
}).listen(443);
const https = require('https');
const fs = require('fs');
const express = require('express');
const app = express();
// // Security headers setupapp.use((req, res, next) => {
res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('X-Frame-Options', 'DENY');
res.setHeader('X-XSS-Protection', '1; mode=block');
next();
});
app.get('/', (req, res) => {
res.send('Hello HTTPS!');
});
const options = {
key: fs.readFileSync(process.env.SSL_KEY_PATH || '/etc/ssl/private/your_domain.key'),
cert: fs.readFileSync(process.env.SSL_CERT_PATH || '/etc/ssl/certs/fullchain.crt'),
minVersion: 'TLSv1.2',
maxVersion: 'TLSv1.3',
ciphers: [
'TLS_AES_256_GCM_SHA384',
'TLS_AES_128_GCM_SHA256',
'ECDHE-RSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES256-GCM-SHA384',
'ECDHE-RSA-CHACHA20-POLY1305'
].join(':'),
honorCipherOrder: true
};
https.createServer(options, app).listen(443, () => {
console.log('HTTPS server running on port 443');
});
Error: Error: Error: ENOENT: no such file or directory
Solution:
Error: Error: Error: listen EADDRINUSE: address already in use :::443
Solution:
Error: Error: Error: SSL routines:ssl3_read_bytes:sslv3 alert handshake failure
Solution:
Error: Error: Error: listen EACCES: permission denied
Solution:
Solution: Problem: Browser shows certificate warning, indicating incomplete certificate chain
Solution: