4

I have an application in rails and it runs unicorn in production. There are some requests that take too long to process. I have configured the server to increase the timeout, so these request work correctly. The problem is that when the request takes more than 30 seconds to respond, I get this message:

Service Unavailable

The service is temporarily unavailable. Please try again later.

In unicorn.rb I have timeout 120 configured and my nginx.conf is:

upstream unicorn_my_app {
    server unix:/tmp/my_app.socket fail_timeout=0;
}

server {
        listen 80;
        client_max_body_size 4G;
        server_name www.my_app.com;

        proxy_read_timeout 120;

        keepalive_timeout 5;

        root /home/ubuntu/my_app/current/public;

        location / {
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_redirect off;

          if (!-f $request_filename) {
            proxy_pass http://unicorn_my_app;
            break;
          }
        }

        error_page 404 500 502 503 504 /erro/erro.html;
        location = /erro/ {
          root /home/ubuntu/my_app/current/public;
        }
}

Is there any other configuration that I forgot?

Daniel Cukier
  • 823
  • 1
  • 10
  • 18
  • 2
    You should fix the application. Return immediately to the user and start a background job. – Michael Hampton Nov 07 '14 at 16:55
  • 1
    Hi Michael, unfortunately I can't do this at the present moment, I want to have a temporary solution until I solve this – Daniel Cukier Nov 07 '14 at 23:12
  • Any output in nginx error.log when this happened? – masegaloeh Nov 12 '14 at 01:48
  • No output in nginx error.log – Daniel Cukier Nov 12 '14 at 02:02
  • Could you enable debug log as explained in [documentation page](http://nginx.org/en/docs/debugging_log.html) and post the output? – masegaloeh Nov 12 '14 at 06:40
  • I think I found the problem. The server is behind Rackspace Load Balance and the LB has a 30s timeout. I noticed it because in the debug log I saw the load balance ip: 2014/11/12 12:12:35 [info] 17859#0: *101 client prematurely closed connection, so upstream connection is closed too while sending request to upstream, client: , server: www.playax.com, request: "GET /admin_dashboard HTTP/1.1", upstream: "http://unix:/tmp/my_app.socket:/my_url", host: "www.my_app.com" – Daniel Cukier Nov 12 '14 at 12:18
  • The solution is here: https://community.rackspace.com/products/f/25/t/89 – Daniel Cukier Nov 12 '14 at 15:25

1 Answers1

1

The configuration used is right. The problem in this case is that you are behind Rackspace Load Balance, which by default set timeout to 30 seconds. To change Rackspace Load Balance timeout value, follow this instructions. In abstract, you do two API calls:

1) Get a token:

curl -s -d \
'{
    "auth":
    {
       "RAX-KSKEY:apiKeyCredentials":
       {  
          "username": "your_api_username",  
          "apiKey": "your_api_key"}
    }  
}' \
-H 'Content-Type: application/json' \
'https://identity.api.rackspacecloud.com/v2.0/tokens' | python -m json.tool

2) Change LB timeout:

curl -s -d \ '{"loadBalancer":{
    "timeout": 120
    } }' \
-H 'X-Auth-Token: token_returned_in_last_request' \
-H 'Content-Type: application/json' \
-X PUT \ 'https://iad.loadbalancers.api.rackspacecloud.com/v1.0/<your_customer_id>/loadbalancers/<your_lb_id>'
Daniel Cukier
  • 823
  • 1
  • 10
  • 18