1

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_params

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;

/etc/php5/fpm/main.conf

; FPM Configuration ;

;include=/etc/php5/fpm/*.conf

; 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 ; 

include=/etc/php5/fpm/pool.d/*.conf

/etc/php5/fpm/pool.d/www.conf

[www]
listen = 127.0.0.1:9000

;listen.backlog = -1
;listen.allowed_clients = 127.0.0.1
;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
James
  • 171
  • 1
  • 3
  • 15

3 Answers3

1

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.

Ben Jencks
  • 1,351
  • 8
  • 13
1

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.

monkee
  • 329
  • 2
  • 4
0

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.

alfish
  • 3,027
  • 15
  • 45
  • 68