CirclingMinds

October 5, 2007

Using nginx + mongrel + rails on Ubuntu

Filed under: advanced-rails — shovan @ 10:40 pm

STEP 1
Install Nginx. Get the source code from here. Before you compile make sure you have SSL libraries if you plan on using the SSL mode. To get the ssl libraries run:

sudo aptitude install openssl libssl-dev

To compile:

./configure --with-http_ssl_module
make
sudo make install

To start nginx run

sudo /usr/local/nginx/sbin/nginx

STEP 2
Edit

/usr/local/nginx/conf/nginx.conf</code>

file to setup configuration for your rails app and mongrel cluster. Here is a sample of nginx.conf file:

worker_processes  1; 
 
error_log  logs/error.log;
error_log  logs/error.log  notice; 
 
pid        logs/nginx.pid; 
 
events {
    worker_connections  1024;
} 
 
http
{
        include       conf/mime.types;
        default_type  application/octet-stream;
        server_names_hash_bucket_size 128; 
 
        sendfile        on;
        tcp_nopush     on; 
 
        keepalive_timeout  65;
        tcp_nodelay        on; 
 
        upstream mongrel_test {
            server 127.0.0.1:8000;
            server 127.0.0.1:8001;
            server 127.0.0.1:8002;
        } 
 
        gzip on;
        gzip_min_length  1100;
        gzip_buffers     4 8k;
        gzip_types       text/plain; 
 
        # ************************ server conf begins **********************
        server
        {
                listen       80;
             server_name  servername.com;
             root    /home/shovan/apps/oas/public; 
 
             access_log  off;
             rewrite_log on; 
 
            # this rewrites all the requests to the maintenance.html
            # page if it exists in the doc root. This is for capistrano's
            # disable web task
            if (-f $document_root/system/maintenance.html) {
              rewrite  ^(.*)/system/maintenance.html last;
              break;
            } 
 
                location ~ ^/$ {
                  if (-f /index.html){
                    rewrite (.*) /index.html last;
                  }
                   proxy_pass  http://mongrel_test;
                } 
 
                location / {
                  if (!-f $request_filename.html) {
                    proxy_pass  http://mongrel_test;
                  }
                  rewrite (.*) $1.html last;
                } 
 
                location ~ .html {
                                root    /home/shovan/apps/oas/public;
                } 
 
                   location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)$ {
                  root /home/shovan/apps/oas/public;
                } 
 
                location / {
                    proxy_pass  http://mongrel_test;
                    proxy_redirect     off;
                    proxy_set_header   Host             $host;
                    proxy_set_header   X-Real-IP        $remote_addr;
                    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                } 
 
        }
 
} # end of http

STEP 3

Set up your mongrel_cluster.yml file. Here is how it should look like:

 ---
cwd: /home/shovan/apps/oas
port: "8000"
environment: production
address: 127.0.0.1
pid_file: log/mongrel.pid
servers: 3

STEP 4
Now start the mongrel cluster using the following command:

 sudo mongrel_rails cluster::start

That should fire up the clusters. Restart ngnix and you should be able to see your rails app when you load up the url in the browser. If you get errors make sure you look at the logs to see whats going on. They can be really helpful.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress