How to configure nginx + Unicorn to avoid timeout errors?

I have a Rails application (v3.2.13, Ruby 2.0.0) running on nginx + Unicorn (Ubuntu 12.04). Everything works well, unless the admin user downloads (thousands) of users through a CVS file. The problem is that I set the timeout to 30 seconds and the import process takes much longer. So, after 30 seconds, I get the nginx 502 Bad Gateway page (killed working Unicorn).

The obvious solution is to increase the timeout, but I do not want this because it will cause other problems (I think), because this is not typical behavior.

Is there any way to deal with such problems?

Thank you very much in advance.

PS: Maybe this is a solution to change the code. If so, I want the user to not execute another request.

Some ideas (I don’t know if this is possible):

  • Set up a worker dedicated to this request.
  • Send Work in progress to Unicorn to avoid killing.

Nginx-app.conf

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

unicorn.rb

 worker_processes 2 listen "/tmp/xxx.socket" timeout 30 pid "/tmp/unicorn.xxx.pid" 
+7
ruby-on-rails timeout nginx unicorn
source share
2 answers

This is a good reason to create a queue. And you will:

  • upload csv file (which should be within 30 seconds)
  • your background job that will import user data (which can work for hours ...)
  • While this task is being carried out, you can serve some WIP page with the status of work / percent / etc.

Check out https://github.com/resque/resque for example. There are many other lines.

+9
source share

Is there any way to deal with such problems?

Performing tasks in the background. You must have a separate process that receives tasks from the queue one by one and processes them. And since it does not work with user queries, it can do its job as much as necessary. For this, you do not need a unicorn, just a separate demon.

+2
source share

All Articles