我正在尝试使我的 ec2 实例(带有在 NodeJS 上运行的应用程序)网站安全(作为 https)。
我可以成功访问 IP 地址以查看我的应用程序在实例上运行,但是当我进入子域时,响应时间太长。
我设置了一个与实例的证书和目标组链接的负载均衡器。节点应用程序在端口 3000 上运行。
我有同一个实例的 2 个注册目标,一个用于端口 3000,一个用于端口 80。两者的运行状况都是健康的。
我的负载均衡器具有 SSL/TLS 证书,其中规则将端口 80 重定向到端口 443 并转发到目标组。
在路由 53 中,我创建了一个路由到负载均衡器 DNS 的子域。
我通过 ssh 连接到我的 EC2 实例,我克隆了我的 github 存储库并使用节点来运行服务器。我可以成功访问 IP 地址以查看我的应用程序在实例上运行,但是当我进入子域时,响应时间太长。
我期望发生的是我的 IP 地址通过安全连接重定向到子域,显示该子域上我的节点应用程序的内容。相反,IP 地址会使用地址栏中的 IP 地址加载不安全页面上的内容。
我的负载均衡器的入站规则设置为所有流量。出站设置为端口 443 和 80,源作为 EC2 实例的安全组 ID。
我的 EC2 实例的入站设置为端口 443、80、3000,源作为负载均衡器安全组 ID。出站设置为所有流量。
我可能做错了什么?
我还有 nginx.conf 文件:
user nginx;
worker_processes auto; error_log /var/log/nginx/error.log notice; pid /run/nginx.pid;
events { worker_connections 1024; }
http { include /etc/nginx/mime.types; default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
server {
listen 80;
server_name <subdomain_goes_here>;
location / {
proxy_pass <load_balancer_DNS_goes_here>;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /health {
return 200 'Healthy';
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
这是我的nodejs文件:
require("dotenv").config();
const sendGrid = require("@sendgrid/mail"); sendGrid.setApiKey(process.env.sendGridAPIKey);
const express = require('express'); const app = express();
// Define a route handler for the health check path app.get('/health', (req, res) => { // Respond with a 200 OK status and the text "Healthy" res.status(200).send('Healthy'); });
process.env
const PORT = process.env.PORT || 3000;
//access to html frontend app.use(express.static('frontend')); app.use(express.json())
//access to file app.get('/', (req, res)=>{ res.sendFile(__dirname + '/frontend/contact.html') })
//access to data in contact form app.post('/', (req, res)=>{ console.log(req.body)
const mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: req.body.subject,
reply_to: req.body.email,
text: req.body.message
}
sendGrid.send(mailOptions, (error)=>{
if(error){
console.logy(error);
res.send('error');
} else {
console.log('Email sent');
res.send('success');
}
})
})
app.listen(PORT,'0.0.0.0',()=>{ console.log(Server running on port ${PORT}) })
您的负载均衡器安全组似乎存在问题。您应该在入站规则中允许来自 0.0.0.0/0 的端口 80 和 443 上的流量,并在出站规则中允许所有流量。
此外,您的 EC2 安全组应允许来自负载均衡器安全组的端口 80 上的入站流量,并允许出站组中的所有流量。
nginx 配置中的 proxy_pass 设置应具有节点应用程序本地 URL (0.0.0.0:3000),而不是负载均衡器端点。