504 Gateway Timeout With Nginx And Apache

These past couple of days I’ve spent a lot of time debugging the nginx 504 gateway timeout error. As far as I understand, this error 504 is caused by proxy timeout — nginx waits too long for an Apache response. By the way, I installed nginx as a reverse proxy with Apache to serve my static files. You can refer to my previous post: Install nginx as Reverse Proxy With Apache.

As I was digging around with Google, there are a lot of possible causes for this problem. Some say it’s because of fastcgi_read_timeout, but I’m not using fastcgi — the nginx server is only serving static content. So I guess that’s not it. Then after digging deeper, I found that my Apache was causing high CPU load and memory usage. Hmm… Wondering what causes this. It only happens when I post a new article on the WordPress blog.

So my first guess is that the Apache server is still waiting for MySQL to finish fetching data, but it’s not finishing. So nginx keeps waiting for Apache, and Apache keeps waiting for MySQL. This process keeps loading as new connections come in — in the end, it’s a deadlock!

Until now I’m still debugging the server and still confused. If you have ideas, please help me.