node.js - 将 Node.js 页面部署到 AWS Elastic Beanstalk 后出现“hell.php”错误和“502 Bad Gateway”错误
问题描述
我最近将我的第一个 Node.js 应用程序部署到 AWS Elastic Beanstalk。这是一个非常简单的投资组合页面。该站点运行了几个小时没有问题,但随后实例进入严重状态,页面返回以下消息:
502 错误网关 nginx/1.12.1
日志中的错误消息是“第一个参数必须是字符串或缓冲区”。
我重新启动了应用程序服务器,页面运行了 12 个小时没有问题,但随后又出现了同样的消息。所以我开始排除故障并尝试了这些事情:
Elastic Beanstalk 中的 Node.js 版本与用于创建我的应用程序的版本不同,因此我将其更改为创建网站时使用的相同版本 (8.12.0)。重新启动应用服务器。同样的问题。
我认为负载均衡器可能无法读取响应,因此我开始将响应中发送的数据转换为字符串 (.toString()),但这并没有帮助。事实证明,我的配置甚至没有负载均衡器。
fs.readFile 的 Node 文档说 readFile 方法使用大量内存并考虑改用 readStream,因此我进行了更改,但使用 readStream 得到了相同的结果。
我重建了环境并再次尝试。这次页面成功运行了两天。然后两天后它再次出错并显示此消息:
错误:ENOENT:没有这样的文件或目录,打开 'public//hell.php' events.js:183 throw er; // 未处理的“错误”事件 ^
我不使用任何 php 代码。为什么它引用一个名为“hell”的php文件?
这是我在 server.js 文件中的代码:
const http = require("http");
const fs = require("fs");
//use AWS's default port, or if it's not available, use port 8081.
const port = process.env.PORT || 8081;
const server = http.createServer(function (req, res) {
res.statusCode = 200;
if (req.url == "/" || req.url == "/index.html" || req.url == "/home") {
let readStream = fs.createReadStream("public/index.html");
// When the stream is done being read, end the response
readStream.on('close', () => {
res.end();
})
// Stream chunks to response
readStream.pipe(res);
}
else {
let readStream = fs.createReadStream("public/" + req.url);
// When the stream is done being read, end the response
readStream.on('close', () => {
res.end();
})
// Stream chunks to response
readStream.pipe(res);
}
}).listen(port);
fs 正在读取的“public/index.html”文件的副本可以在以下位置找到: https ://zurafuse.github.io/index.html
有谁知道我做错了什么?
解决方案
我已经解决了这个问题。事实证明,机器人经常访问像我这样的 AWS 网站寻找漏洞,在我的例子中,他们试图打开不存在的页面(如 Wordpress 页面)。所以我修改了我的代码,只打开我定义的存在的页面,如果有任何 http 请求来要求意外的东西,我会返回一个“找不到页面”响应。从那以后我就没有问题了。
因为我的站点在尝试打开不存在的页面时不断出错,所以它使我的 AWS Elastic Beanstalk 实例崩溃。而且由于我有免费版本,它根本无法扩展,因此不是很宽容。
推荐阅读
- python - 在python中将json文件与comon键合并
- javascript - 对象 PDF 查看器无法在 chrome 中呈现超过 2.0 MB 的大文件
- javascript - 在不使用 js 中的 shift/unshift/loop 的情况下从数组的开头添加或删除
- postgresql - 如何在关系数据库中对版本控制用例进行数据建模
- java - 如何正确覆盖 java.io.Closeable 中的 close() 方法来杀死一个对象?
- flutter - Flutter - 如何通过 enough_mail 回复邮件
- java - 任务 ':app:packageDebug' 执行失败。> 预期的文件集合只包含一个文件,但是,它包含多个文件
- html - 我正在为我的表单使用mailto,但是当我尝试提交表单时,绝对没有任何反应,我错过了什么吗?
- ruby-on-rails - 在 Rails 中迁移后保存到数据库不起作用
- python - Python,Selenium:如果我们设置一个变量来“查找一个元素”,那么所有可能的返回值是多少?