Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Nginx, Unicorn and multiple Rails apps

nginx unicorn subdirectory

  • Please log in to reply
No replies to this topic

#1 Jakson Rochelly

Jakson Rochelly

    Passenger

  • Members
  • 6 posts
  • LocationPalmas, TO - Brazil

Posted 15 August 2013 - 02:38 AM

I've posted a tutorial about how to implement rails in subdirectory, I thought it'd be nice to share here.

 

It's really easy to make a rails app run using unicorn + nginx, but when it comes to multiple apps, things get a lot harder, well, at least that's what I thought.

 

unicorn.png

 

What I want is to make the root ("/") folder a place on the server where you can put HTML orPHP and use subdirectories like "/app1", for exemple, to provide a rails app. Doing this correctly, makes a lot easier for me to deploy others apps using folders instead of http ports. It was really hard to find this better/easier way to run multiple rails applications and that's what makes me want to share with you.

 

In this post I'm going to show you how to make your server run rails on subdirectory. For this tutorial, I'm using Ruby 2.0 and Rails 4.0. I suppose you already have nginx and unicorn installed. So, let's get started!

In you nginx.conf file we are going to make nginx point to a unicorn socket:

upstream unicorn_socket_for_myapp {
  server unix:/home/coffeencoke/apps/myapp/current/tmp/sockets/unicorn.sock fail_timeout=0;
}

Then, with your server listening to port 80, add a location block that points to the subdirectory your rails app is (this code, must be inside server block):

location /myapp/ {
  try_files $uri @unicorn_proxy;
}

location @unicorn_proxy {
  proxy_pass http://unix:/home/coffeencoke/apps/myapp/current/tmp/sockets/unicorn.sock;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_redirect off;
  proxy_set_header X-Forwarded-Proto $scheme;
}

Now you can just Unicorn as a Deamon:

sudo unicorn_rails -c config/unicorn.rb -D

The last thing to do, and the one I dug the most is to add a scope for your rails routes file, like this:

MyApp::Application.routes.draw do
  scope '/myapp' do
    root :to => 'welcome#home'

    # other routes are always inside this block
    # ...
  end
end

This way, your app will map a link /myapp/welcome, instead of just /welcome

 

Also, we need to make rails see his in a subdirectory. At the end of your production.rb file, add:

config.relative_url_root = "/myapp"

So now our app opens using "/myapp" subdirectory, but, hey! Where's my assets? Well, rails still believe your assets are at "/assets" when they should be at "/myapp/assets". Let's solve this:

 

Into your config/applcation.rb file add:

config.assets.prefix = "/myapp/assets"

If in production environment your css or js are not being found, maybe "bundle exec rake assets:precompile RAILS_RELATIVE_URL_ROOT=/mypapp" solves the problem.

 

Now we're done. :)

 

My blog post is here.

 

Feedback would be great! :D


  • Kelli Shaver and wilsonsilva like this





Also tagged with one or more of these keywords: nginx, unicorn, subdirectory

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users