502 Bad Gateway with nginx | Google App Engine | N

2020-02-09 05:59发布

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.

8条回答
女痞
2楼-- · 2020-02-09 06:21

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

查看更多
Lonely孤独者°
3楼-- · 2020-02-09 06:26

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);
  }
查看更多
祖国的老花朵
4楼-- · 2020-02-09 06: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

查看更多
唯我独甜
5楼-- · 2020-02-09 06:29

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.

查看更多
趁早两清
6楼-- · 2020-02-09 06:30

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);
         });
查看更多
虎瘦雄心在
7楼-- · 2020-02-09 06:30

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);
查看更多
登录 后发表回答