title: nginx php5-fpm upstream timed out (110: Connection timed out) while connecting to upstream
body: <p>We have a web server running with nginx php5-fpm apc setup. 
  However we experienced upstream connection timeout errors and slow downs during page rendering recently. A quick php5-fpm restart fixed the problem but we could not find the cause. </p>
  
  <p>We have another web server running apache2 under another subdomain, connecting the same database, doing exact same job. But the slow downs occur only on the nginx-fpm server.
  I think the php5-fpm or apc may cause the problems.</p>
  
  <p>Logs tell that various connection time outs: </p>
  
  <p><code>upstream timed out (110: Connection timed out) while connecting to upstream bla bla bla</code></p>
  
  <p>The php5-fpm log does not show anything. Just child starts and finishes:</p>
  
  <pre><code>Apr 07 22:37:27.562177 [NOTICE] [pool www] child 29122 started
  Apr 07 22:41:47.962883 [NOTICE] [pool www] child 28346 exited with code 0 after 2132.076556 seconds from start
  Apr 07 22:41:47.963408 [NOTICE] [pool www] child 29172 started
  Apr 07 22:43:57.235164 [NOTICE] [pool www] child 28372 exited with code 0 after 2129.135717 seconds from start
  </code></pre>
  
  <p>Server was not loaded when the error occured and load avg was just 2 (2cpus 16cores) and the php5-fpm processes seemed to be working fine. </p>
  
  <p>nginx conf:</p>
  
  <pre><code>user www-data;
  worker_processes 14;
  pid /var/run/nginx.pid;
  # set open fd limit to 30000
  worker_rlimit_nofile 30000;
  
  events {
          worker_connections 768;
          # multi_accept on;
  }
  
  http {
  
          ##
          # Basic Settings
          ##
  
          sendfile on;
          tcp_nopush on;
          tcp_nodelay on;
          keepalive_timeout 65;
          types_hash_max_size 2048;
          # server_tokens off;
  
          # server_names_hash_bucket_size 64;
          # server_name_in_redirect off;
  
          include /etc/nginx/mime.types;
          default_type application/octet-stream;
  
          ##
          # Logging Settings
          ##
  
          access_log /var/log/nginx/access.log;
          error_log /var/log/nginx/error.log;
  
          ##
          # Gzip Settings
          ##
  
          gzip on;
          gzip_disable "msie6";
  
          # gzip_vary on;
          # gzip_proxied any;
          # gzip_comp_level 6;
          # gzip_buffers 16 8k;
          # gzip_http_version 1.1;
          # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  
          ##
          # Virtual Host Configs
          ##
  
          include /etc/nginx/conf.d/*.conf;
          include /etc/nginx/sites-enabled/*;
  }
  </code></pre>
  
  <p>nginx enabled site conf:</p>
  
  <pre><code>    location ~* \.php$ {
          fastcgi_split_path_info ^(.+\.php)(.*)$;
          fastcgi_pass   backend;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          include 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_intercept_errors        off;
          fastcgi_ignore_client_abort     off;
          fastcgi_connect_timeout 20;
          fastcgi_send_timeout 20;
          fastcgi_read_timeout 180;
          fastcgi_buffer_size 128k;
          fastcgi_buffers 4 256k;
          fastcgi_busy_buffers_size 256k;
          fastcgi_temp_file_write_size 256k;
      }
  
  ## Disable viewing .htaccess &amp; .htpassword
      location ~ /\.ht {
          deny  all;
      }
  }
  upstream backend {
          server 127.0.0.1:9000;
  }
  </code></pre>
  
  <p>fpm conf:</p>
  
  <pre><code>pm.max_children = 500
  pm.start_servers = 100
  pm.min_spare_servers = 50
  pm.max_spare_servers = 100
  pm.max_requests = 10000
  </code></pre>
  
  <p>There are emergency restart settings in fpm conf file. 
  I do not know if they help us fix the issue?</p>
  
  <pre><code>emergency_restart_interval = 0
  </code></pre>