[英]How to set up https for a node.js server running on docker in aws EC2
I have a dockerised node.js/express app running on an aws ec2 instance container.我有一个在 aws ec2 实例容器上运行的 dockerised node.js/express 应用程序。 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.现在我的应用程序运行在由 aws route 53 托管的域名上。当我转到域名时,使用的协议是 http。 How can I set up https for a node.js server running on docker in aws EC2?如何为在 aws EC2 中的 docker 上运行的 node.js 服务器设置 https?
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.从该文档中,我可以看到我需要服务器中的证书副本,以便 https 与我的服务器一起使用。
I am new to devops and aws.我是 devops 和 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.我有一个在 aws ec2 实例容器上运行的 dockerised node.js/express 应用程序,可通过 aws route53 托管域名访问。 From here how can I change the default connection protocol from http to https?从这里如何将默认连接协议从 http 更改为 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. Elastic Load Balancer 足够好,可以将额外的请求标头附加到请求 (x-forwarded-proto) 中,它告诉我们请求源自哪个协议。 We can use this in an Express middleware to do redirections:我们可以在 Express 中间件中使用它来进行重定向:
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或使用 npm 包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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.