Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a simple HTTPS Node JS Server on Amazon EC2

I'm trying to create a simple https server on Amazon EC2 to test a third party API.

Here are the steps I've followed:

  1. Created an Amazon EC2 instance, and opened up HTTP and HTTPS ports:

enter image description here

  1. Created simple ssl credentials using

openssl genrsa 2048 > privatekey.pem

openssl req -new -key privatekey.pem -out csr.pem

openssl x509 -req -days 365 -in csr.pem -signkey privatekey.pem -out server.crt

  1. Created a simple node js server
var https = require('https');
var fs = require('fs');
var options = {
  key: fs.readFileSync('./privatekey.pem'),
  cert: fs.readFileSync('./server.crt')
};

https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8080);

When I run the server, and attempt to connect to it using url https://ec2-XX-XXX-XXX-XXX.compute-1.amazonaws.com/, I keep getting a connection refused.

A telnet test also produces:

Trying XX.XXX.XXX.XXX...
telnet: connect to address XX.XXX.XXX.XXX: Connection refused
telnet: Unable to connect to remote host

Can someone please tell me what I need to fix to enable https on this EC2 instance?

like image 475
dpigera Avatar asked Apr 21 '16 15:04

dpigera


Video Answer


1 Answers

Change your listen(8080) to listen(443) unless you have a web server listening on 443 and sending request to node on 8080.

like image 88
Frederic Henri Avatar answered Oct 16 '22 17:10

Frederic Henri