Recurring 502 Bad Gateway (nginx) and 504 Gateway Time-out (nginx) error

April 27, 2016 5.7k views
LEMP WordPress

Hi there

Thanks to all community members for the great support provided.

I'm dealing with a Vps with Wordpress Installation, Lemp based.

Today morning my site was stocked in an 502 Bad Gateway (nginx) so I

"service ningx restarted"


"service php5-fpm restarted"


"stop: Unknown instance:
php5-fpm start/running, process 1012"

So I uncommented the "# reload signal USR2" line within the php5-fpm.conf file

But I hadn't any fortune and after 2 hours of missing attempts, I decided to reboot the droplet.

So I

"sudo shutdown -h now"

and then I switched it on by the console on

After that I could restart my php5-fpm and had my website online but the system look unstable and slow.

Connecting to the site the server gives me first 502 or 504 error.
If I restart php5-fpm and nginx it looks working but if I clic again through the site it gives me again the above errors. Mmmmmmmm.....

After checking web server installation, running and syntax configuration file. After checking Port, Dns and permission settings, I went through the log side of the moon.

I have to say that I found a lot of stuff but tried to move in a rational way.

first I checked the php5-fpm.log.4.gz and it gave me the following lines:

[27-Apr-2016 12:25:17] NOTICE: configuration file /etc/php5/fpm/php-fpm.conf test is successful.

But I supposed it's been sorted uncommenting the: "# reload signal USR2"

Then I move to the nginx dir. and I checked first the:

error.log file and I found

[notice] 25991#0: using inherited sockets from "9;"

and then the site-available.error.log.4 file and I found

2016/04/27 12:13:29 [error] 3085#0: *31943 connect() to unix:/var/run/ajenti-v-php-fcgi-ggtestit-php-fcgi-0.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client:, server: mysite, request: "POST /xmlrpc.php HTTP/1.0", upstream: "fastcgi://unix:/var/run/ajenti-v-php-fcgi-ggtestit-php-fcgi-0.sock:", host: ""

Sorry for the long post but I want to provided as much information as I can of an hell day.

I don't really know where to look or what to do, so please is there anyone that can help me?

Have you any suggestion?

Thanks a lot

3 Answers

With these issues, there are a few possible causes:

  • Memory
  • CPU
  • Disk I/O

To check memory: free -m
To check CPU: either apt-get -y install sysstat or yum -y install sysstat, followed by mpstat
To check Disk I/O: iostat 1 (CTRL+C to stop)

If all those look fine, the issue is most likely a mis-configuration, but my bet is on one of those 3 causes.


Thanks for your reply

I just try with the free -m command and i get:
total used free shared buffers cached
Mem: 490 470 19 49 5 107
-/+ buffers/cache: 356 133
Swap: 0 0 0

That it looks I have few but still space.

About Cpu I get
05:51:42 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
05:51:42 PM all 3.77 0.01 0.29 0.11 0.00 0.01 0.00 0.00 0.00 95.81

And for Disk I/O
Device: tps kBread/s kBwrtn/s kBread kBwrtn
vda 6.87 99.26 51.99 4245345 2223632

avg-cpu: %user %nice %system %iowait %steal %idle
0.00 0.00 0.00 0.00 0.00 100.00

I have to say that it's really hard for me to read the last two stats.

Can you help me on understand them?

Have you any further advice?

  • You've shown me what I needed to see.

    You don't have enough memory on the box to run everything. You need to either upgrade to a larger server, or build another server dedicated to your database.

I'm still trying to sort the situation?
Is there someone who can help me with this?

Have another answer? Share your knowledge.