Nginx 502 bad gateway google cloud run the site takes approximately 50 minutes then starts working

Update

The problem had nothing to do with GCP finally solved the issue after I came across this answer just luck 🤠

Niko Solihin solution:

  1. Set node HTTP server to listen strictly for ipv4 by including localhost as host: server.listen(5000, 'localhost');
  2. Removed any ipv6 listen directives (listen [::]:80; or listen [::]:443 ssl default_server;).
  3. Changed location block proxy_pass to use IPs: proxy_pass http://127.0.0.1:5000 (not proxy_pass http://localhost:5000).

In my case the nuxt host config is localhost instead of 127.0.0.1

Cheers 🥂🎈. Hope it helps someone else.

How I solved the problem old:

You can use these as optimization tips 🚀🚀

summary


  • Deploying both static and SSR for nuxt.

The static build to be used as nginx fallback when the nuxt upstream fails. This evidently solves the problem of users getting nginx 502 bad gateway error when the api's and other stuff are not working. Instead the static build is served.

  • Reducing docker start up time

The google documentation specifies Maximum container startup time is 4 minutes See here

  1. Using entrypoint instead of CMD for some reason this reduced how regularly the container failed during deployment and instead of 50 minutes it took around 20 minutes to start on it's own.

  2. Increase your container instance memory and and vCPU

  • Bandwidth issue - Limit how many times you deploy (probably opinion based)

I don't know if this is just me however I did note that when the container exceed the free allocated bandwidth...I start running into 502 bad gateway problems. If you have a very large container limit how many times you build your container. enter image description here

In my case the first deployment of the day is between free quota and runs fine. The second is in between free and paid. I sometimes get the 502 eventually resolves itself when I interact with the static build. Anyway its just a nice idea to avoid unnecessary deployments.

NB: I also noted after deleting cloud run artifacts storage and the rest of the storage wait for a few while before deploying. Deletion is usually done in the background. Quick deletion and deployment of my project to cloud run lead to 502 sometimes.

(Will update in case I have more insights)