Report this

What is the reason for this report?

504 Gateway Timeout with One-Click Rails Install

Posted on September 6, 2014

I am attempting to get my rails application to work on the Ubuntu 14.04 one click rails install image. I am following tutorial at https://www.digitalocean.com/community/tutorials/how-to-launch-your-ruby-on-rails-app-with-the-digitalocean-one-click-image. The sample application displays without issue. I developed a sample microposts application following Michael Hartl’s Rails Tutorial. I modified the Gemfile to use mysql2 and then changed my database.yml to point to the mysql2 server instead of using the Heroku deploy method. I then uploaded my entire kit and kaboodle to /home/rails as the user root. After following the instructions to do the bundle install, RAILS_ENV=production rake db:migrate, and bundle assets precompile I go to load the rails application and get a 504 gateway timeout. Static pages load without issue (i.e. http://104.131.5.195/404.html) but the front page does not.

If I tail -40 my unicorn log I get:

root@nathan-hale:/home/rails# tail -40 /home/unicorn/log/unicorn.log 
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/bundler-1.3.5/lib/bundler/setup.rb:17:in `<top (required)>'
/usr/local/rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:135:in `require'
/usr/local/rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:135:in `rescue in require'
/usr/local/rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:144:in `require'
/home/rails/config/boot.rb:4:in `<top (required)>'
/usr/local/rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/usr/local/rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/home/rails/config/application.rb:1:in `<top (required)>'
/usr/local/rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/usr/local/rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/home/rails/config/environment.rb:2:in `<top (required)>'
/usr/local/rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/usr/local/rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
config.ru:4:in `block in <main>'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
config.ru:1:in `new'
config.ru:1:in `<main>'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn.rb:48:in `eval'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn.rb:48:in `block in builder'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:750:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:750:in `build_app!'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:623:in `init_worker_process'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:643:in `worker_loop'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:527:in `spawn_missing_workers'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:538:in `maintain_worker_count'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:303:in `join'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/bin/unicorn:126:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.0.0-p353/bin/unicorn:23:in `load'
/usr/local/rvm/gems/ruby-2.0.0-p353/bin/unicorn:23:in `<main>'
/usr/local/rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>'
E, [2014-09-06T23:34:37.382827 #29321] ERROR -- : reaped #<Process::Status: pid 3365 exit 1> worker=1
I, [2014-09-06T23:34:37.383062 #29321]  INFO -- : worker=1 spawning...
E, [2014-09-06T23:34:37.385768 #29321] ERROR -- : reaped #<Process::Status: pid 3367 exit 1> worker=0
I, [2014-09-06T23:34:37.385948 #29321]  INFO -- : worker=0 spawning...
I, [2014-09-06T23:34:37.386960 #3371]  INFO -- : worker=1 spawned pid=3371
I, [2014-09-06T23:34:37.387144 #3371]  INFO -- : Refreshing Gem list
I, [2014-09-06T23:34:37.391896 #3373]  INFO -- : worker=0 spawned pid=3373
I, [2014-09-06T23:34:37.392136 #3373]  INFO -- : Refreshing Gem list

if I trail -f my nginx log I get:

2014/09/06 22:23:54 [error] 690#0: *65 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 70.188.169.36, server: _, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "104.131.5.195"
2014/09/06 22:31:44 [error] 690#0: *80 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 70.188.169.36, server: _, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "104.131.5.195"
2014/09/06 22:49:10 [error] 690#0: *84 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 70.188.169.36, server: _, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "104.131.5.195"
2014/09/06 22:53:03 [error] 690#0: *86 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 70.188.169.36, server: _, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "104.131.5.195"
2014/09/06 23:11:25 [crit] 13103#0: *1 connect() to unix:/home/rails/tmp/sockets/unicorn.sock failed (2: No such file or directory) while connecting to upstream, client: 70.188.169.36, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/rails/tmp/sockets/unicorn.sock:/", host: "104.131.5.195"
2014/09/06 23:11:27 [crit] 13103#0: *1 connect() to unix:/home/rails/tmp/sockets/unicorn.sock failed (2: No such file or directory) while connecting to upstream, client: 70.188.169.36, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/rails/tmp/sockets/unicorn.sock:/", host: "104.131.5.195"
2014/09/06 23:17:38 [error] 18044#0: *1 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 70.188.169.36, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/rails/tmp/sockets/unicorn.sock/", host: "104.131.5.195"
2014/09/06 23:18:40 [error] 18044#0: *1 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 70.188.169.36, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/rails/tmp/sockets/unicorn.sock/", host: "104.131.5.195"
2014/09/06 23:25:52 [error] 18044#0: *4 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 70.188.169.36, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/rails/tmp/sockets/unicorn.sock/", host: "104.131.5.195"
2014/09/06 23:28:55 [error] 18044#0: *6 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 70.188.169.36, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/rails/tmp/sockets/unicorn.sock/", host: "104.131.5.195"

I tried the following suggestions from other threads: commenting out user: rails in /home/unicorn/unicorn.conf, changing from listening on 127.0.0.1:8080 to using a socket (http://recipes.sinatrarb.com/p/deployment/nginx_proxied_to_unicorn) both of which made no difference.

I am using the default nginx.conf other than the change to the socket now:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events { worker_connections 1024; }

http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        gzip on;
        gzip_disable "msie6";
        gzip_types text/plain text/xml text/css text/comma-separated-values;
        upstream app_server { server unix:/home/rails/tmp/sockets/unicorn.sock  fail_timeout=0; }

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

My unicorn.conf is:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events { worker_connections 1024; }

http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        gzip on;
        gzip_disable "msie6";
        gzip_types text/plain text/xml text/css text/comma-separated-values;
        upstream app_server { server unix:/home/rails/tmp/sockets/unicorn.sock  fail_timeout=0; }

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

Some other interesting tidbits: New Relic sees my application but seems to believe I am in the development environment. So this leads me to believe I need to fix this somehow. It is unclear what more to do since I have done RAILS_ENV=production and if I echo $RAILS_ENV I get production.

I do not believe this is an nginx problem based on its log, it seems to be unicorn.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

This question was answered by @mzagaja:

Ok so I resolved the unicorn issue by manually installing all the gems, which is good. I think the issue was that I setup the rails application with its own gemset with rvm that the systemwide unicorn was accessing only the systemwide gems. So I learned a lot there.

Now experiencing some ruby/rails growing pains as I’m learning things like the fact that force_ssl was on in production.rb and I didn’t have an SSL certificate for the digital ocean server. I am learning that heroku did a lot for me, at least in relation to this Michael Hartl tutorial that I otherwise didn’t consider.

You can see the comment here.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.