Skip to content
Chris edited this page Mar 9, 2012 · 8 revisions

Nginx and Thin

Install Nginx

sudo aptitude install nginx

Install Thin

Thin is a gem that was installed during the Ruby installation done previously.

Refer to Ruby from source installation instructions or Ruby installation using RVM.

Add upstream Thin servers to Nginx configuration

Stop nginx:

sudo service nginx stop

Edit the nginx conf file:

sudo nano /etc/nginx/nginx.conf

ensure the contents are similar to the following:

user www-data;
worker_processes  3;

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

events {
    worker_connections  2048;
    # multi_accept on;
}
worker_rlimit_nofile 2048;

http {
    include       /etc/nginx/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  5;
    tcp_nodelay        on;

    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

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

    server {
      listen       80;
      server_name  osprotect.example.com;

      root /home/rails-app-user/apps/osprotect/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;
      }

      location /private_files/ {
        internal;
        alias /home/rails-app-user/apps/osprotect/shared/reports/;
      }

    }

    server {
      listen 80;
      server_name example.com www.example.com;
      root /var/www;
    }

    # include /etc/nginx/conf.d/*.conf;
    # include /etc/nginx/sites-enabled/*;
}

ensure that the three Thin upstream servers are specified:

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

be sure to replace example.com with your domain name, and the rails-app-user with the appropriate user account, ensure the paths match your server set up.

Start nginx:

sudo service nginx start