本文介绍了带有 NodeJS 的 Google App Engine 502(坏网关)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 NodeJS、MongoDB(Mongoose 作为驱动程序)和 ExpressJS 的完整 Web 应用程序.

I have a full web application using NodeJS, MongoDB (Mongoose as the driver) and ExpressJS.

该项目在我的本地机器上完美运行.今天我决定将所有东西都转移到生产中.我使用 Google App Engine 来托管我的应用程序,并使用 Compose(以前称为 MongoHQ)来托管我的数据库.

The project works perfectly on my local machine. Today I decided to move everything to production. I'm using Google App Engine to host my application, and Compose (formally MongoHQ) to host my database.

App Engine 完美地为我的应用程序提供服务,尽管我的 API 似乎不起作用.我的 API 由 example.com/api 提供,每个请求(GETPOSTDELETEPUT) 都返回 502(坏网关)错误.

App Engine servers my application perfectly, although my API does not seem to work. My API is served from example.com/api, and each request (GET, POST, DELETE and PUT) all returns a 502 (Bad Gateway) error.

我尝试在连接到远程 MongoDB 数据库的同时在本地计算机上运行我的应用程序,并且运行良好.所以肯定是 App Engine 或 NodeJS 的问题,而不是 MongoDB.

I tried running my application on my local machine while connected to my remote MongoDB database and that worked perfectly fine. So it must be a problem with App Engine or NodeJS, not with MongoDB.

我已尝试检查 Google Cloud 中的所有错误日志,但没有错误.

I have tried checking all error logs within Google Cloud, although there are no errors.

为什么 App Engine/NodeJS 可以完美地为我的应用程序的静态内容提供服务,尽管不允许对我的 API 发出任何请求?

Why is App Engine/NodeJS serving my application's static content perfectly fine, although not allowing any requests to my API?

推荐答案

502 Bad Gateway 通常是 Nginx 端的错误.遗憾的是,这些日志尚未显示在 Cloud Logging 中.

502 Bad Gateway is usually an error on the Nginx side. Unfortunately those logs are not surfaced to Cloud Logging, yet.

很多时候问题是你的 HTTP 数据包对于缓冲区或类似的东西来说太大了.查看nginx日志的方式是这样的:

A lot of times the problem is that your HTTP packets are too big for the buffers or something similar. The way you can see the nginx log is something like this:

  • 仅使用 1 个虚拟机.这不是绝对必要的,但很多时候,如果您知道您的请求是在一台机器上进行的,那么它可以更轻松地调试您的应用程序.您可以通过将其添加到您的 app.yaml 中来完成此操作:

手动缩放:实例:1然后重新部署

  • 将虚拟机从Google 拥有"切换到自我管理.这可以在 Cloud Console 中完成.转到 Compute Engine、instances,点击与 App Engine 版本匹配的实例名称,您应该会看到一个将其切换为自我管理的选项.

  • Switch the VM from "Google owned" to self-managed. This can be done in the Cloud Console. Go to Compute Engine, instances, click on the instance name that matches the App Engine version, and you should see an option to switch it to self-managed.

gcloud compute ssh SSH 到机器

docker ps 查看正在运行的容器.查找名为 nginx 的容器并获取其 id.

docker ps to see your running containers. Look for the container named nginx and grab its id.

一旦你有了容器 ID,你应该能够docker exec -it -- cat/var/log/nginx/error.log.您可能想要 ls 整个日志目录.

Once you have a container ID, you should be able to docker exec -it <container id> -- cat /var/log/nginx/error.log. You might want to ls that whole log directory.

您可能会在那里看到一个错误,这将是关于发生了什么问题的更大提示.

You will likely see an error there which will be a bigger hint as to what's going wrong.

我知道这比它应该的复杂得多:-如果您对上述步骤有任何疑问,请发表评论.如果您确实发现了错误并且不确定如何处理,也请发表评论.

I know this is way more complicated than it should be :- If you have any problems with the steps above, leave a comment. If you do find an error and you're not sure what to do with it, also leave a comment.

这篇关于带有 NodeJS 的 Google App Engine 502(坏网关)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 13:17