title: 502 Bad Gateway on Ubuntu 14.04 with Nginx 1.6.2 and PHP 5.6.4
body: <p>I'm getting a 502 Bad Gateway response on scripts that takes <strong>longer than 5 minutes</strong>.</p>
  
  <p>The server runs on Ubuntu 14.04 with Nginx 1.6.2 and PHP 5.6.4</p>
  
  <p>I'v created a test function, which fails:</p>
  
  <pre><code>    public function test(){
          sleep(600);
      }
  </code></pre>
  
  <p>I'v tried changing the fastcgi params, with no change.</p>
  
  <p><strong>Config:</strong></p>
  
  <pre><code>server {
      listen 80;
      server_name www.xxxx.xx;
      root /home/forge/xxxx.xx/public;
  
      # FORGE SSL (DO NOT REMOVE!)
      # ssl_certificate;
      # ssl_certificate_key;
  
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  
      index index.html index.htm index.php;
  
      charset utf-8;
  
      location / {
          try_files $uri $uri/ /index.php?$query_string;
      }
  
      location = /favicon.ico { access_log off; log_not_found off; }
      location = /robots.txt  { access_log off; log_not_found off; }
  
      access_log off;
      error_log  /var/log/nginx/xxxx.xx-error.log error;
  
      error_page 404 /index.php;
  
      location ~ \.php$ {
          fastcgi_param PHP_VALUE "newrelic.appname=xxxx.xx";
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass unix:/var/run/php5-fpm.sock;
          fastcgi_index index.php;
          include fastcgi_params;
      }
  
      location ~ /\.ht {
          deny all;
      }
  
      location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
          expires 365d;
      }
  
  }
  </code></pre>
  
  <p>nginx.conf http block (The part I'v changed):</p>
  
  <pre><code>http {
      fastcgi_buffers 32 32k;
      fastcgi_buffer_size 32k;
      fastcgi_connect_timeout 3600;
      fastcgi_send_timeout 3600;
      fastcgi_read_timeout 3600;
      # EDIT: Added and tried these:
      client_header_timeout 3600;
      client_body_timeout 3600;
      proxy_read_timeout 3600;
      proxy_connect_timeout 3600;
  
  }
  </code></pre>
  
  <p>php.ini:</p>
  
  <pre><code>max_execution_time = 3600
  </code></pre>
  
  <p><strong>Log error:</strong></p>
  
  <pre><code>2015/10/20 10:01:12 [error] 5132#0: *297705 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 195.215.168.170, server: xxxx.xx, request: "GET /cron/test HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "xxxx.xx"
  </code></pre>
  
  <p>Spent endless hours searching for solutions, but every solution I'v tried havn't had any effect. So any help is welcomed with open arms!</p>