title: How to configure nginx + Unicorn to avoid timeout errors?
body: <p>I have a Rails (v3.2.13, Ruby 2.0.0) application running on nginx + Unicorn (Ubuntu 12.04). All is working well, except when an admin user is uploading users (thousands) via a CVS file. The problem is that I have set timeout to 30 seconds and the import process takes much more time. So, after 30 seconds I get an nginx 502 Bad Gateway page (Unicorn worker is killed).</p>
  
  <p>The obvious solution is to increase timeout, but I don't want this because it'll cause another problems (I guess), because it's not a typical behavior.</p>
  
  <p>Is there a way to handle this kind of problems?</p>
  
  <p>Thanks a lot in advance.</p>
  
  <p>PS: Maybe a solutions is to modify the code. If so, I want to avoid the user to perform another request.</p>
  
  <p>Some ideas (don't know if possible):</p>
  
  <ul>
  <li>Setup a worker dedicated to this request.</li>
  <li>Send a "work in progress" signal to Unicorn to avoid to be killed.</li>
  </ul>
  
  <p><strong>nginx-app.conf</strong></p>
  
  <pre><code>upstream xxx {
    server unix:/tmp/xxx.socket fail_timeout=0;
  }
  
  
  server {
    listen   80; 
  
    ...
  
    location / {
  
      proxy_pass  http://xxx;
      proxy_redirect     off;
      ...
  
      proxy_connect_timeout      360;
      proxy_send_timeout         360;
      proxy_read_timeout         360;
    }
  }
  </code></pre>
  
  <p><strong>unicorn.rb</strong></p>
  
  <pre><code>worker_processes 2
  
  listen "/tmp/xxx.socket"
  
  timeout 30
  
  pid "/tmp/unicorn.xxx.pid"
  </code></pre>