読者です 読者をやめる 読者になる 読者になる

Nginx + Unicorn + Sinatra 構成の設定メモ

以前 Nginx + Unicorn + Sinatra 構成のアプリを作成したが、その時に行った設定をメモしておく。

サーバのディレクトリ構成

$ tree /usr/local/app
/usr/local/app
├── config.ru
├── log
│   ├── unicorn.stderr.log
│   ├── unicorn.stdout.log
│   └── crawler.log
├── myapp.rb
├── public
│   ├── index.html
│   ├── main.min.js
│   └── underscore-min.js
├── tmp
│   ├── pids
│   │   └── unicorn.pid
│   └── sockets
│       └── unicorn.sock
├── unicorn.rb
└── crawler.rb

Sinatra

/usr/local/app/myapp.rb

# coding: utf-8
require "sinatra/base"
require 'sinatra/reloader' if development?

class MyApp < Sinatra::Base

  get '/' do
    File.read(File.join(settings.public_folder, 'index.html'))
  end

  not_found do
    '404 not found'
  end

end

/usr/local/app/config.ru

require "sinatra"

require File.expand_path '../myapp.rb', __FILE__

run MyApp

Unicorn

/usr/local/app/unicorn.rb

@dir = "/usr/local/app/"

worker_processes 2
working_directory @dir

timeout 30

listen "#{@dir}tmp/sockets/unicorn.sock", :backlog => 64

pid "#{@dir}tmp/pids/unicorn.pid"

stderr_path "#{@dir}log/unicorn.stderr.log"
stdout_path "#{@dir}log/unicorn.stdout.log"

unicorn の起動

unicorn -c /usr/local/app/unicorn.rb -E development -D

-cは設定ファイル、-EはRackの環境変数-Dはデーモン起動。

unicorn の停止

cat /usr/local/app/tmp/pids/unicorn.pid | xargs kill -QUIT

Nginx

/etc/nginx/nginx.conf

worker_processes  1;

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

events {
  worker_connections  1024;
  accept_mutex off;
}

http {
  include       /etc/nginx/mime.types;

  default_type application/octet-stream;
  access_log /tmp/nginx.access.log combined;

  sendfile        on;
  tcp_nopush     on;

  keepalive_timeout  5;
  tcp_nodelay        on;

  gzip  on;
  gzip_vary on;
  gzip_min_length 500;

  gzip_disable "MSIE [1-6]\.(?!.*SV1)";
  gzip_types text/plain text/xml text/css
     text/comma-separated-values
     text/javascript application/x-javascript
     application/atom+xml image/x-icon;

  upstream unicorn_server {
    server unix:/usr/local/app/tmp/sockets/unicorn.sock
        fail_timeout=0;
  }

  server {
    server_name foobar.com;
    root /usr/local/app/public;
    listen 80;
    client_max_body_size 4G;
    keepalive_timeout 5;

    location / {
      try_files $uri @app;
    }

    location @app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://unicorn_server;
    }
  }

  server {
    server_name  dev.foobar.com;

    location / {
      root   /usr/local/dev/public;
      index  index.html index.htm;
    }
    location /gitbucket {
      proxy_pass http://localhost:8080/gitbucket;
    }
    location /jenkins {
      proxy_pass http://localhost:8080/jenkins;
    }
  }

}

crontab

0,15,30,45 * * * * (cd /usr/local/app && /home/ubuntu/.rbenv/shims/ruby crawler.rb >> /usr/local/app/log/crawler.log 2>&1)

参考:

Sinatra Recipes - Deployment - Nginx Proxied To Unicorn
http://recipes.sinatrarb.com/p/deployment/nginx_proxied_to_unicorn