502 error on production even though the response times are well below 10 seconds

Everything runs fine locally when I run netlify dev, but when I deploy to production I get a 502 error. I have checked my database and everything is getting updated properly in there when I visit different routes in my app. Cookies are even getting set in the browser, but the response from the server is returning a 502 error even though the response times are well below 10 seconds.

IMPORTANT NOTE: The database that I am using right now is a development database and it goes to sleep after about 30 minutes of inactivity and then it takes about 30 to 60 seconds to wake up after it receives a request. So the initial response will probably time out for you, but after the database is awake then the functions respond under 10 seconds.

Heya, could you please send over your Netlify URL and a x-nf-request-id? That’ll help us dig in further :slight_smile:

Hi @jen,

My Netlify base URL is https://financial-wreck-app.netlify.com, but it automatically redirects users to https://financial-wreck-app.netlify.com/login.

My x-nf-request-id is c21be6d2-c3ca-4097-a6f9-b2af52368d79-2700489

Thank you!

Hi @samwell, first, I noticed you’re using no-cache for cache-control, if the idea is to make sure the funciton is hit every time and the response from it isn’t just cached, you should instead use no-store instead.Also, make sure your functions are returning valid JSON, and not just a boolean value or a number(I don’t know if you are returning a number anywhere, just wanted to make sure you know this).

Hey @samwell, were you able to find a solution for this? Facing a similar issue now where I get a 502 Bad Gateway in production even though response time is less than a second. Works fine locally.

I have had to pause this project for now, so I have not been able to find a solution yet.

Hey @leonard, if you want to share your Netlify URL and x-nf-request-id (and code if it’s public), we’d be happy to take a look.

I also wanted to share a thread with some ideas about how to fix the 502 (encoding errors abound):