Nginx + Ruby On Rails = 504 Gateway Time-out

August 28, 2014 8.2k views

Hi there!

5th droplet attempt at DigitalOcean. I've followed all the steps provided by DigitalOcean guides (one click image etc...). I've uploaded my site by FTP, run bundle install, run rake db:migrate, I've even connected to my DB remotely and populated it with an old backup. Everything is perfect.

Except one thing. I haven't been able to run my site not once. I get the classic "504 Gateway Time-out" error many have posted here although none has a response yet.

What is going on?

A few notes...

When running bundle install:

Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this application for all non-root users on this machine.

nginx/error.log

2014/08/28 21:47:32 [error] 16805#0: *32 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 190.18.93.156, server: _, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "objectservers.com.ar"

log/unicorn.log

/usr/local/rvm/gems/ruby-2.0.0-p353/gems/bundler-1.7.2/lib/bundler.rb:302: warning: Insecure world writable dir /home/rails in PATH, mode 040777
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/bundler-1.7.2/lib/bundler.rb:302: warning: Insecure world writable dir /home/rails in PATH, mode 040777
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/bundler-1.7.2/lib/bundler/runtime.rb:34:in `block in setup': You have already activated rack 1.6.0.beta, but your Gemfile requires rack 1.5.2. Prepending `bundle exec` to your command may solve this. (Gem::LoadError)
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/bundler-1.7.2/lib/bundler/runtime.rb:19:in `setup'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/bundler-1.7.2/lib/bundler.rb:121:in `setup'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/bundler-1.7.2/lib/bundler/setup.rb:17:in `<top (required)>'
    from /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'
    from /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'
    from /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'
    from /home/rails/config/boot.rb:4:in `<top (required)>'
    from /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'
    from /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'
    from /home/rails/config/application.rb:1:in `<top (required)>'
    from /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'
    from /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'
    from /home/rails/config/environment.rb:2:in `<top (required)>'
    from /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'
    from /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'
    from config.ru:4:in `block in <main>'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/rack-1.6.0.beta/lib/rack/builder.rb:55:in `instance_eval'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/rack-1.6.0.beta/lib/rack/builder.rb:55:in `initialize'
    from config.ru:1:in `new'
    from config.ru:1:in `<main>'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn.rb:48:in `eval'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn.rb:48:in `block in builder'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:750:in `call'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:750:in `build_app!'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:623:in `init_worker_process'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:643:in `worker_loop'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:527:in `spawn_missing_workers'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:538:in `maintain_worker_count'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:303:in `join'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/bin/unicorn:126:in `<top (required)>'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/bin/unicorn:23:in `load'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/bin/unicorn:23:in `<main>'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/bundler-1.7.2/lib/bundler/runtime.rb:34:in `block in setup': You have already activated rack 1.6.0.beta, but your Gemfile requires rack 1.5.2. Prepending `bundle exec` to your command may solve this. (Gem::LoadError)
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/bundler-1.7.2/lib/bundler/runtime.rb:19:in `setup'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/bundler-1.7.2/lib/bundler.rb:121:in `setup'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/bundler-1.7.2/lib/bundler/setup.rb:17:in `<top (required)>'
    from /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'
    from /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'
    from /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'
    from /home/rails/config/boot.rb:4:in `<top (required)>'
    from /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'
    from /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'
    from /home/rails/config/application.rb:1:in `<top (required)>'
    from /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'
    from /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'
    from /home/rails/config/environment.rb:2:in `<top (required)>'
    from /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'
    from /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'
    from config.ru:4:in `block in <main>'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/rack-1.6.0.beta/lib/rack/builder.rb:55:in `instance_eval'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/rack-1.6.0.beta/lib/rack/builder.rb:55:in `initialize'
    from config.ru:1:in `new'
    from config.ru:1:in `<main>'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn.rb:48:in `eval'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn.rb:48:in `block in builder'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:750:in `call'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:750:in `build_app!'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:623:in `init_worker_process'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:643:in `worker_loop'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:527:in `spawn_missing_workers'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:538:in `maintain_worker_count'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:303:in `join'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/bin/unicorn:126:in `<top (required)>'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/bin/unicorn:23:in `load'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/bin/unicorn:23:in `<main>'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>'
E, [2014-08-28T21:52:49.144863 #32524] ERROR -- : reaped #<Process::Status: pid 4237 exit 1> worker=1
I, [2014-08-28T21:52:49.145159 #32524]  INFO -- : worker=1 spawning...
E, [2014-08-28T21:52:49.145979 #32524] ERROR -- : reaped #<Process::Status: pid 4234 exit 1> worker=0
I, [2014-08-28T21:52:49.146203 #32524]  INFO -- : worker=0 spawning...
I, [2014-08-28T21:52:49.147495 #4240]  INFO -- : worker=1 spawned pid=4240
I, [2014-08-28T21:52:49.147709 #4240]  INFO -- : Refreshing Gem list
I, [2014-08-28T21:52:49.151910 #4242]  INFO -- : worker=0 spawned pid=4242
I, [2014-08-28T21:52:49.152180 #4242]  INFO -- : Refreshing Gem list

I would really appreciate any help. It's been days I've been meaning to finally try out DigitalOcean.

3 Answers
  1. Running Bundler as root. It says don't do that, so don't do that. Create a non-root user and run Bundler as that user.
  2. Probably caused by #3.
  3. warning: Insecure world writable dir /home/rails in PATH, mode 040777 - you should really fix that, but it's not the problem.

The useful information in stack traces is usually in the first line or so. In this case, it's this:

You have already activated rack 1.6.0.beta, but your Gemfile requires rack 1.5.2. Prepending `bundle exec` to your command may solve this. (Gem::LoadError)

It even gives you the solution to the problem. Put bundle exec before the command you use to start your application. See if it works better.

  • Awesome! So now I have a few questions.

    1. How do I run bundler not as root? I logged in as "rails" using the "su - rails" command but neither the bundle nor rails commands were recognized. I logged in as "rails" since the app is hosted at /home/rails.
    2. Which chmod command should I run to make it safe? Surely it's not the issue, but I would love to fix that. But I don't know what to set it to without giving me writing issues.
    3. Where do I add the "bundle exec" to? This is running Nginx + Unicorn, so I guess Unicorn is what's auto-launching rails, right? Where do I edit that?

    Thank you, I already really really appreciate your response. It's been a week of swimming in a sea of no responses xD.

    1. When I set up a droplet, I create a user named "larry" (because that’s my name), and I log in as that user and do all my work as that user, using "sudo" when I need root privileges. See the tutorials on setting up a droplet if you need to learn how to create a user with sudo privileges.

    2. Permissions of 775 should stop the warning. The three digits refer to owner, group, and everyone (world or all), respectively. The values are read (4), write (2), and execute (1 - when referring to a directory, execute means being able to cd to the directory). Add the numbers together to determine the permissions on a given file or directory - read + write + execute = 7, read + write = 6, read + execute = 5, etc. In this case, it's the third digit we're really concerned about. If it's 777, everybody can read and write to that directory, which is somewhat less than secure. Changing it to 775 (owner rwx, group rwx, world r-x) should silence the warning. It might be slightly more permissive than it needs to be - 755 might be more appropriate - but 775 is probably good enough. The command to use would be either chmod 775 /path/to/app or chmod a -w /path/to/app. The first form specifies the permissions explicitly, and the second just removes write permission (-w) for all (a). I tend to use the first form because I get it wrong less often. More than once, I have changed permissions for the owner (o) because I think of "others" instead of using "a" for "all".

    3. You are presumably either launching the application yourself using unicorn_rails (I think that's the command - I haven't done a lot of work with Rails since Unicorn came along) or you have that command in a script somewhere. So instead of unicorn_rails myapp, you use bundle exec unicorn_rails myapp.

  • Hey there! Okay, I got around the chmod part :P.

    Now, as for the points 1 and 3...

    Point 1

    I created a new user, and everything seemed to work better, until I try to do bundle install as the user. I get this:

    Errno::EACCES: Permission denied - activeadmin-1.0.0.pre.gem
    An error occurred while installing activeadmin (1.0.0.pre), and Bundler cannot continue.
    Make sure that `gem install activeadmin -v '1.0.0.pre'` succeeds before bundling.
    

    So I try sudo bundle install, but I get this:

    sudo: unable to execute /usr/local/bin/bundle: No such file or directory
    

    Why would it stop finding it??

    Point 3

    I don't manually start the server. It seems to autostart on boot. Is there a place I can change that startup command?

    Thank you very much!

  • Point 1
    "Permission denied" indicates a permissions problem. Could be that the advice I gave you to set 755 on the application directory is too restrictive. If that's what you did, set it back to 775 and see if it works. Most likely, it will. If that doesn't help, I will go a different direction, which might be the better thing to do anyway. What I'm thinking the problem is, is this: The new user you created is not the owner of the application directory, and is not a member of the owner's group. (Add this stuff to your list of things to learn about Linux administration. You will use it a lot.) Here is what I mean by that.

    Samarium:site lfransson$ ls -l
    total 72
    -rw-r--r--@  1 lfransson  staff    188 Aug 16 21:32 Gemfile
    -rw-r--r--   1 lfransson  staff    827 Aug 16 21:42 Gemfile.lock
    -rw-r--r--@  1 lfransson  staff   1014 Aug 16 20:54 Guardfile
    -rw-r--r--@  1 lfransson  staff     98 Jun  8  2013 Rakefile
    -rwxr-xr-x@  1 lfransson  staff   4235 Aug  9 23:24 Rules
    

    That is a few lines of a directory listing, which you get by typing ls -l at the command prompt. The first bunch of stuff is the file permissions. The owner of all of those files is lfransson. The group is staff. As the owner, I have read and write permissions on all of those files. Anybody who is a member of the staff group has read permission (and execute on the Rules file, which isn't actually executable, but I digress).

    So I think the problem here is that you are trying to install gems using bundle install in a directory which you do not own, are not a member of the appropriate groups, and since you have removed world writability, you cannot write to it. It is possible to manage group membership and add yourself to the group, but I have never done it.

    There is a solution: chown. sudo chown -R <your_user_here> /path/to/myapp will make your user the owner of the application directory and everything inside. As that user, you should be able to do anything you want to do with it.

    If that doesn't solve the problem, I'm going to be a little bit stuck.

    As for why sudo bundle install didn't work, first keep in mind that using sudo is running as root, which is what we were trying to avoid. It also relates to paths and, probably, the use of RVM. When you use RVM, the gem's executable gets installed in a directory that isn't on the standard path. (For an illustration, type echo $PATH and sudo echo $PATH at a command prompt and compare the output of each. Those are the places that get searched for an executable when you run a command. RVM (something of a black box to me) does its magic and finds the appropriate place. Since root is probably not set up to use RVM, its path is not appropriately configured to find the executable.

    Point 3
    If the application is auto starting on boot, there must be a script somewhere. The most likely place is /etc/init.d/. If it's not there, there might be something under /etc/unicorn/, but that is purely a guess on my part, based only on something I read once about auto starting applications using Thin. If you can't find anything in those places, I'd say you should search tutorial or documentation that got you to this point for any evidence of a startup script. There must be something there somewhere.

Just wanted to chime in that following Digital Ocean's tutorial (https://www.digitalocean.com/community/tutorials/how-to-use-the-ruby-on-rails-one-click-application-on-digitalocean#configuration-details), I get this exact same problem. Trying to run 'bundle install' as root gets you the warning from Bundler that you shouldn't do that, but trying to run it as rails user gets you the error that you do not have permission to install gems. Awesome! I chose to run it as root, and now have the Rack version problem with Unicorn. Will try some of these solutions and see if I can get my app running.

**Archived:** DigitalOcean's Ruby on Rails One-Click application provides a convenient way to get your Rails app up and running quickly. With Nginx, Unicorn, and Postgres all pre-installed, the One-Click is a great base for hosting your app. This tutorial will give you all the details you need to get your project off the ground.
Have another answer? Share your knowledge.