ssl Nodejs https结合cpanel

idv4meu8  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(175)

我尝试在我的nodejs服务器上运行https,如下所示:

var http = require('http');
var https = require('https');
var fs = require('fs');
var express = require('express');
var privateKey  = fs.readFileSync('server.key', 'utf8');
var certificate = fs.readFileSync('server.crt', 'utf8');

var credentials = {key: privateKey, cert: certificate};
var app = express();

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

httpServer.listen(8080, ()=> {
    console.log('Server started and listening on port 8080...')
});
httpsServer.listen(8443, ()=>{
    console.log('Server started and listening on port 8443...')
});

字符串
当我运行服务器时,只有http url在工作,https超时了。你知道为什么会发生这种情况吗?
我习惯使用cpanel,所以我已经在那里设置了ssl证书,但是当涉及到node时,我碰壁了。

x8goxv8g

x8goxv8g1#

看起来你的ssl文件有问题,你需要给予证书文件和私钥,同时创建https服务器像这样:

var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');

// This line is from the Node.js HTTPS documentation.
var options = {
  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.cert')
};

// Create a service (the app object is just a callback).
var app = express();

// Create an HTTP service.
http.createServer(app).listen(80);
// Create an HTTPS service identical to the HTTP service.
https.createServer(options, app).listen(443);

字符串
希望对你有帮助。

goqiplq2

goqiplq22#

问题是端口没有转发。

相关问题