I have a dockerised node.js/express app running on an aws ec2 instance container. Right now my app is running on a domain name hosted by aws route 53. When I go to the domain name the protocol used is http. How can I set up https for a node.js server running on docker in aws EC2?
This is what I have done so far.
Now this is where I get stuck. From this documentation I can see that I need a copy of the certificate in my server for https to work with my server.
I am new to devops and aws. If you could outline your answer in steps, from the beginning, it would be much appreciated. I have a dockerised node.js/express app running on an aws ec2 instance container accessible by an aws route53 hosted domain name. From here how can I change the default connection protocol from http to https?
Elastic Load Balancer is nice enough to append an extra request header to the request (x-forwarded-proto) which tells us from which protocol the request originated from. We can use this in an Express middleware to do redirections:
function forceHttps(req, res, next) {
const xfp =
req.headers["X-Forwarded-Proto"] || req.headers["x-forwarded-proto"];
if (xfp === "http") {
res.redirect(301, `https://${hostname}${req.url}`);
} else {
next();
}
}
server.use(forceHttps);
or use the npm package https://www.npmjs.com/package/@crystallize/elasticloadbalancer-express-force-https
const express = require('express');
const forceHttps = require('@crystallize/elasticloadbalancer-express-force-https');
const server = express();
server.use(forceHttps());
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.