47

I am hosting the web app on Google Cloud Platform with App Engine and I am using ExpressJS and MongoDB, which is hosted on mLab.

Everything worked well until 1/1/2017. I had vm:true before and now was forced to change the env to flex. Now I am getting 502 bad gateway error with nginx. App engine doesn't allow us to change the nginx config file.

I had tried the suggestion from this post: Google App Engine 502 (Bad Gateway) with NodeJS but still doesn't work.

For some reason, I have another app with exactly the same setting on app engine and it works perfectly.

Any suggestion will be greatly appreciated. Thank you.

5
  • Did you check your app's exposure to the breaking changes coming with this migration? cloud.google.com/appengine/docs/flexible/python/… Commented Jan 5, 2017 at 2:55
  • I have the same issue. Upvoted your question.
    – Coder1000
    Commented Jan 8, 2017 at 17:01
  • Hey guys, got the same error today with the same config (express and mlab). Have you managed to fix this issue? Commented Feb 4, 2017 at 13:10
  • If using postgres - 'ENGINE': 'django.db.backends.postgresql_psycopg2' . Install psycopg2 on your local machine and mention it along with its version in requirements.txt file
    – Aseem
    Commented Jul 8, 2019 at 1:27
  • @NodeJsNewbie did you find solutions to this?
    – martin
    Commented Nov 9, 2020 at 17:03

9 Answers 9

28

check out the logs for any deployment errors

$ gcloud app logs read

I have came across a similar issue with the code provided by this tutorial (https://cloud.google.com/nodejs/getting-started/authenticate-users)

And found there was a missing dependency. I fixed the missing dependency and the app is deployed and working fine.

Details into the issue: https://github.com/GoogleCloudPlatform/nodejs-getting-started/issues/106

3
  • Led me to the my problem as well. Ended up being that I forgot to define a requirements.txt file. My error log showed "ModuleNotFoundError: No module named 'flask'"
    – pr-
    Commented Dec 2, 2018 at 20:58
  • I was using psycopg2 in my django code which wasnt mentioned in my requirements.txt file. Found it out when I read logs.
    – Aseem
    Commented Jul 7, 2019 at 21:40
  • For a better debug experience in the cloud : cloud.google.com/appengine/docs/flexible/nodejs/… Commented Jan 16, 2021 at 21:08
28

app should always listen to port 8080, google forwards all request from 80 to 8080 https://cloud.google.com/appengine/docs/flexible/custom-runtimes/build#listen_to_port_8080

0
6

Set the host to 0.0.0.0

Port 8080 is set by default by the engine. In fact, you are not able to define the environment var PORT as it is reserved.

Run the next command (as mentioned by @sravan )

gcloud app logs read tail

and make sure it looks like this,

[Sun May 27 2018 10:32:44 GMT+0000 (UTC)] serving app on 0.0.0.0:8080

Cheers

1
  • 1
    Even though my ports were correctly set up, the command gcloud app logs read proved to be a live saver for me.
    – wandesky
    Commented May 29, 2022 at 11:40
5

I had the same problem with Express. What solved it for me was to not provide an IP address for the app.

So my old code would be:

var ip = "127.0.0.1";
var port = "8080";
var server = http.createServer(app);
server.listen(port, ip);

This would result in a 502 in app engine.

Removing the ip was the solution for me.

server.listen(port);
1
  • Another thing to look at is whether SSL is setup on gcloud. I'm developing on localhost with a self-signed certificate to enable HTTPS development (to use Service Workers). After deploying to gcloud, check your appengine settings for SSL.
    – Kalnode
    Commented Dec 19, 2017 at 23:12
3

Google App Engine uses an nginx front to load balance all requests for node.js apps. With nginx acting as a forward proxy, this error usually happens when the request the user is making in the browser is reaching nginx (you see the unstyled 502 bad gateway error page) but the nginx server is not able to correctly forward the request to your node app. There could be many issues why this is happening but here are some common ones:

  1. By default, App Engine assumes your node app is running on 8080. nginx itself will run on 80 and forward the request to 8080. Check if your app's port number is 8080.

  2. You app may have a hostname defined like a domain something.appspot.com or an IP 127.18.21.21 or the like. Remove any hostnames from your server.listen or config.json or vhost wherever. App Engine will take care of domains, IPs etc so you dont have to.

  3. Your app may be crashing before its sending a response to nginx. Check the logs of both nginx AND your node app.

To check logs / find out what is going on use this guide https://cloud.google.com/appengine/docs/flexible/nodejs/debugging-an-instance#connecting_to_the_instance to SSH directly inside the VM behind app engine. There will be one docker process with nginx where you can see the nginx error log and one docker image with your node app to check your node app's error message.

I'm just wondering, based on the activity in this question and the timestamps, why hasn't Google updated its documentation to cover this issue!!! ???

2

Please take care of http also, while deploying, it should be http server not https

  var server;
  if (process.env.NODE_ENV == "dev") {
    server = https.createServer(httpsOptions, app);
  } else {
    server = http.createServer(app);
  }
1

A 502 is not necessarily an error with nginx itself, it can most often happen when the nginx proxy cannot talk to your app container (usually because your app failed to start). If you get a 502 after migrating to 'env: flex' this is most likely due to some code changes needed in your app as mentioned in Upgrading to the Latest App Engine Flexible Environment Release.

Checking your application logs for errors from NPM will also help to diagnose the exact reason for the failed startup.

0

Create a server and then check with a ternary condition if current environment is production or not, assign port '80' if current environment is development else assign process.env.NODE.ENV.

const app = require('express')();
        const server = require('http').Server(app);
        const port = process.env.NODE_ENV === 'production' ? process.env.PORT :'80';
        server.listen(port, ()=> {
          console.log('listening on port number  *:' + server.address().port);
         });
0
0

In my case, I had the same error due to google app engine update which trigged auto re-deployment of my React SPA to the google cloud vm. Then it leads to a build fail in the process because of incompatibility of runtime which is node 16.x.x. Compatible runtime was node 14.19.0. I had to specify node version in my package.json file and do the deployment again to fix 502 Bad Gateway error.

{
  "engines": {
    "node": "14.19.0"
  }
}

Also refer:

Hope this helps with someone having this issue with React SPAs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.