I've seen a number of questions here that highlight random 502 (Nginx + PHP-FPM = "Random" 502 Bad Gateway) and similar time outs when using Nginx + PHP-FPM.

Even with all the questions, I'm still unable to find a solution.

Using Ubuntu 10.10 + Nginx + PHP5-FPM + APC and every 1 out of 4 requests ends in a timeout and failure. This isn't a load issue or large traffic, it happens even in dev environment with one person.

I am doing this across 3 1GB machines, each with the same configurations and same problems.


fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

fastcgi_param  REDIRECT_STATUS    200;


; FPM Configuration ;


; Global Options ;

pid = /var/run/php5-fpm.pid

error_log = /var/log/php5-fpm.log

;log_level = notice

;emergency_restart_threshold = 0

;emergency_restart_interval = 0

;process_control_timeout = 0

;daemonize = yes

; Pool Definitions ; 



listen =

;listen.backlog = -1
;listen.allowed_clients =
;listen.owner = www-data
;listen.group = www-data
;listen.mode = 0666

user = www-data
group = www-data

;pm.max_children = 50
pm.max_children = 15
;pm.start_servers = 20
pm.min_spare_servers = 5
;pm.max_spare_servers = 35
pm.max_spare_servers = 10
;pm.max_requests = 500
;pm.status_path = /status

;ping.path = /ping
;ping.response = pong

request_terminate_timeout = 30
;request_slowlog_timeout = 0

;slowlog = /var/log/php-fpm.log.slow

;rlimit_files = 1024
;rlimit_core = 0

;chroot = 

chdir = /var/www

;catch_workers_output = yes
  • Did you try any of the proposed solutions?
    – Rob Olmos
    Commented Nov 14, 2010 at 8:54
  • might help if you show us configs
    – Mike
    Commented Nov 14, 2010 at 11:32
  • Removed my customisations that didn't work and have reverted to basically the default with my installation. Thanks
    – James
    Commented Nov 14, 2010 at 15:05

3 Answers 3


My money is on APC being to blame. I was seeing something similar with apc.include_once_override = On. Turning that off (and apc.canonicalize = On) fixed it for me.


It usually means that your Fastcgi configuration is not set correctly for the data and headers you are handing around.

You will need to play with the following settings in the fastcgi_parms file:

fastcgi_connect_timeout 90;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 1024K;
fastcgi_buffers 8 512k;
fastcgi_busy_buffers_size 1024k;
fastcgi_temp_file_write_size 1024k;
fastcgi_intercept_errors on;
fastcgi_pass_header *;

The above settings are from my development server and I use FirePHP, when the header information is too large (ie it fills up the buffers) I find that I always get the 502 error. Hence, I have made the buffers really really big. If you know that this is what your application is doing then this is a good place to start.

I would never use the above settings on a production server but you will find that the connect_timeout, send_timeout, intercept_errors and pass_header settings usually are a good place to start solving the 502 if excessive header/data is not the problem.


I agree with Ben tha APC could be culprit. I found Xcache less error prone, however it is told to be older and less cpu-efficient. Nevertheless, since 502 error is difficult to track, I suggest you to 'apt-get --purge remove' all your php packages (APC and php5-fpm in particular) and just follow the instructions in howtoforge it worked out of the box for me.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .