Rails + Nginx + Unicorn multiple apps
- by Mikhail Nikalyukin
I get the server where is currently installed two apps and i need to add another one, here is my configs.
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
Also im added capistrano to the project
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 in app shared path
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
Deploy goes successfully, but when i try to access beta.whotracker.com or ip-address i get SERVER NOT FOUND error, while others app works great. Nothing shows up in error logs. Can you please point me where is my fault?