私は最近、Rails 3.2.13 上の Ruby 2.0.0-p0 に基づく電子商取引アプリケーションを実行するために、パッセンジャーから Unicorn に移行しました。newrelic メトリクスによると、ユニコーンであるアプリサーバーの平均応答時間は非常に長いです。アプリの応答時間を 500 ミリ秒未満に短縮するためにユニコーンを微調整する方法、現在 1200 ミリ秒以上が発生しています。ユニコーン アプリ サーバーの応答時間のスナップ ショットを添付し、unicorn.rb と nginx.conf も添付しました。Ruby 自体が 800 ミリ秒以上消費していることに気付くことができます。どうすればそれを減らすことができますか?AWS ubuntu ec2 インスタンスを実行しています。私は大きなインスタンスで実行しています。
require 'unicorn/oob_gc'
# this should probably be between CPU threads and CPU threads * 2
worker_processes 2
# this is your current deployed code symlink
root = "/path/to/app"
working_directory root
# don't use TCP to talk to Nginx
listen "/tmp/unicorn.sock"
# how long is it ok for your workers to hang
timeout 30
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn_stderr.log"
stdout_path "#{root}/log/unicorn_stdout.log"
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
GC.copy_on_write_friendly = true
check_client_connection false
before_exec do |server|
ENV['BUNDLE_GEMFILE'] = "#{root}/Gemfile"
end
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
end
after_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to sent QUIT'
end
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end
ここに私のnginx.confがあります
worker_processes 2;
user ubuntu ubuntu; # for systems with "nobody" as a group instead
# Feel free to change all paths to suite your needs here, of course
pid /etc/nginx/nginx.pid;
error_log /var/log/nginx/nginx.error.log;
events {
worker_connections 1024; # increase if you have lots of clients
accept_mutex off; # "on" if nginx worker_processes > 1
# use epoll; # enable for Linux 2.6+
# use kqueue; # enable for FreeBSD, OSX
}
http {
# nginx will find this file in the config directory set at nginx build time
include mime.types;
# fallback in case we can't determine a type
default_type application/octet-stream;
# click tracking!
# access_log /var/log/nginx/nginx.access.log combined;
# you generally want to serve static files with nginx since neither
# Unicorn nor Rainbows! is optimized for it at the moment
sendfile on;
tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
tcp_nodelay off; # on may be better for some Comet/long-poll stuff
client_max_body_size 2M;
client_body_buffer_size 64k;
#file_cache
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors off;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
# gzip_types text/plain text/html text/xml text/css
# text/comma-separated-values
# text/javascript application/x-javascript
# application/atom+xml;
# this can be any application server, not just Unicorn/Rainbows!
upstream app_server {
# for UNIX domain socket setups:
#server unix:/path/to/.unicorn.sock fail_timeout=0;
;
# for TCP setups, point these to your backend servers
# server 192.168.0.9:8080 fail_timeout=0;
}
server {
keepalive_timeout 5;
# path for static files
root path/to/app;
try_files $uri/index.html $uri.html $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://app_server;
}
# Rails error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root path/to/app;
}
}
}
newrelicの添付ファイルはこちら