CentOS - Nginx, Rails and Thin

Having installed the thin web server for Ruby, we can now look at configuring a Nginx vhost to proxy to thin so we can serve our Ruby on Rails application.

The process is easy to follow and easy to repeat for hosting multiple domains.


Setup

Firstly you will have needed to follow the previous Nginx articles and installed via yum or via source.

You will also need to have installed the thin web server as per the article linked to above.

Plan

The plan is very simple:

We'll create a basic rails application and use 3 thin instances running from port 3000 to 3002.

I won't go into detail as the [thin web server for Ruby] article shows how to install and configure thin.

Once that is done, we'll create a simple vhost to proxy requests to the thin instances.

You may also notice this article is very similar to the Nginx and mongrels article. There is a very good reason for that - they use exactly the same methods in creating the virtual host.

All the virtual host has to do is proxy requests to the 3rd party web server - in this case thin, in the sister article, mongrels.

Rails Application

As the default permissions only allow us, the 'demo' user, to browse our home folder, let's start off by giving Nginx access to this folder as well:

chmod 755 /home/demo

To create a rail application, move into your public_html folder:

cd /home/demo/public_html

and create a new Ruby on Rails application:

rails railsapp

Done.

Thin

Ensure you are in the rails folder:

cd ~/public_html/railsapp

Then create a thin configuration file:

sudo thin config -C /etc/thin/railsapp.yml -c /home/demo/public_html/railsapp/  --servers 3 -e production

It's always a good idea to check the created file:

cat /etc/thin/railsapp.yml

The contents are as such:

pid: tmp/pids/thin.pid
log: log/thin.log
timeout: 30
port: 3000
max_conns: 1024
max_persistent_conns: 512
environment: production
chdir: /home/demo/public_html/railsapp
require: []

address: 0.0.0.0
servers: 3
daemonize: true

Unlike with mongrels, we don't need to manually create symlinks, etc to make sure thin is started on a reboot.

Now all we need to do is start thin:

sudo /etc/init.d/thin start

Done.

Nginx Virtual Host

Let's create the Nginx vhost:

sudo nano /etc/nginx/conf.d/virtual.conf

Note: If you installed Nginx from source, the path may vary to something like:

sudo nano /usr/local/nginx/conf/conf.d/virtual.conf

The contents of the file are as such:

upstream domain1 {
        server 127.0.0.1:3000;
        server 127.0.0.1:3001;
        server 127.0.0.1:3002;
    }

server {
            listen   80;
            server_name  www.domain.com;
            rewrite ^/(.*) http://domain.com permanent;
           }


server {
            listen   80;
            server_name domain.com;

            access_log /home/demo/public_html/railsapp/log/access.log;
            error_log /home/demo/public_html/railsapp/log/error.log;

            root   /home/demo/public_html/railsapp/public/;
            index  index.html;

            location / {
                          proxy_set_header  X-Real-IP  $remote_addr;
                          proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                          proxy_set_header Host $http_host;
                          proxy_redirect false;

                          if (-f $request_filename/index.html) {
                                           rewrite (.*) $1/index.html break;
                          }

                          if (-f $request_filename.html) {
                                           rewrite (.*) $1.html break;
                          }

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

}

Take each section at a time and you will see that the basics are the same as for a 'normal' Nginx vhost.

We have the server_name, listen, log and index variables.

Where it differs is the addition of the Rails proxy settings.

In this example, we will use 3 thin instances running on ports 3000, 3001 and 3002 which I have defined in the 'upstream' setting. I've called it domain1 for ease of use.

The location settings say that if the requested file exists to serve the static version straight away.

And if the requested file doesn't exist, pass the request to the thin server.

Restart

Remember that we need to enable any available vhosts or it won't be served (an easy thing to leave out).

Use the 'stop' and 'start' method as issuing a restart command does not always work with Nginx.

sudo /etc/init.d/nginx stop
...
sudo /etc/init.d/nginx start

Done.

Navigate

All that's left is to navigate to your domain:

http://www.domain.com

Where you will be greeted with the rails welcome page.

Summary

Setting up Nginx virtual hosts to proxy to the thin server easily completed.

To serve multiple domains, simply repeat the process with the new domain details.

Article Comments:

Rodrigo commented Mon Feb 27 15:12:07 UTC 2012:

Hi,

Thanks for your tutorial, but when I run thi, I get the following error: /usr/local/rvm/gems/ruby-1.8.7-p358/gems/eventmachine-0.12.10/lib/eventmachine.rb:572:in start_tcp_server': no acceptor (RuntimeError) from /usr/local/rvm/gems/ruby-1.8.7-p358/gems/eventmachine-0.12.10/lib/eventmachine.rb:572:instart_server' from /usr/local/rvm/gems/ruby-1.8.7-p358/gems/thin-1.3.1/lib/thin/backends/tcp_server.rb:16:in connect' from /usr/local/rvm/gems/ruby-1.8.7-p358/gems/thin-1.3.1/lib/thin/backends/base.rb:53:instart' from /usr/local/rvm/gems/ruby-1.8.7-p358/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in call' from /usr/local/rvm/gems/ruby-1.8.7-p358/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:inrun_machine' from /usr/local/rvm/gems/ruby-1.8.7-p358/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in run' from /usr/local/rvm/gems/ruby-1.8.7-p358/gems/thin-1.3.1/lib/thin/backends/base.rb:61:instart' from /usr/local/rvm/gems/ruby-1.8.7-p358/gems/thin-1.3.1/lib/thin/server.rb:159:in start' from /usr/local/rvm/gems/ruby-1.8.7-p358/gems/thin-1.3.1/lib/thin/controllers/controller.rb:86:instart' from /usr/local/rvm/gems/ruby-1.8.7-p358/gems/thin-1.3.1/lib/thin/runner.rb:185:in send' from /usr/local/rvm/gems/ruby-1.8.7-p358/gems/thin-1.3.1/lib/thin/runner.rb:185:inrun_command' from /usr/local/rvm/gems/ruby-1.8.7-p358/gems/thin-1.3.1/lib/thin/runner.rb:151:in run!' from /usr/local/rvm/gems/ruby-1.8.7-p358/gems/thin-1.3.1/bin/thin:6 from /usr/local/rvm/gems/ruby-1.8.7-p358/bin/thin:19:inload' from /usr/local/rvm/gems/ruby-1.8.7-p358/bin/thin:19

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)