2

I got (502 Bad Gateway nginx) from google cloud after I uploaded my node.js code to the app engine. I have no errors in my logs. I have done a lot of searching online and nothing helped, but I read some article saying that because the response time is long, the server returns (502 Bad Gateway).

This is my listening code:

const server = app.listen(8080, () => {
    const host = server.address().address;
    const port = server.address().port;

    console.log("EDU-vents server started");
});

This is my app.yaml:

runtime: nodejs
env: flex

my package.json includes a start script

Thank you in advance.

1 Answers1

3

Typically, when you encounter 5.x.x (502, 503, etc) it is recommended to wait a minute and try the request again. You may find more information about these errors specific to App Engine flexible environment.

However, most of the time the error code 502 with "BAD_GATEWAY" indicates that GAE terminated the application because it ran out of memory. By default, GAE Flex only has 1GB of memory and only 600MB is available for the application container. The following documentation describes steps on how to troubleshoot this type of error (You will have to most likely investigate your Stackdriver logs.

I recommend you to specify a higher CPU and Memory of your instance. If specifying a higher CPU and Memory doesn't work. I recommend you to check the Nginx error logs by following. Before SSHing to the VM instance, you need to enable debug mode for a VM instance

If this is not the case, do you have any support package/free trial? I'd recommend you to open a support ticket directly in their support center, if not you can contact Google through Google Issue Tracker as 5XX errors can be caused by different reasons.

Michael T
  • 120
  • 6
  • 1
    I contacted support. They told me to change the environment to standard. After I did, I started getting the error ```2019/09/18 14:46:51 [error] 24#24: *2 upstream prematurely closed connection while reading response header from upstream, client: 169.254.1.1, server: _, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/"``` – Abdullah AlJasser Sep 18 '19 at 14:52
  • 1
    I saw in the port 8081 in error, please ensure that the application is listening on port 8080 that's used by default and you may try adding [port here](https://cloud.google.com/appengine/docs/standard/nodejs/how-requests-are-handled#handling_requests); some customers found the [solution](https://stackoverflow.com/questions/36488688/nginx-upstream-prematurely-closed-connection-while-reading-response-header-from) by changing timeout. Take a look at GAE's logs. 1/2 – Michael T Sep 19 '19 at 01:20
  • This may be a code issue and you may need to share your app.yaml and add [some logs](https://cloud.google.com/appengine/docs/standard/nodejs/writing-application-logs) to find out at which point does your application prematurely closes the connection, then working with support is more appropriate. 2/2 – Michael T Sep 19 '19 at 01:21