简体   繁体   English

将Node.js页面部署到AWS Elastic Beanstalk之后,出现“ hell.php”错误和“ 502 Bad Gateway”错误

[英]Getting “hell.php” error and “502 Bad Gateway” error after deploying Node.js page to AWS Elastic Beanstalk

I recently deployed my very first Node.js app to AWS Elastic Beanstalk. 我最近将我的第一个Node.js应用程序部署到了AWS Elastic Beanstalk。 It is a very simple portfolio page. 这是一个非常简单的投资组合页面。 The site worked with no problem for several hours, but then the instance went to Severe status and the page returned this message: 该站点正常工作了几个小时,但是实例变为“严重”状态,页面返回了以下消息:

502 Bad Gateway nginx/1.12.1 502错误的网关nginx / 1.12.1

The error message in the log was "First argument must be a string or Buffer". 日志中的错误消息是“第一个参数必须是字符串或缓冲区”。

I restarted the app server, and the page worked for 12 hours with no problem, but then it went down again with the same message. 我重新启动了应用服务器,该页面正常工作了12个小时,但随后又出现了同样的消息,该页面再次掉线。 So I started troublshooting and tried these things: 因此,我开始进行故障排除并尝试了以下操作:

The Node.js version in Elastic Beanstalk was different than the version used to create my app, so I changed it to the same version the site was created with (8.12.0). Elastic Beanstalk中的Node.js版本与用于创建我的应用程序的版本不同,因此我将其更改为使用(8.12.0)创建站点的相同版本。 Restarted app server. 重新启动应用服务器。 Same problem. 同样的问题。

I thought that maybe the load balancer was having trouble reading the response, so I started converting the data sent in the response to a string (.toString()), but that did not help. 我以为负载平衡器可能无法读取响应,因此我开始将响应中发送的数据转换为字符串(.toString()),但这没有帮助。 And it turns out that my configuration does not even have a load balancer. 事实证明,我的配置甚至没有负载平衡器。

The Node documentation for fs.readFile said that the readFile method uses a lot of memory and to consider using readStream instead, so I made that change, but I'm getting the same result with readStream. fs.readFile的Node文档表示,readFile方法使用大量内存,并考虑改为使用readStream,因此我进行了更改,但使用readStream获得相同的结果。

I rebuilt the environment and tried again. 我重建了环境,然后重试。 This time the page ran successfully for two days. 这次页面成功运行了两天。 Then after two days it errored again with this message: 然后两天后,再次出现此错误消息:

Error: ENOENT: no such file or directory, open 'public//hell.php' events.js:183 throw er; 错误:否:没有这样的文件或目录,请打开'public // hell.php'events.js:183 throw er; // Unhandled 'error' event ^ //未处理的“错误”事件^

I don't use ANY php code. 我不使用任何PHP代码。 Why is it referencing a php file called "hell"? 为什么引用一个名为“ hell”的php文件?

Here is my code in the server.js file: 这是我在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);

A copy of the "public/index.html" file being read by fs can be found at: https://zurafuse.github.io/index.html 可以在以下位置找到fs正在读取的“ public / index.html”文件的副本: https : //zurafuse.github.io/index.html

Does anyone have any idea what I am doing wrong? 有人知道我在做什么错吗?

I have resolved this issue. 我已经解决了这个问题。 It turns out, bots frequently hit AWS sites like mine looking for vulnerabilities and in my case they were trying to open pages that do not exist (like Wordpress pages). 事实证明,僵尸程序经常像我的一样在AWS网站上寻找漏洞,而就我而言,他们试图打开不存在的页面(如Wordpress页面)。 So I modified my code to only open pages that exist that I have defined, and if any http requests come asking for something unexpected, I return a "page not found" response. 因此,我将代码修改为仅打开已定义的现有页面,并且如果有任何HTTP请求提出了意外要求,我将返回“找不到页面”响应。 I have not had a problem since. 从那以后我就没有问题了。

Because my site was constantly getting errors trying to open pages that do not exist, it was crashing my AWS Elastic Beanstalk instance. 由于我的网站在尝试打开不存在的页面时经常出错,因此使我的AWS Elastic Beanstalk实例崩溃。 And since I have the free version, it is not scalable at all and so not very forgiving. 由于我有免费版本,因此它根本无法扩展,因此也不是很宽容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js和Docker- Elastic Beanstalk 502错误网关 - Node.js and Docker- Elastic Beanstalk 502 Bad Gateway 弹性beantalk上的node.js:Firefox 502错误网关 - node.js on elastic beanstalk: firefox 502 bad gateway 尽管CORS配置正确,但CORS 502 Bad Gateway,AWS Elastic Beanstalk Node.js服务器 - CORS 502 Bad Gateway, AWS Elastic Beanstalk Node.js server, despite proper CORS configuration AWS Elastic Beanstalk / NodeJS / Nginx错误:不断收到502错误的网关 - AWS Elastic Beanstalk / NodeJS / Nginx Error: Keep getting 502 bad gateway Nodejs 502 Bad Gateway在Elastic Beanstalk AWS上部署Express - Nodejs 502 Bad Gateway Deploying Express on Elastic Beanstalk AWS “502 Bad Gateway”将hapi.js部署到AWS Beanstalk? - “502 Bad Gateway” deploying hapi.js to AWS Beanstalk? 部署后 AWS 弹性 beanstalk Express 服务 502 Bad Gateway - AWS elastic beanstalk Express service 502 Bad Gateway after deployment 带有 nginx 502 错误网关错误的 node.js 应用程序 - node.js app with nginx 502 bad gateway error 将 node.js 应用程序部署到 Amazon Elastic beanstalk 时出错 - Error deploying node.js app to Amazon Elastic beanstalk 将 node.js 应用程序部署到 Amazon Linux 2 上的 Elastic Beanstalk 时出错 - Error deploying a node.js app to Elastic Beanstalk on Amazon Linux 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM