Apache + Passenger で動かしていた Redmine を Nginx + Unicorn に移行したのでそのメモ。
設定した環境
Unicorn のインストール
Redmine ディレクトリのルートに Gemfile.local を作る。
# Gemfile.local
gem “unicorn”
bundler で Unicorn をインストール
$ bundle install
Unicorn の設定スクリプト
Redmine ディレクトリの config に unicorn.rb を下記のとおり作成する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| worker_processes 1
timeout 60
listen File.expand_path("tmp/sockets/unicorn.sock", ENV['RAILS_ROOT']) pid File.expand_path("tmp/pids/unicorn.pid", ENV['RAILS_ROOT'])
preload_app true
stderr_path File.expand_path("log/unicorn.stderr.log", ENV['RAILS_ROOT']) stdout_path File.expand_path("log/unicorn.stdout.log", ENV['RAILS_ROOT'])
GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true
before_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[:pid]}.oldbin" if old_pid != server.pid begin sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU Process.kill(sig, File.read(old_pid).to_i) rescue Errno::ENOENT, Errno::ESRCH end end
sleep 1 end
after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end
|
- 初回アクセス時にタイムアウトしないように 60 秒に増やす。
- 数人しかアクセスしないのでワーカーは 1 つにする。
Nginx の設定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| upstream redmine { server unix:/var/redmine/tmp/sockets/unicorn.sock; }
… location /redmine { root /var/redmine/public;
if ( -f $request_filename ) { break; }
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_pass http://redmine; } …
|
Unicorn 起動
Unicorn で Redmine を起動
$ bundle exec unicorn_rails -c config/unicorn.rb -D -E production –path /redmine
参考にした記事
tilfin
freelance software engineer