【发布时间】:2022-01-20 12:23:17
【问题描述】:
我最近使用 NodeJS 为 Web 服务器设置 SSL 证书,类似于以下方式,一切正常:
process.env.HTTPS_PORT = 3000; // Listen on port 3000 for HTTPS.
process.env.HTTP_PORT = 6000; // Listen on port 6000 for HTTP.
// Create HTTPS server all workflow.
https
.createServer(
{
key: await fs.readFile('/etc/pki/tls/private/key.txt'),
cert: await fs.readFile('/etc/pki/tls/certs/xxxxxxxxxxxxxx.crt'),
ca: await fs.readFile('/etc/pki/tls/certs/gd_bundle-g2-g1.crt'),
},
app
)
.listen(process.env.HTTPS_PORT, () => {
console.log(`Main server is running on: ${process.env.HTTPS_PORT}`);
})
.on('error', (err) => {
console.log(`Failed to start main server: ${err}`);
});
// Create HTTP server for redirection to HTTPS only.
http.createServer(app)
.listen(process.env.HTTP_PORT, () => {
console.log(`Redirection server is running on: ${process.env.HTTP_PORT}`);
})
.on('error', (err) => {
console.log(`Failed to start redirection server: ${err}`);
});
我正在监听两个端口,一个服务 HTTPS,另一个服务 HTTP。 HTTP 服务器的目的是仅重定向到我设置了路由的 HTTPS。
此设置适用于服务器的 FQDN (app.subdomain1.domain.com)。服务器还有一个 CNAME (web.subdomain2.domain.com)。从我所做的research 看来,CNAME 似乎需要单独处理,因为浏览器仍然需要用户请求的 URL 的有效证书。预计用户将使用任一 URL 来访问应用程序。
我找不到太多关于如何使用 NodeJS/ExpressJS 为此类 CNAME 设置 SSL 证书的信息。任何有关这方面的信息都会非常有帮助。
【问题讨论】:
标签: node.js express ssl https cname