如何使用来自 ACM 的 AWS 证书使用 Amazon EC2 从端口 80 重定向到 443?
How to redirect from port 80 to 443 with amazon EC2 using AWS certificate from ACM?
我目前有一个 react/node 应用程序位于 /home/ubuntu 的 EC2 ubuntu 实例中。该服务器是一个侦听端口 443 的 https 服务器。当我点击 Public DNS 时,只有在我的 dns 之前添加 https:// 时它才会出现。它按预期工作(没有它,它默认为端口 80 并且应用程序不显示,这是预期的)。
我有一个由亚马逊和亚马逊证书管理器生成的证书。如何将所有流量从端口 80 重定向到端口 443 并将我的亚马逊证书集成到我的实例中?
您需要在您的节点应用程序中将 HTTP 重定向到 HTTPS。网上有很多关于如何做到这一点的例子,例如:
您可以尝试两个选项。
- AWS ABL 重定向
- Nginx
我更喜欢 ALB,因为您不需要在实例级别公开端口,而且您不会在实例上管理任何代理。
If you are using ALB, you can redirect to https from LB rule. A rule
has to have a condition and an action. Since we want to redirect all
traffic that comes in on port 80 to the same URI, just with HTTPS
instead, our condition should be simply “all”. Unfortunately you can’t
just put a single asterisk in for the condition, the closest I’ve been
able to come up with is *.example.com
, where xample.com
is
whatever your domain is.
如果你没有使用ALB,那你可以试试Nginx。
- 您需要在服务器上安装 Nginx
- 在您的实例中允许端口 80
- 在 nginx.conf
中的配置下方
这将重定向所有
server {
listen 80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
这将重定向特定站点。
server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
}
你的问题的另一个澄清
How to integrate my amazon certificate into my instance?
不可以,您不能在EC2 实例中使用AWS 证书,您需要将LB 放在实例顶部才能使用AWS 证书。
所以,感谢 Adiii,他为我指明了正确的方向。我所做的是为我的实例创建一个弹性 IP(非常简单),然后我将我的实例放在一个弹性负载均衡器(亚马逊 ELB)后面(也很简单),我将我在 ELB 中的端口从 443 设置为 443、80 到 80。我在 443 到 443 端口重定向上添加了我的证书。
我已经配置了我的实例和应用 运行,所以我引用了 /index.html 进行健康检查,以使其与 ELB 一起使用。我的应用 运行 在端口 443 上。我添加了一个 http_redirect.js 服务器文件侦听端口 80。
const express = require('express');
const http = require('http');
const app = express();
// set up a route to redirect http to https
app.get('*', function(req, res) {
res.redirect('https://' + req.headers.host + req.url);
})
http.createServer(app).listen(80, () => {
console.log('redirect-server up and running on port 80');
});
它只是重定向到 https 端口 443。
有了这个,我就可以使用来自 AWS 的免费证书,还可以使用 express/node.
从 http 重定向到 https
我目前有一个 react/node 应用程序位于 /home/ubuntu 的 EC2 ubuntu 实例中。该服务器是一个侦听端口 443 的 https 服务器。当我点击 Public DNS 时,只有在我的 dns 之前添加 https:// 时它才会出现。它按预期工作(没有它,它默认为端口 80 并且应用程序不显示,这是预期的)。
我有一个由亚马逊和亚马逊证书管理器生成的证书。如何将所有流量从端口 80 重定向到端口 443 并将我的亚马逊证书集成到我的实例中?
您需要在您的节点应用程序中将 HTTP 重定向到 HTTPS。网上有很多关于如何做到这一点的例子,例如:
您可以尝试两个选项。
- AWS ABL 重定向
- Nginx
我更喜欢 ALB,因为您不需要在实例级别公开端口,而且您不会在实例上管理任何代理。
If you are using ALB, you can redirect to https from LB rule. A rule has to have a condition and an action. Since we want to redirect all traffic that comes in on port 80 to the same URI, just with HTTPS instead, our condition should be simply “all”. Unfortunately you can’t just put a single asterisk in for the condition, the closest I’ve been able to come up with is
*.example.com
, wherexample.com
is whatever your domain is.
如果你没有使用ALB,那你可以试试Nginx。
- 您需要在服务器上安装 Nginx
- 在您的实例中允许端口 80
- 在 nginx.conf 中的配置下方
这将重定向所有
server {
listen 80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
这将重定向特定站点。
server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
}
你的问题的另一个澄清
How to integrate my amazon certificate into my instance?
不可以,您不能在EC2 实例中使用AWS 证书,您需要将LB 放在实例顶部才能使用AWS 证书。
所以,感谢 Adiii,他为我指明了正确的方向。我所做的是为我的实例创建一个弹性 IP(非常简单),然后我将我的实例放在一个弹性负载均衡器(亚马逊 ELB)后面(也很简单),我将我在 ELB 中的端口从 443 设置为 443、80 到 80。我在 443 到 443 端口重定向上添加了我的证书。
我已经配置了我的实例和应用 运行,所以我引用了 /index.html 进行健康检查,以使其与 ELB 一起使用。我的应用 运行 在端口 443 上。我添加了一个 http_redirect.js 服务器文件侦听端口 80。
const express = require('express');
const http = require('http');
const app = express();
// set up a route to redirect http to https
app.get('*', function(req, res) {
res.redirect('https://' + req.headers.host + req.url);
})
http.createServer(app).listen(80, () => {
console.log('redirect-server up and running on port 80');
});
它只是重定向到 https 端口 443。 有了这个,我就可以使用来自 AWS 的免费证书,还可以使用 express/node.
从 http 重定向到 https