gpt4 book ai didi

node.js - 当我启用 https 网站停止工作

转载 作者:太空宇宙 更新时间:2023-11-03 13:25:29 24 4
gpt4 key购买 nike

我是 nodejs 的新手,我已经在 nodejs 上安装了 ubuntu我也有 comodo 的 SSL 证书

我按照以下步骤操作

1) Login to root account and copy the key file in SSL cert to  /etc/ssl/private/private.key file.
2) Then created file called /etc/ssl/certs/STAR_cert.com.crt and paste the contents of STAR_cert_com.crt into it.

3) Then create file called /etc/ssl/certs/AddTrustExternalCARoot.crt file and paste all the contents of three files present inside "CA and Intermediate Certs" folder.

然后我将我的 app.js 文件配置如下

var sslOptions = {
key: fs.readFileSync('/etc/ssl/private/private.key'),
cert: fs.readFileSync('/etc/ssl/certs/STAR_cert_com.crt'),
requestCert: false,
//ca: fs.readFileSync('/etc/ssl/certs/AddTrustExternalCARoot.crt'),
rejectUnauthorized: false
};

var secureServer = https.createServer(sslOptions,app).listen(443, function(){
console.log("Express server listening on port : " + app.get('port'));
console.log("Mode : " + app.get('mode'));
});

var http = require('http');
http.createServer(function (req, res) {
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
res.end();
}).listen(80);
var secureServer = https.createServer(sslOptions,app).listen(443, function(){
console.log("Express server listening on port : " + app.get('port'));
console.log("Mode : " + app.get('mode'));
});

当我运行我的网站 example.com 时,它重定向到 https://example.com并给我以下错误

>     This webpage is not available
>
> DNS_PROBE_FINISHED_NXDOMAIN

但服务器控制台上没有错误。

如果我在没有 https 的情况下运行网站,网站运行正常

有什么想法吗?

谢谢

最佳答案

您应该将 sslOption json 的 key 和证书参数作为字符串传递,例如,您可以使用 express 4+ 轻松启动 https nodejs 服务器,例如:

 var fs = require('fs');
var options = {
key: fs.readFileSync('your.key').toString(),
cert: fs.readFileSync('your_crt.crt').toString()
};
var https = require('https').Server(options,app);
https.listen(config.port, function() {
console.log('Server started successfully');
});

希望对您有所帮助!

关于node.js - 当我启用 https 网站停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33132098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com