nest icon indicating copy to clipboard operation
nest copied to clipboard

How do you set up SSL?

Open Kurdiez opened this issue 9 years ago • 6 comments

Is there a way to start the Nest application with SSL cert options so I can have a HTTPS server?

For now I manually call the init() function on the Nest app and start the HTTPS server with express app manually.


let options = {
    key: fs.readFileSync(Config.SSL_KEY_PATH),
    cert: fs.readFileSync(Config.SSL_CERT_PATH)
};

let httpsServer = https.createServer(options, expressApp);

const app = NestFactory.create(ApplicationModule, expressApp);
app.useGlobalPipes(new ValidatorPipe());
app.init();

httpsServer.listen(httpsPort);

I was just wondering if there is any standard way of doing SSL with Nest. Couldn't find it in the documentation.

Kurdiez avatar Jul 15 '17 19:07 Kurdiez

Hey @jiminssy, Yep, that's why the init() method exists. Your solution is the example how to use HTTPS, there's no other option right now.

kamilmysliwiec avatar Jul 16 '17 12:07 kamilmysliwiec

full demo here if anyone needs a reference: https://github.com/EveripediaNetwork/backend-api/blob/master/src/main.ts

k26dr avatar Nov 26 '18 23:11 k26dr

For me was enough this code in the main.ts:

async function bootstrap() {
  const fs = require('fs');
  const keyFile  = fs.readFileSync(__dirname + '/../ssl/mydomain.com.key.pem');
  const certFile = fs.readFileSync(__dirname + '/../ssl/mydomain.com.crt.pem');

  const app = await NestFactory.create(AppModule, {
    httpsOptions: {
      key: keyFile,
      cert: certFile,
    }});
  
//...
}

miguelcamposdev avatar Nov 29 '18 22:11 miguelcamposdev

Thanks @camposmiguel for the above answer. If I wish to run it locally on my machine over https, then how can i generate the key and crt certificates? I know this may sound trivial, but I am new to this party

anishLearnsToCode avatar May 06 '19 22:05 anishLearnsToCode

It's possible to generate a Let' encrypt free certificate to use in localhost. Here you have the Let's Encrypt documentation: https://letsencrypt.org/docs/certificates-for-localhost/

miguelcamposdev avatar May 07 '19 07:05 miguelcamposdev

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Sep 23 '19 17:09 lock[bot]