我得到了当前安装了两个应用程序的服务器,我需要添加另一个,这是我的配置。
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;
}
独角兽
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 添加到项目中
部署.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;
}
}
独角兽日志
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 地址时出现SERVER NOT FOUND
错误,而其他应用程序运行正常。错误日志中没有显示任何内容。您能指出我的错误在哪里吗?
答案1
您的共享中似乎有拼写错误nginx.conf
。您尝试使用的域是,www.beta.whotracker.com
但服务器配置显示www.beta.hotracker.com
。
答案2
不是http错误代码。你用的是Chrome浏览器吗?可能是浏览器缓存问题。清除缓存后再试。
答案3
检查您的防火墙和 DNS。
这两个应用程序可以正常工作吗?曾经遇到过 nginx/unicorn 的问题最终发现这是由于防火墙配置错误所致。