Skip to content

Instantly share code, notes, and snippets.

@Dev-Dipesh
Last active August 5, 2016 05:45
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 Dev-Dipesh/aa9d6fbd62b6e824b4f25b64c0af7cc1 to your computer and use it in GitHub Desktop.
Save Dev-Dipesh/aa9d6fbd62b6e824b4f25b64c0af7cc1 to your computer and use it in GitHub Desktop.
const server = restify.createServer();
const https_server = restify.createServer({
certificate: fs.readFileSync('certs/ca.crt'),
key: fs.readFileSync('certs/key.pem'),
name: 'Sellnews-Trends',
}, err => {
console.log(err, err.stack);
});
const setup_server = function(server) {
server.get("/", (req, res, next) => {
res.writeHead(200, {
'Content-Type': 'application/json; charset=utf-8'
});
res.end(JSON.stringify({"status": "Get req."}));
});
}
// Now, setup both servers in one step
setup_server(server);
setup_server(https_server);
// Start our servers to listen on the appropriate ports
server.listen(6066, function () {
console.log("Server started @ 6066");
});
https_server.listen(4043, function () {
console.log("Server started @ 4043");
});
@Dev-Dipesh
Copy link
Author

There are lot of articles out there on the internet to explain how to setup https in a Node.JS app on EC2, but none of them are really well suited.

Above approach is one way, in which I'm setting servers for both http and https and then using iptables to re-route the ports as bleow

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 6066

However, this isn't the best way to do this. Instead, setup and ELB in AWS for you instance and create a certificate in AWS CM (Certificate Manager). Use the certificate in CM while creating ELB, and in ELB use Load Balancer port 443 and Instance Port 80.

Now in main app just run the app on any port and re-route it to port 80.

From above ELB get the DNS (A-record) name and save it in your Domain Provider CNAME records.

Voila! that's it you're done here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment