20

I have a 2GB VPS on DigitalOcean and I am hosting WordPress 3.9.1 under Debian 7 with NGINX, php-fpm and unix socket.

It was working perfectly until last week it started showing a "502 bad gateway" error. I checked the logs and found that:

php5-fpm log is showing pm.max_children was reached and nginx log is showing the following:

[error] 3239#0: *15188 connect() to unix:/var/run/php5-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: my.domain, request: "POST /xmlrpc.php HTTP/1.0", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "xxx.xxx.xxx.xxx"

I manually changed pm with different settings with no luck. I always restart the daemons after every change.

pm settings are:

pm = dynamic 
pm.max_children = 100 
pm.start_servers = 10 
pm.min_spare_servers = 10 
pm.max_spare_servers = 10 
pm.max_requests = 200

www.conf has the listen = /var/run/php5-fpm.sock enabled.

Anyone with a similar experience?

James Jones
  • 3,850
  • 5
  • 25
  • 44
vsapountzis
  • 618
  • 3
  • 11
  • 28

1 Answers1

27

The first problem is you are specifying 100 max_children, that is awfully high for 2GB. I would drop it to 25 children. See my post here on how to optimise your php-fpm configuration for your setup:

WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning

Also, using unix sockets is slightly faster since it provides you direct network access without any TCP/IP overhead. On the down side, it is not as scalable as TCP/IP. Nginx will throw 502 errors when the sockets have been depleted. In such a case you can either tweak the OS settings to accommodate the larger connection pool or just switch to switch to TCP/IP.

In your fastcgi conf change:

fastcgi_pass unix:/var/run/php5-fpm.sock;

to:

fastcgi_pass 127.0.0.1:9000;

Note that port 9000 is the default port set in php-fpm, if you have changed php-fpm to listen on another port then swap 9000 with that value. Make sure you restart both php-fpm and nginx.

Now, if after all of this, you still cannot get it to work and free -m returns high memory usage, then it is time to add more ram to your server.

Rijndael
  • 3,683
  • 2
  • 24
  • 26
  • Thanks for the reply. I will make the configuration changes you suggested and i will let you know later. – vsapountzis Aug 03 '14 at 17:23
  • Hi again. Thanks for the help. Your solution worked perfectly but although i got rid of the 502 error i am experiencing 504 timeouts and free -m is reporting 1.4GB free out of 2GB available. TCP/IP configuration produces this error or is it unrelated? – vsapountzis Aug 03 '14 at 19:13
  • The 504 is nginx waiting for a reply from php-fpm but not receiving one within X amount of time. Could be due to many reasons. First, you should check your nginx error log, should give you some error output. You should also consider increasing the nginx timeout. You can try setting fastcgi_send_timeout and fastcgi_read_timeout in nginx.conf somewhere between 30s and 60s and see whether that does the trick. – Rijndael Aug 03 '14 at 21:52
  • Hi again. I eventually changed the timeout in my nginx.conf and php.ini file to 60s and i am still experiencing 504. Any ideas @Rijndael? – vsapountzis Aug 06 '14 at 08:22
  • Try setting request_terminate_timeout=30s in nginx.conf and max_execution_time=30 in your php-fpm conf. The latter should already be present. Make sure you restart both nginx and php-fpm. – Rijndael Aug 06 '14 at 11:43
  • No luck unfortunately. Any sysctl tweaks? – vsapountzis Aug 07 '14 at 07:49
  • I usually get "[error] 8454#0: *1415 upstream timed out (110: Connection timed out) while connecting to upstream" – vsapountzis Aug 08 '14 at 17:27
  • Okay, looks like a different issue. You might need to toggle your upstream timeouts, although its difficult to say without having seen your configuration. Create a new thread and post up your config. – Rijndael Aug 08 '14 at 17:57
  • Here is the new thread with my configuration along with the changes i made lately. https://stackoverflow.com/questions/25178031/error-upstream-timed-out-what-is-wrong-with-my-setup – vsapountzis Aug 08 '14 at 18:01