web-dev-qa-db-ja.com

Rails + Nginx + Unicorn複数のアプリ

現在2つのアプリがインストールされているサーバーを取得し、別のアプリを追加する必要があります。これが私の構成です。

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

また、プロジェクトにcapistranoを追加しました

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

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

デプロイは正常に実行されますが、beta.whotracker.comまたはip-addressにアクセスしようとすると、SERVER NOT FOUNDエラーが発生しますが、他のアプリは正常に機能します。エラーログには何も表示されません。私のせいはどこにあるのか教えていただけますか?

2

共有にタイプミスがあるようですnginx.conf。使用しようとしているドメインはwww.beta.whotracker.comしかし、サーバーconfはwww.beta.hotracker.com

1
platforms

Httpエラーコードではありません。 Chromeブラウザを使用していますか?ブラウザのキャッシュの問題である可能性があります。キャッシュをクリアしてから試してください。

0
Raj

ファイアウォールとDNSを確認してください。

2つのアプリは機能していますか?私 nginx/Unicornで一度問題がありました ファイアウォールの設定が間違っていることが判明しました。

0
New Alexandria