【发布时间】:2019-04-29 17:51:17
【问题描述】:
我最近将我的第一个 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' 事件.js:183 投掷者; // 未处理的“错误”事件 ^
我不使用任何 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
有人知道我做错了什么吗?
【问题讨论】:
-
我还注意到它正在尝试打开 lol.php 和 wp-config.php。这是某种拒绝服务攻击吗?我是否需要创建逻辑来拒绝这些请求?
标签: node.js amazon-web-services nginx fs amazon-elastic-beanstalk