Skip to content

Instantly share code, notes, and snippets.

@hengkiardo
Created May 25, 2014 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hengkiardo/0136c8c36a868063902f to your computer and use it in GitHub Desktop.
Save hengkiardo/0136c8c36a868063902f to your computer and use it in GitHub Desktop.
### Definitions:
1. A .csr file is a certificate signing request which initiates your certificate request with a certificate provider and contains administrative information about your organization.
2. A .key file is the private key used to encrypt your site’s SSL-enabled requests.
3. .pem and .crt extensions are often used interchangeably and are both base64 ASCII encoded files. The technical difference is that .pem files contain both the certificate and key whereas a .crt file only contains the certificate. In reality this distinction is often ignored.
## Generate SSL Keys for Heroku:
$ openssl genrsa -des3 -out server.orig.key 2048
$ openssl rsa -in server.orig.key -out server.key
$ openssl req -new -key server.key -out server.csr // Used for request of cert generation
## Self signed Cert for test env.
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
## Save the keys you receive - server.crt and intermediate.crt for the certificate authority’s certificate. Combine your key and the intermediate key using
$ cat server.crt intermediate.crt > server.pem
## Add the cert and key to heroku
$ heroku certs:add server.pem server.key
## Update Cert for some reason:
$ heroku certs:update server.pem server.key
## Test the cert in heroku
$ heroku certs
$ heroku certs:info
/**
* Create Express https Server
**/
var httpsOptions = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
express.createServer(httpsOptions).listen(443);
/**
* Nodejs on Heroku is served via nginx, you can access the “x-forwarded-proto” in request’s header
**/
app.use(function(req, res, next) {
var reqType = req.headers["x-forwarded-proto"];
reqType == 'https' ? next() : res.redirect("https://" + req.headers.host + req.url);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment