Mac + unicorn + nginx + Rails
Unicorn
Install
gem install unicorn
設定
を作成
# ワーカーの数 worker_processes 2 # ソケット経由で通信する listen File.expand_path('tmp/sockets/unicorn.sock', ENV['RAILS_ROOT']) # ログ stderr_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT']) stdout_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT']) # ダウンタイムなくす preload_app true before_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! old_pid = "#{ server.config[:pid] }.oldbin" unless old_pid == server.pid begin # SIGTTOU だと worker_processes が多いときおかしい気がする Process.kill :QUIT, File.read(old_pid).to_i rescue Errno::ENOENT, Errno::ESRCH end end end after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end
nginx
Install
brew install nginx
nginxへPathを通す
/usr/local/sbin/
設定ファイル
/usr/local/etc/nginx/nginx.conf
変更した所だけ記述
upstream unicorn.rails_app{ #自分のRailsアプリへの場所 server unix:/Users/giwa/workspace/ror/depot/tmp/sockets/unicorn.sock fail_timeout=0; } server{ location / { alias /Users/giwa/workspace/ror/depot/public; index index.html index.htm index; try_files $uri/index.html $uri.html $uri @unicorn_rails_app; } location @unicorn_rails_app { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://unicorn.rails_app; } }
neginx -t でsyntaxが正しいかチェック
起動
nginx
これでlocalhost:8080 にアクセスし、自分のRailsアプリが見えれば成功
ref
http://aoyagikouhei.blog8.fc2.com/blog-entry-195.html
http://archive.aerial.st/archive/2011/06/16/nginx-unicorn-rails-on-mac
http://techracho.bpsinc.jp/piichan1031/2010_07_09/2075
http://www.mk-mode.com/octopress/2013/01/22/ruby-on-rails-nginx-unicorn/