Search code examples
phpnginxfpm

Error 502 Bad Gateway while my CPU and RAM are at 50%


For a while, my server has been returning 502 Bad Gateway errors randomly during the day. Sometimes we have the same traffic and everything is fine, other times it causes 502 errors. I have a web traffic of about 50r/s and here is the CPU and RAM usage attached.

I'm using PHP FPM 7.4 with config:

pm.max_childrens = 5
pm.start_servers = 2

VPS Specs:

8 vCore
16 GB Ram

CPU & Ram Usage: click here

PHP-FPM Error Log:

server reached pm.max_children setting (5), consider raising it

If I raise the value, for one minute everything works correctly and then after all the mysql requests return the following error: Lock wait timeout exceeded; try restarting transaction. So the site is unusable.

Thanks for your help


Solution

  • You should consider raising pm.max_children and then think about MySQL optimization, start from configuration side, try to switch on slow_query_log to get a clue where might be a bottleneck. There is huge list of options and methods to extract more performance from MySQL server, but you need start from something and move forward step by step. Maybe show us your MySQL server configuration file and some data about load profile to give you better advice.