Install

First and of course, install them:

sudo apt-get update
sudo apt-get install nginx

gem install thin

Configure Thin server

In your rails app, create a thin.yml setting for thin server:

thin.yml:

chdir: /home/user/work/rails/application/releases/20130508014904
environment: production
address: 0.0.0.0
port: 5000
timeout: 30
log: log/thin.log
pid: tmp/pids/thin.pid
max_conns: 1024
max_persistent_conns: 100
require: []
wait: 30
servers: 3
daemonize: true 

usage:
thin server -C thin.yml
or if you don't want the config bounded to a specific dir, just don't specify "chdir", then its usage:
cd {some_path}; thin server -C thin.yml

It will create 3 thin servers in port 5000, 5001, 5002

Connect Nginx with Thin

Now let's set up nginx, insert these code into http{...} section

upstream thin_cluster {
            server unix:/tmp/thin.0.sock;
            server unix:/tmp/thin.1.sock;
            server unix:/tmp/thin.2.sock;
  }

  client_max_body_size 20m;

  server {
    listen       80;
    server_name  application;

    root /home/user/work/rails/application/releases/20130508014904/public;

    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 off;

      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://thin_cluster;
        break;
      }
    }

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

in /etc/nginx/sites-available create a configure file, or modify default. If you choose to create a configure file, like myApp.com, remember to add it in /etc/nginx/sites-enable as well, make a symbolic link to sites-available.

configure file:

upstream thinservers {
        server 127.0.0.1:5000;
        server 127.0.0.1:5001;
        server 127.0.0.1:5002;
    }


server {
            listen   80;
            server_name put_your_domain_or_ip_here;

            access_log /home/user/work/rails/application/shared/log/access.log;
            error_log /home/user/work/rails/application/shared/logerror.log;

            root   /path/to/rails/application/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 off;

                          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://thinservers;
                                           break;
                          }
            }

}

Worth Notice

  1. Remember to run assets pipeline first in your rails app so that nginx could serve the static files (thin is not going to serve static files by default). rake assets:precompile RAILS_ENV=production

  2. Create dir "log" and "system" in rails_deployment_dir_path/shared/

Problems might face

If you got "413 Request Entity Too Large", that's nginx's setting stuff, in nginx.conf, insert this line of code into http{...}.

client_max_body_size 20m;

Ref

Ubuntu Hardy - Nginx, rails and thin

Useful command:
sudo service nginx start/stop/restart
sudo service apache2 start/stop/restart
rails db:migrate RAILS_ENV=production
thin start -s 3 -p 5000 -e production
tail -f log/production.log

Comments

comments powered by Disqus