Skip to content

Instantly share code, notes, and snippets.

@STAR-ZERO
Last active December 14, 2016 08:55
Show Gist options
  • Save STAR-ZERO/4203420 to your computer and use it in GitHub Desktop.
Save STAR-ZERO/4203420 to your computer and use it in GitHub Desktop.
nginx + unicorn + capistrano設定

nginx + unicorn + capistrano設定メモ

Nginx

起動

$ nginx

停止

$ nginx -s stop

設定ファイルのチェック

$ nginx -t

Unicorn

起動

$ bundle exec unicorn -c config/unicorn.rb -E production -D

シグナル

QUIT

リクエスト処理が終了するのを待ってからmasterまたはworkerのプロセスを終了。安全に終了できる。

$ kill -QUIT <masterまたはworkerのプロセス番号>
INT/TERM

masterまたはworkerをすぐ終了。

$ kill -INT <masterまたはworkerのプロセス番号>
または
$ kill -TERM <masterまたはworkerのプロセス番号>
HUP

設定ファイルのリロード
preloadがfalseの場合はアプリ再読み込み、trueの場合は再読み込みしない

$ kill -HUP <masterのプロセス番号>
USR1

masterまたはworkerが書き込んでいるlogを再OPEN

$ kill -USR1 <masterまたはworkerのプロセス番号>
USR2

再起動

$ kill -USR2 <masterのプロセス番号>
TTIN

workerプロセスを増やす

$ kill -TTIN <masterのプロセス番号>
TTOU

workerプロセスを減らす

$ kill -TTOU <masterのプロセス番号>

Capistrano

capistrano-unicornを使用

https://github.com/sosedoff/capistrano-unicorn

Gemfile

rubygemsのはversionが古いのでGithubから直接

group :development do
  gem 'capistrano', :require => false
  gem 'capistrano_colors', :require => false
  gem 'capistrano-unicorn', :require => false, :git => 'git://github.com/sosedoff/capistrano-unicorn.git'
end

deploy.rb

set :unicorn_pid, "/tmp/unicorn_#{application}.pid"
# preload_appがtrueの場合
after "deploy:restart", "unicorn:restart"
# preload_appがfalseの場合
after 'deploy:restart', 'unicorn:reload'
require 'capistrano-unicorn'
# http://wiki.nginx.org/Configuration
# http://labs.unoh.net/2009/08/tips_for_nginx.html
# http://techracho.jp/?p=2075
# http://oblog.objectclub.jp/nginxweb-5
# 実行ユーザー
user nginx;
# workerプロセス数
# CPUのコア数以下に設定
worker_processes 2;
# PIDパス
pid /tmp/nginx.pid;
# 接続に関する設定
# http://wiki.nginx.org/EventsModule
events {
# 1workerが扱えるコネクション数
worker_connections 1024;
}
http {
# TCPまたはUnix Socketで接続
# 複数記述した場合はラウンドロビン方式でリクエストを捌く
# http://oblog.objectclub.jp/nginxweb-5
upstream backend {
server unix:/tmp/unicorn_app.sock;
}
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name locahost;
# サーバートークンを出力しない
server_tokens off;
# gzip圧縮を有効
# http://server-setting.info/centos/apache-nginx-6-gzip-use.html
gzip on;
# ログ出力先
access_log /var/log/nginx/app.access.log;
error_log /var/log/nginx/app.error.log;
# rootディレクトリ
root /var/www/app/current/public;
# 静的ファイル
location ~* \.(html|ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
# キャッシュ期間(この場合は30日)
# 単位 http://wiki.nginx.org/ConfigurationSyntax
expires 30d;
break;
}
location / {
# バックエンドのアプリ側にクライアントIPアドレスを渡す
# http://d.hatena.ne.jp/sirocco634/20110827
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# バックエンドにホスト名を渡す
# http://blog.fujimuradaisuke.com/post/12622482560/nginx
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $host;
# upstreamに設定したバックエンド
# http://wiki.nginx.org/NginxHttpProxyModule#proxy_pass
proxy_pass http://backend;
}
}
}
# -*- encoding: utf-8 -*-
application = 'app'
# worker数
# コア数よりちょっと多めでも良い。メモリと相談。
# Unicornは1workerで1リクエストを裁く
#
# TTINシグナルでワーカープロセスを増やせる
# TTOUシグナルでワーカープロセスを減らせる
# 再起動の必要はない
#
# 参考:http://blog.takeyu-web.com/entry/20120416/1334539217
worker_processes 3
# Unix socketのパス
listen "/tmp/unicorn_#{application}.sock"
# masterプロセスPIDのパス
pid "/tmp/unicorn_#{application}.pid"
rails_env = ENV['RAILS_ENV'] || 'production'
# ログのパス
stderr_path 'log/unicorn.log'
stdout_path 'log/unicorn.log'
# currentのパス
current_path = "/path/to/current"
# Ruby Enterprise Editionの場合の設定
# たぶんいらない
#if GC.respond_to?(:copy_on_write_friendly=)
# GC.copy_on_write_friendly = true
#end
# 色々複雑!!
# master側でアプリをロードしてworkerで使いまわす
# ダウンタイムがなくなる
# before_forkにmasterプロセスの制御必要になる
#
# masterがUSR2シグナルを受けるとmaster自身がforkされ新しくmasterが作られる
# 古いmasterプロセスはunicorn master(old)というプロセス名になり、古いPIDはunicorn_#{application}.pid.oldbinに変更される
#
# trueの場合はmasterがアプリを読み込みworkerで共有する
# falseの場合はmasterはアプリ読み込まずworker毎に読み込む
#
# http://d.hatena.ne.jp/motsat/20120314/1331736445
# http://techracho.jp/?p=2075
# http://techracho.jp/?p=2208
# http://kray.jp/blog/troubleshooting-rails-unicorn/
# http://nekogata.hatenablog.com/entry/2012/07/21/014714
# https://github.com/blog/517-unicorn
# http://codelevy.com/2010/02/09/getting-started-with-unicorn.html
preload_app true
# Bundler::GemfileNotFoundになって再起動しない問題がある
# http://ikm.hatenablog.jp/entry/2013/06/27/164942
before_exec do |server|
ENV['BUNDLE_GEMFILE'] = "#{current_path}/Gemfile"
end
# workerをforkする前の処理
before_fork do |server, worker|
# http://techracho.bpsinc.jp/baba/2012_08_29/6001
defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect!
# 古いPIDファイル取得
old_pid = "#{server.config[:pid]}.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
# 古いPIDがある場合
begin
# 古いmasterプロセスを終了させる
Process.kill('QUIT', File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
end
# workerをforkしたあとの処理
after_fork do |server, worker|
# http://techracho.bpsinc.jp/baba/2012_08_29/6001
defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection
# dalli使ってる場合(新しいバージョンだといらないっぽい)
# if defined?(ActiveSupport::Cache::DalliStore) && Rails.cache.is_a?(ActiveSupport::Cache::DalliStore)
# Rails.cache.reset
# ObjectSpace.each_object(ActionDispatch::Session::DalliStore) { |obj| obj.reset }
# end
end
@broccoli1002
Copy link

自分みたいな糞虫ではあってるかわからん!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment