【问题标题】:How do I setup SSL certificates for a web server's CNAME with NodeJS / ExpressJS?如何使用 NodeJS / ExpressJS 为 Web 服务器的 CNAME 设置 SSL 证书?
【发布时间】: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


    【解决方案1】:

    证书必须与 URL 中给出的域匹配。最简单的方法是拥有一个包含所有域的单一多域证书。另一种方法是拥有多个证书并为每个证书创建不同的上下文。见server.addContext(hostname, context)Serve two https hostnames from single node process & port

    【讨论】:

      猜你喜欢
      • 2021-06-23
      • 1970-01-01
      • 1970-01-01
      • 2016-06-26
      • 2013-11-01
      • 2020-12-17
      • 2012-04-13
      • 2013-05-24
      相关资源
      最近更新 更多