减少独角兽应用服务器响应时间

时间:2013-08-28 05:51:02

标签: ruby-on-rails ubuntu nginx unicorn

我最近从乘客迁移到Unicorn运行我的电子商务应用程序,该应用程序基于Rails 3.2.13上的Ruby 2.0.0-p0。根据新的指标,独角兽应用服务器的平均响应时间非常长。如何调整独角兽将应用响应时间减少到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的附件

enter image description here

1 个答案:

答案 0 :(得分:2)

此问题与unicorn或nginx无关,而是与Rails应用程序的代码有关。

你正在做一些你需要修复的应用程序。

在开发模式中使用newrelic获取详细的跟踪可以帮助您找到性能问题。

相关问题