如何使用HTTPS库在Node.js中使用有效证书托管HTTPS服务器

我想托管自己的https服务器,但是生成的证书无效,并且当从浏览器访问时,它表示连接不安全。

laiyong369 回答:如何使用HTTPS库在Node.js中使用有效证书托管HTTPS服务器

您可以在NodeJS代码库中实现它,也可以使用http proxyNginxApache一起使用

const crypto = require('crypto'),fs = require("fs"),http = require("http");

var privateKey = fs.readFileSync('privatekey.pem').toString();
var certificate = fs.readFileSync('certificate.pem').toString();

var credentials = crypto.createCredentials({key: privateKey,cert: certificate});

var handler = function (req,res) {
  res.writeHead(200,{'Content-Type': 'text/plain'});
  res.end('Hello World\n');
};

var server = http.createServer();
server.setSecure(credentials);
server.addListener("request",handler);
server.listen(8000);

建议使用Nginx方法

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04

在Vhost中添加SSL文件

如果要使用免费的SSL,请尝试certbot,它将自动在vhost中安装SSL

https://certbot.eff.org/

本文链接:https://www.f2er.com/2676012.html

大家都在问