LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Blogs > omgiamlinux
User Name
Password

Notices

Rate this Entry

Day 6.5 - Configuring Nginx to Serve Rails Apps

Posted 10-27-2011 at 04:54 PM by omgiamlinux
Updated 10-27-2011 at 06:31 PM by omgiamlinux

This post is a part of a mini-series!
Day 6
1) Installing link
2) Test a Rails App! link
3) Configuring Webserver (You Are Here)
4) Configuring MySql link


ref: http://wiki.rubyonrails.org/deployment/nginx-thin

Ok, let's assume that you've setup nginx on your computer, and installed rails, and even have PHP configured maybe. Now you want to work with rails via nginx. Well, that means you need to install 'thin' to be the ruby server. We'll do that in this blog post.

Prerequisites/assumptions:
1) nginx installed
2) ruby, gem, and rails are installed
3) You have a debian based system (I hear ubuntu is similar enough)


:: Install Thin
Thin is a gem, so it's easy to download the latest version onto your system.
Code:
$  rvm 1.9.2     ~~ thin is ok to install in 1.9.2, but webBrick won't work...
$  gem install thin
$  thin install

Now have thin autoboot at startup:
Code:
$  /usr/sbin/update-rc.d -f thin defaults

Now create a configuration script that will... tell thin how many processes to spawn, where your app is, etc.

Here's the "key: that I referenced:
Code:
>> KEY >>  sudo thin config -C /etc/thin/<config-name>.yml -c <rails-app-root-path> --servers <number-of-threads> -e <environment>
Here's my own actual implimentation:
Code:
$  thin config -C /etc/thin/myapp.yml -c /home/kentos/dev/rails/mt --servers 5 --socket /tmp/thin.myapp.sock -e development
That command will create a new yml file visible from [inline]cat /etc/thin/myapp.yml[/inline].

Then start those thin processes which handle the guts of rails:
Code:
$  /etc/init.d/thin start





:: Configure nginx (yes, again...)

To your nginx configuration, once again, you must make changes, these:
  • Add information about your thin processes
  • change the root location to your app
  • add a bunch of declairations
  • add a weird chuck of if statements
If you have php setup already, you can probably get away with just leaving that in place.


(this content should be involved in your /usr/local/nginx/conf/nginx.conf file)
Code:

user nginx;
worker_processes 5;

error_log /var/log/nginx.error.log;
pid /var/run/nginx.pid;

events {
  worker_connections 1024;
}

http {
  include mime.types;
  default_type application/octet-stream;

  log_format main '$remote_addr - $remote_user [$time_local] $request '
                      '"$status" $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

  access_log /var/log/nginx.access.log main;

  sendfile on;

  keepalive_timeout 65;

  upstream thin_cluster {                        #   NEW ADDITION!
    server unix:/tmp/thin.myapp.0.sock;          #   The names of these depends on what
    server unix:/tmp/thin.myapp.1.sock;          #   You specify after '--socket'
    server unix:/tmp/thin.myapp.2.sock;          #   When you run 'thin config'
    server unix:/tmp/thin.myapp.3.sock;          #   And the output you get when you run 
    server unix:/tmp/thin.myapp.4.sock;          #   '/etc/init.d/thin start'
  }

  server {
    listen 80;
    server_name www.myserver.com;

    root /home/kentos/dev/rails/mt/public;                            #   CHANGE LOCATION!

    location / {
      proxy_set_header X-Real-IP $remote_addr;                         #   NEW ADDITIONS!
      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) {                            #   NEW ADDITIONS!
        rewrite (.*) $1/index.html break;
      }
      if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
      }
      if (!-f $request_filename) {
        proxy_pass http://thin_cluster;
        break;
      }
      
      #index       index...                                 #   COMMENT THE INDEX LINE OUT I GUESS
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
      root html;
    }
  }
}

Now my only problem is, how do I get those thin processes to auto start at boot? To be concluded...

Ok, once you've got that setup, you should be able to make a new rails project in [inline]/home/kentos/dev/rails/mt[/inline] (it's based on the 'root' directive you give in your nginx.conf

Code:
$  cd /home/kentos/dev/rails
$  rails new mt
$  /etc/init.d/thin restart
Then navigate to [inline]http://localhost[/inline] and you should see your newly created and functional rails app. If not, you went wrong somewhere, plz share a comment about that.


Clarifications:
I think it's important to point out how all of these technologies are working together in order to "just work."

To see a rails app in a web browser, your server needs, at bare minimum:
1) A web server (duh, right?)
2) Ruby (duh, right?)
3) A ruby server (thin is like our... PHP for ruby. Get that? I'm not totally sure, but that's how I think about thin, it allows the magic of rails to work.)
4) Gem (duh) and the gem named rails (duh)

OK, so when you've got all that going, and you navigate to localhost and see a beautiful starter page, you might wonder the following questions:

A. How does it know what rails project to show?
- nginx.conf
- HTTP Host header (eg. example.com vs example2.com can direct elsewhere, even if same IP)

B. How does it know what ruby server to use?
- nginx.conf

C. How does it know what ruby version to use?
-Thin (whatever ruby environment was active at the time of [inline]thin install[/inline]) (untested, brb)

C. How does it know what rails version to use
-Gemfile

D. How does it know what database system to use?
-Gemfile
-database.yml

So you can see that 3 different technologies are configured, ever so delicately, together to bring your end user a slick rails experience.
Views 251 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 06:58 AM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration