简体   繁体   中英

Adding a SSL certificate to an Express Site

I have a small site that I have been tasked with adding an ssl certificate to. The site runs in express with this code

#!/usr/bin/env node
var debug = require('debug')('one-west-end'),
    app = require('../app'),
    fs = require('fs'),
    http = require('http'),
    https = require('https'),
    express = require('express');

var keyPath = '../ssl/key.key';
var certPath = '../ssl/cert.crt';
var caPath = '../ssl/cert.crt';

var port = 3000;

if (app.get('env') === 'production') {
    port = 80;
}

if (fs.existsSync(keyPath) && fs.existsSync(certPath)) {
    port = 443;

    var options = {
        key: fs.readFileSync(keyPath),
        cert: fs.readFileSync(certPath),
    };

    var server = https.createServer(options, app).listen(port, function(){
      console.log("Express server listening on port " + port);
    });

} else {

    var server = app.listen(port, function() {
        console.log('Server started on port ' + port);
        debug('Express server listening on port ' + port);
    });

}

I did not create the necessary files to create the files but this is the guide that was used, which essentilly calls for:

> openssl genrsa -out ~/domain.com.ssl/domain.com.key 2048`
> openssl req -new -key ~/domain.com.ssl/domain.com.key -out ~/domain.com.ssl/domain.com.csr`

The only thing that I receieved from the person who bought the certificate was a zip file containg two .crt files. The content for one is:

-----BEGIN CERTIFICATE-----
....
-----END CERTIFICATE-----

And the other one

-----BEGIN CERTIFICATE-----
....
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
....
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
....
-----END CERTIFICATE-----

I'm using the key file created on in the initial step in the express file, but I'm not sure what file is my CA and which one is my cert. I have tried a few different variations, but I always seem to get a "This webpage is not available" error in chrome.

The first file is likely your certificate, and the second file contains the CA chain. Node requires each certificate in the CA chain to be passed separately in an array. It does not support multiple concatenated certificates in a single file.

Each certificate needs to be put in its own file (ie ca1.crt , ca2.crt , and ca3.crt ) and read separately.

https.createServer({
    key: fs.readFileSync('domain.com.key'),
    certificate: fs.readFileSync('domain.com.crt'),
    ca: [fs.readFileSync('ca1.crt'), fs.readFileSync('ca2.crt'), fs.readFileSync('ca3.crt')]
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM