Request timeout with Nginx, Unicorn and Rails

Asked

Viewed 223 times

0

I have an application in Rails that runs with Unicorn in production. Some calls take a long time to process. I set the server to increase the timeout, so that the server does not respond with error. The problem is that if the request takes more than 30 seconds, I get this message:

Service Unavailable

The service is temporarily unavailable. Please Try Again later.

In the unicorn.rb have timeout 120 configured and in Nginx.conf have:

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 setting I forgot?

Link to the original English question

  • Try increasing keepalive_timeout to 120.

  • It didn’t work @Guigs

  • 1

    I discovered the problem: my server is behind a Rackspace Load Balancer, which puts 30s timeout. I used these instructions to solve. https://community.rackspace.com/products/f/25/t/89

1 answer

1

The configuration used is correct. The problem in this case is that the server is after a load balancer in Rackspace, which has by default 30 seconds of timeout. To change that value follow these instructions. In short, there are 2 API calls:

1) Obtain 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 the timeout of the Load Balancer:

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>'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.