我最近从乘客迁移到 Unicorn 以运行基于 Rails 3.2.13 上的 Ruby 2.0.0-p0 的电子商务应用程序。根据 newrelic 指标,独角兽应用服务器的平均响应时间相当长。如何调整独角兽以将应用程序响应时间减少到 500 毫秒以下,目前我正在经历超过 1200 毫秒。我附上了 unicorn 应用程序服务器响应时间的快照,还附上了 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;
}
}
}
这是新遗物的附件