Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

random 502 gateway errors with nginx php-fpm and ubuntu

Tags:

php

nginx

ubuntu

I was having an issue with random 502 gateway errors using nginx and php-fpm. In my case I discovered a scenario where various php.ini error_log settings and error_reporting levels were causing the random 502 gateway errors to appear.

By changing the php.ini settings error_reporting and error_log I was able to make the 502 gateway errors disappear - but more importantly I was able to see what the real php errors were in the log and fix them.

The main issue was that if "error_reporting was set to display notices, "error_logging = On" then I needed make sure that error_log was set to a valid path tat was writable to the server.

// !!! 502 Gateway Error (unhappy server)
error_reporting = E_ALL & E_NOTICE
; error_log = php_errors.log (note this is commented)

// Happy Server, no 502 gateway error
error_reporting = E_ALL & E_NOTICE
error_log = /valid/log/path/and/permissions

// Happy Server, no 502 gateway error
error_reporting = E_CORE_ERROR
; error_log = php_errors.log (note this is commented)

Note, the actual errors were php notices... however ngingx was throwing 502 gateway errors for php notices that were related to properties not being set.

Also note, that 502 gateway errors were not being triggered on every php notice.

Hope this saves someone some frustration!

like image 631
Justin Vincent Avatar asked May 10 '12 19:05

Justin Vincent


1 Answers

502 gateway errors in Nginx are caused by php-fpm not having enough process and/or timeouts. Logging only tells you what the issues are and are not the cause of 502 errors.

I use stunnel+haproxy+nginx+php-fpm on 25 servers. The defaults in pfp-fpm are very low, even for a moderately busy server. Configure the fpm child processes much the same way you would with apache mod_php.

I use the following:

pm.max_children = 250
pm.start_servers = 20
pm.min_spare_servers =10
pm.max_spare_servers = 20
pm.max_requests = 1500

This is a server with 2GB ram. It serves 20-30GB traffic per day with no 502's

like image 87
gmm Avatar answered Sep 21 '22 02:09

gmm