1

I have a Ubuntu server from Azure services which I want to run all of my ruby on rails applications. For that I'm using NGINX with UNICORN and RAINBOWS!

I set ruby on rails project for production and try to run it. I can see the requests arriving but it's taking too long for the answers. Sometimes I even get a time out error.

I really need help on how to properly setup my server. Is there any tutorials, documentation or even tips from you guys (y'all always saves my ass) that could help me making this server work?

This is my ngnx config file

upstream nutrimais {
  server unix:/tmp/nutrimais.sock;
}

server {
  listen 80;
  server_name dev.mydomain.com.br;
  server_name www.mydomain.com.br;
  server_name mydomain.com.br;

  location / {
    autoindex on;
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://nutrimais; # match the name of upstream directive
    # time out settings
    proxy_connect_timeout 159s;
    proxy_send_timeout   600;
    proxy_read_timeout   600;
    proxy_buffer_size    64k;
    proxy_buffers     16 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    proxy_pass_header Set-Cookie;
    proxy_redirect     off;
    proxy_hide_header  Vary;
    proxy_set_header   Accept-Encoding '';
    proxy_ignore_headers Cache-Control Expires;
    proxy_set_header   Referer $http_referer;
    proxy_set_header   Host   $host;
    proxy_set_header   Cookie $http_cookie;
    proxy_set_header   X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

and this is my Rainbows! config

Rainbows! do
  use :ThreadSpawn # concurrency model to use
  worker_connections 400
  keepalive_timeout 0 # zero disables keepalives entirely
  client_max_body_size 5*1024*1024 # 5 megabytes
  keepalive_requests 666 # default:100
  client_header_buffer_size 2 * 1024 # 2 kilobytes
end

@dir = File.expand_path(File.dirname(__FILE__)) + "/.."

worker_processes 1
working_directory @dir

timeout 30

listen File.join('/tmp/nutrimais.sock')

preload_app true# if ENV['RAILS_ENV'] != 'development'

GC.respond_to?(:copy_on_write_friendly=) and
  GC.copy_on_write_friendly = true

check_client_connection false

before_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

This is the log of a request that took too long

I, [2016-01-20T17:25:17.297122 #9]  INFO -- : Started GET "/menus" for 127.0.0.1 at 2016-01-20 17:25:17 +0000
I, [2016-01-20T17:25:17.580380 #9]  INFO -- : Processing by MenusController#index as HTML
D, [2016-01-20T17:25:17.904933 #9] DEBUG -- :   [1m[36mMenu Load (322.3ms)[0m  [1mSELECT `menus`.* FROM `menus`  ORDER BY created_at DESC[0m
I, [2016-01-20T17:25:20.006674 #9]  INFO -- : Started GET "/menus" for 127.0.0.1 at 2016-01-20 17:25:20 +0000
I, [2016-01-20T17:25:20.102991 #9]  INFO -- : Started GET "/menus" for 127.0.0.1 at 2016-01-20 17:25:20 +0000
I, [2016-01-20T17:25:20.411379 #9]  INFO -- : Processing by MenusController#index as HTML
I, [2016-01-20T17:25:22.172944 #9]  INFO -- : Processing by MenusController#index as HTML
D, [2016-01-20T17:25:22.498899 #9] DEBUG -- :   [1m[35mMenu Load (324.0ms)[0m  SELECT `menus`.* FROM `menus`  ORDER BY created_at DESC
D, [2016-01-20T17:25:23.059756 #9] DEBUG -- :   [1m[36mMenu Load (884.2ms)[0m  [1mSELECT `menus`.* FROM `menus`  ORDER BY created_at DESC[0m
I, [2016-01-20T17:25:45.530393 #9]  INFO -- :   Rendered menus/index.html.erb within layouts/application (27948.1ms)
I, [2016-01-20T17:25:45.534684 #9]  INFO -- : Completed 200 OK in 27954ms (Views: 27630.4ms | ActiveRecord: 322.3ms)
I, [2016-01-20T17:25:52.230905 #9]  INFO -- :   Rendered menus/index.html.erb within layouts/application (30055.5ms)
I, [2016-01-20T17:25:52.234927 #9]  INFO -- : Completed 200 OK in 30062ms (Views: 29175.4ms | ActiveRecord: 884.2ms)
I, [2016-01-20T17:26:00.640179 #9]  INFO -- :   Rendered menus/index.html.erb within layouts/application (38465.6ms)
I, [2016-01-20T17:26:00.643772 #9]  INFO -- : Completed 200 OK in 40232ms (Views: 38145.8ms | ActiveRecord: 324.0ms)
I, [2016-01-20T17:31:51.678765 #9]  INFO -- : Started GET "/" for 127.0.0.1 at 2016-01-20 17:31:51 +0000
I, [2016-01-20T17:31:52.827556 #9]  INFO -- : Started GET "/" for 127.0.0.1 at 2016-01-20 17:31:52 +0000
I, [2016-01-20T17:31:53.144345 #9]  INFO -- : Processing by PostsController#index as HTML
I, [2016-01-20T17:31:54.117563 #9]  INFO -- : Processing by PostsController#index as HTML
D, [2016-01-20T17:31:54.836516 #9] DEBUG -- :   [1m[36mPost Load (702.9ms)[0m  [1mSELECT `posts`.* FROM `posts`  ORDER BY created_at DESC[0m
D, [2016-01-20T17:31:54.871071 #9] DEBUG -- :   [1m[35mPost Load (735.0ms)[0m  SELECT `posts`.* FROM `posts`  ORDER BY created_at DESC
I, [2016-01-20T17:31:59.274495 #9]  INFO -- :   Rendered posts/index.html.erb within layouts/application (5145.7ms)
I, [2016-01-20T17:31:59.281284 #9]  INFO -- : Completed 200 OK in 6137ms (Views: 3911.2ms | ActiveRecord: 1251.6ms)
I, [2016-01-20T17:31:59.363025 #9]  INFO -- :   Rendered posts/index.html.erb within layouts/application (5229.2ms)
I, [2016-01-20T17:31:59.368484 #9]  INFO -- : Completed 200 OK in 5248ms (Views: 3951.1ms | ActiveRecord: 1294.4ms)

as you can see it too 5 seconds just to render a page.

Fred Novack
  • 727
  • 9
  • 27

0 Answers0