使用SSL的Node.js网站,收到“拒绝连接”错误

时间:2017-11-13 14:04:15

标签: node.js ssl

我已成功通过OpenSSL为我的网站创建了SSL证书和密钥。我将它们上传到服务器上的文件夹中。启动服务器时没有错误。

但是当我尝试使用https://在浏览器中加载网站时,我收到“拒绝连接”错误。

以下是我用来创建https服务器的代码:

var app = require('../app');
    var debug = require('debug')('gameserver:server');
    const https = require('https');
    const fs = require('fs');

    const options = {
        port: normalizePort(process.env.PORT || '80'),
        key: fs.readFileSync('../keys/key.key'),
        cert: fs.readFileSync('../keys/cert.crt')
    };

    var server = https.createServer(options);
    require('../config/socket')(server);

    server.listen(port);
    server.on('error', onError);
    server.on('listening', onListening);

    function normalizePort(val) {
        var port = parseInt(val, 10);

        if (isNaN(port)) {
            return val;
        }

        if (port >= 0) {
            return port;
        }

        return false;
    }

    function onError(error) {
        if (error.syscall !== 'listen') {
            throw error;
        }

        var bind = typeof port === 'string'
            ? 'Pipe ' + port
            : 'Port ' + port;

        switch (error.code) {
            case 'EACCES':
                console.error(bind + ' requires elevated privileges');
                process.exit(1);
                break;
            case 'EADDRINUSE':
                console.error(bind + ' is already in use');
                process.exit(1);
                break;
            default:
                throw error;
        }
    }

    function onListening() {
        var addr = server.address();
        var bind = typeof addr === 'string'
            ? 'pipe ' + addr
            : 'port ' + addr.port;
        debug('Listening on ' + bind);
    }

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

您的密钥/证书有效吗?

以下命令应显示相同的结果:

openssl rsa -noout -modulus -in mykey.key | openssl md5 ; openssl x509 -noout -modulus -in mykey.crt | openssl md5 ;

或者节点只能管理.pem密钥文件,只需将.key和.cm连接在.pem中然后使用.pem而不是.key:

cat server.crt server.key > server.pem

然后验证pem:

openssl verify -CAfile server.pem server.crt;

答案 1 :(得分:0)

我设法解决了这个问题。实际上,它需要是端口443.此外,在createServer函数中,我忘了将app作为参数传递。这是一个有效的例子:

var app = require('../app');
var debug = require('debug')('gameserver:server');
const https = require('https');
const fs = require('fs');

const options = {
    port: normalizePort(process.env.PORT || '80'),
    key: fs.readFileSync('../keys/key.key'),
    cert: fs.readFileSync('../keys/cert.crt')
};

    var server = https.createServer(options, app).listen(443, function() {
    console.log("Express server listening on port 443");
});
require('../config/socket')(server);

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

function normalizePort(val) {
    var port = parseInt(val, 10);

    if (isNaN(port)) {
        return val;
    }

    if (port >= 0) {
        return port;
    }

    return false;
}

function onError(error) {
    if (error.syscall !== 'listen') {
        throw error;
    }

    var bind = typeof port === 'string'
        ? 'Pipe ' + port
        : 'Port ' + port;

    switch (error.code) {
        case 'EACCES':
            console.error(bind + ' requires elevated privileges');
            process.exit(1);
            break;
        case 'EADDRINUSE':
            console.error(bind + ' is already in use');
            process.exit(1);
            break;
        default:
            throw error;
    }
}

function onListening() {
    var addr = server.address();
    var bind = typeof addr === 'string'
        ? 'pipe ' + addr
        : 'port ' + addr.port;
    debug('Listening on ' + bind);
}