Its my first time setting up nginx and unicorn.
My capistrano deployment went through and everything succeeded.
here is my unicorn.rb
#app_dir = File.expand_path('../../', __FILE__)
#shared_dir = File.expand_path('../../../shared/', __FILE__)
preload_app true
worker_processes 4
timeout 30
working_directory "home/deploy/appname"
shared_dir = "home/deploy/appname/shared"
# Set up socket location
# by default unicorn listens on 8080
listen "#{shared_dir}/tmp/sockets/unicorn.sock", :backlog => 64
# Logging
stderr_path "#{shared_dir}/log/unicorn.stderr.log"
stdout_path "#{shared_dir}/log/unicorn.stdout.log"
# Set master PID location
pid "#{shared_dir}/tmp/pids/unicorn.pid"
#must set preload app true to use before/after fork
before_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
#before forking, this is suppose to kill the master process that belongs to the oldbin
#enables 0 downtime to deploy
old_pid = "#{shared_dir}/tmp/pids/unicorn.pid.oldbin"
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
# before_exec do |server|
# ENV['BUNDLE_GEMFILE'] = "#{app_dir}/Gemfile"
# end
my nginx conf at /etc/nginx/nginx.conf
user www-data;
worker_processes 4;
pid /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;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
my default file at /etc/nginx/sites-enabled/default
upstream app_server {
#path to unicorn sock file, as defined previously
server unix:/home/deploy/appname/shared/tmp/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 80;
root /home/deploy/appname;
try_files $uri/index.html $uri @app;
#click tracking
access_log /var/log/nginx/appname_access.log combined;
error_log /var/log/nginx/appname_error.log;
location @app {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
when I do this
deploy@localhost:~$ sudo nginx -s reload
nginx: [emerg] host not found in upstream "app" in /etc/nginx/sites-enabled/default:46
When I head into
/shared/tmp/sockets
I don't have a file in there. I don't think I should create it manually. I am using capistrano 3. Am I suppose to generate this file?
I am using
require 'capistrano3/unicorn' #in capfile
in deploy.rb
set :linked_files, %w{config/database.yml config/secrets.yml}
set :linked_dirs, %w{tmp/pids tmp/cache tmp/sockets log bin vendor/bundle public/system}
#just pointing to our unicorn.rb
set :unicorn_config_path, "config/unicorn.rb"
#capistrano tasks and processes
after "deploy", "deploy:cleanup"
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'unicorn:restart'
end
end
after :finishing, "deploy:cleanup"
end
I put my cap file here because I notice no log on unicorn restart in my cap production deploy log. I am not sure if this helps.
I made sure the working_directory matches the root in the default nginx page. I made sure the listen in unicorn matches the upstream app server unix in the default page. I made sure the nginx.conf file included the default config nginx page in sites-enabled.
Well this is 6 months old, but I'm going to answer it anyways. The issue is proxy_pass in @app in sites_enabled/default. It's trying to pass to the upstream server http://app , but you don't have that upstream set, you have it named app_server.
You need to rename: proxy_pass http://app
to: proxy_pass http://app_server
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With