Rails + Nginx + Unicorn vários aplicativos

2

Eu recebo o servidor onde atualmente estão instalados dois aplicativos e preciso adicionar outro, aqui estão minhas configurações.

nginx.conf

user www-data www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    ##
    # Logging Settings
    ##
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    ##
    # Disable unknown domains
    ##
    server {
        listen       80  default;
        server_name  _;
        return       444;
    }
    ##
    # Virtual Host Configs
    ##
    include /home/ruby/apps/*/shared/config/nginx.conf;
}

unicorn.rb

deploy_to  = "/home/ruby/apps/staging.domain.com"
rails_root = "#{deploy_to}/current"
pid_file   = "#{deploy_to}/shared/pids/unicorn.pid"
socket_file= "#{deploy_to}/shared/sockets/.sock"
log_file   = "#{rails_root}/log/unicorn.log"
err_log    = "#{rails_root}/log/unicorn_error.log"
old_pid    = pid_file + '.oldbin'

timeout 30
worker_processes 10 # Здесь тоже в зависимости от нагрузки, погодных условий и текущей фазы луны
listen socket_file, :backlog => 1024
pid pid_file
stderr_path err_log
stdout_path log_file

preload_app true 

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

before_exec do |server|
  ENV["BUNDLE_GEMFILE"] = "#{rails_root}/Gemfile"
end

before_fork do |server, worker|
  defined?(ActiveRecord::Base) and
  ActiveRecord::Base.connection.disconnect!

  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
    end
  end
end

after_fork do |server, worker|
  defined?(ActiveRecord::Base) and
  ActiveRecord::Base.establish_connection
end

Também adicionei capistrano ao projeto

deploy.rb

# encoding: utf-8

require 'capistrano/ext/multistage'
require 'rvm/capistrano'
require 'bundler/capistrano'

set :stages,              %w(staging production)
set :default_stage,       "staging"

default_run_options[:pty] = true
ssh_options[:paranoid]    = false
ssh_options[:forward_agent] = true


set :scm, "git"

set :user,      "ruby"
set :runner,    "ruby"
set :use_sudo,  false

set :deploy_via,            :remote_cache

set :rvm_ruby_string, '1.9.2'

# Create uploads directory and link
task :configure, :roles => :app do
  run "cp #{shared_path}/config/database.yml #{release_path}/config/database.yml"
  # run "ln -s #{shared_path}/db/sphinx #{release_path}/db/sphinx"
  # run "ln -s #{shared_path}/config/unicorn.rb #{release_path}/config/unicorn.rb"
end

namespace :deploy do
  task :restart do
    run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -s USR2 'cat #{unicorn_pid}'; else cd #{deploy_to}/current && bundle exec unicorn_rails -c #{unicorn_conf} -E #{rails_env} -D; fi"
  end
  task :start do
    run "cd #{deploy_to}/current && bundle exec unicorn_rails -c #{unicorn_conf} -E #{rails_env} -D"
  end
  task :stop do
    run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -QUIT 'cat #{unicorn_pid}'; fi"
  end
end


before 'deploy:finalize_update', 'configure'
after "deploy:update", "deploy:migrate", "deploy:cleanup"
require './config/boot'

nginx.conf no caminho compartilhado do aplicativo

upstream staging_whotracker  {
    server   unix:/home/ruby/apps/staging.whotracker.com/shared/sockets/.sock;
}

server {
    listen 209.105.242.45;
    server_name beta.whotracker.com;
    rewrite ^/(.*) http://www.beta.whotracker.com/$1 permanent;
}

server {
      listen 209.105.242.45;
      server_name www.beta.hotracker.com;
      root /home/ruby/apps/staging.whotracker.com/current/public;

      location ~ ^/sitemaps/ {
    root /home/ruby/apps/staging.whotracker.com/current/system;

    if (!-f $request_filename) {
      break;
    }

    if (-f $request_filename) {
      expires -1;
      break;
    }
      }

      # cache static files :P
      location ~ ^/(images|javascripts|stylesheets)/ {
        root /home/ruby/apps/staging.whotracker.com/current/public;

        if ($query_string ~* "^[0-9a-zA-Z]{40}$") {
          expires max;
          break;
        }

    if (!-f $request_filename) {
      break;
    }
      }

      if ( -f /home/ruby/apps/staging.whotracker.com/shared/offline ) {
        return 503;
      }

      location /blog {
    index index.php index.html index.htm;
    try_files $uri $uri/ /blog/index.php?q=$uri;
      }

      location ~ \.php$ {
    try_files   $uri =404;
    include     /etc/nginx/fastcgi_params;
        fastcgi_pass    unix:/var/run/php-fastcgi/php-fastcgi.socket;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }

      location / {
    proxy_set_header      HTTP_REFERER      $http_referer;
        proxy_set_header          X-Real-IP         $remote_addr;
        proxy_set_header          X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header          Host              $http_host;
        proxy_redirect            off;
        proxy_max_temp_file_size  0;

        # If the file exists as a static file serve it directly without
        # running all the other rewite tests on it
        if (-f $request_filename) {
          break;
        }

        if (!-f $request_filename) {
          proxy_pass        http://staging_whotracker;
          break;
        }
      }

      error_page 502 =503 @maintenance;
      error_page 500 504 /500.html;

      error_page 503 @maintenance;
      location @maintenance {
        rewrite ^(.*)$ /503.html break;
      }     
}

unicorn.log

executing ["/home/ruby/apps/staging.whotracker.com/shared/bundle/ruby/1.9.1/bin/unicorn_rails", "-c", "/home/ruby/apps/staging.whotracker.com/current/config/unicorn.rb", "-E", "staging", "-D", {5=>#<Kgio::UNIXServer:/home/ruby/apps/staging.whotracker.com/shared/sockets/.sock>}] (in /home/ruby/apps/staging.whotracker.com/releases/20120517114413)
I, [2012-05-17T06:43:48.111717 #14636]  INFO -- : inherited addr=/home/ruby/apps/staging.whotracker.com/shared/sockets/.sock fd=5
I, [2012-05-17T06:43:48.111938 #14636]  INFO -- : Refreshing Gem list
worker=0 ready
...
master process ready
...
reaped #<Process::Status: pid 2590 exit 0> worker=6
...
master complete

A implantação é bem-sucedida, mas quando tento acessar o beta.whotracker.com ou o endereço IP, recebo SERVER NOT FOUND error, enquanto outros aplicativos funcionam muito bem. Nada aparece nos registros de erros. Você pode por favor me apontar onde está minha culpa?

    
por Mikhail Nikalyukin 17.05.2012 / 13:24

3 respostas

1

Parece que você tem um erro de digitação no seu nginx.conf compartilhado. O domínio que você está tentando usar é www.beta.whotracker.com , mas o servidor conf diz www.beta.hotracker.com .

    
por 11.11.2012 / 17:26
0

Não é o código de erro http. Você está usando o navegador Chrome? Pode ser problema de cache do navegador. Limpe o cache e tente.

    
por 17.05.2012 / 13:36
0

Verifique seu firewall e seu DNS.

Os dois aplicativos estão funcionando? Eu tinha um problema uma vez com o nginx / unicorn que acabou sendo um firewall mal configurado.

    
por 09.09.2012 / 03:42