ruby on rails - Capistrano 3 + nginx setup: 502 Bad Gateway -
i deploying app via capistrano 3 ubuntu server, deployment process goes through without issues, when load website (ip), error message 502 bad gateway.
my setup:
deploy.rb:
lock "3.8.1" set :application, "project" set :repo_url, "git@bitbucket.org:username/project.git" set :branch, "master" set :tmp_dir, '/home/deployer/tmp' #set :rvm_type, :user # defaults to: :auto #set :rvm_ruby_version, '2.4.0' # defaults to: 'default' # default branch :master # ask :branch, `git rev-parse --abbrev-ref head`.chomp # default deploy_to directory /var/www/my_app_name # set :deploy_to, "/var/www/my_app_name" # default value :format :airbrussh. # set :format, :airbrussh # can configure airbrussh format using :format_options. # these defaults. # set :format_options, command_output: true, log_file: "log/capistrano.log", color: :auto, truncate: :auto # default value :pty false # set :pty, true # default value :linked_files [] # append :linked_files, "config/database.yml", "config/secrets.yml" # default value linked_dirs [] # append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system" # default value default_env {} # set :default_env, { path: "/opt/ruby/bin:$path" } # default value keep_releases 5 set :keep_releases, 5 set(:executable_config_files, %w( unicorn_init.sh )) # files need symlinked other parts of # filesystem. example nginx virtualhosts, log rotation # init scripts etc. set(:symlinks, [ { source: "nginx.conf", link: "/etc/nginx/sites-enabled/default}" }, { source: "unicorn_init.sh", link: "/etc/init.d/unicorn_#{fetch(:application)}" }, { source: "log_rotation", link: "/etc/logrotate.d/#{fetch(:application)}" }, { source: "monit", link: "/etc/monit/conf.d/#{fetch(:application)}.conf" } ]) namespace :deploy desc 'stop unicorn' task :stop on roles(:app) if test("[ -f #{fetch(:unicorn_pid)} ]") execute :kill, capture(:cat, fetch(:unicorn_pid)) end end end desc 'start unicorn' task :start on roles(:app) within current_path rails_env: fetch(:rails_env) execute :bundle, "exec unicorn -c #{fetch(:unicorn_config)} -d" end end end end desc 'reload unicorn without killing master process' task :reload on roles(:app) if test("[ -f #{fetch(:unicorn_pid)} ]") execute :kill, '-s usr2', capture(:cat, fetch(:unicorn_pid)) else error 'unicorn process not running' end end end desc 'restart unicorn' task :restart before :restart, :stop before :restart, :start =begin task :setup_config on roles(:app) sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/default" sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}" run "mkdir -p #{shared_path}/config" put file.read("config/database.example.yml"), "#{shared_path}/config/database.yml" puts "now edit config files in #{shared_path}." end end after "deploy:setup", "deploy:setup_config" task :symlink_config on roles(:app) run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml" end end after "deploy:finalize_update", "deploy:symlink_config" =end desc "make sure local git in sync remote." task :check_revision on roles(:web) unless `git rev-parse head` == `git rev-parse origin/master` puts "warning: head not same origin/master" puts "run `git push` sync changes." exit end end end before "deploy", "deploy:check_revision" end
unicorn.rb
root = "/home/deployer/apps/project/current" working_directory root pid "#{root}/tmp/pids/unicorn.pid" stderr_path "#{root}/log/unicorn.log" stdout_path "#{root}/log/unicorn.log" worker_processes integer(env['web_concurrency']) timeout 30 preload_app true listen '/tmp/unicorn.project.sock', backlog: 64 before_fork |server, worker| signal.trap 'term' puts 'unicorn master intercepting term , sending myself quit instead' process.kill 'quit', process.pid end defined?(activerecord::base) , activerecord::base.connection.disconnect! end after_fork |server, worker| signal.trap 'term' puts 'unicorn worker intercepting term , doing nothing. wait master send quit' end defined?(activerecord::base) , activerecord::base.establish_connection end # force bundler gemfile environment variable # reference capistrano "current" symlink before_exec |_| env['bundle_gemfile'] = file.join(root, 'gemfile') end
on server: /etc/nginx/sites-enabled/default
upstream unicorn { server unix:/tmp/unicorn.project.sock fail_timeout=0; } server { listen 80 default deferred; # server_name example.com; root /home/deployer/apps/project/current/public; location ^~ /assets/ { gzip_static on; expires max; add_header cache-control public; } location ~ ^/(robots.txt|sitemap.xml.gz)/ { root /home/deployer/apps/project/current/public; } try_files $uri/index.html $uri @unicorn; location @unicorn { proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header host $http_host; proxy_redirect off; proxy_pass http://unicorn; } error_page 500 502 503 504 /500.html; client_max_body_size 4g; keepalive_timeout 10; }
error log /var/log/nginx/error.log:
2017/07/26 06:57:55 [crit] 25631#25631: *1879 connect() unix:/tmp/unicorn.project.sock failed (2: no such file or directory) while connecting upstream, client: ip, server: , request: "get /favicon.ico http/1.1", upstream: "http://unix:/tmp/unicorn.project.sock:/500.html", host: "ip", referrer: "http://ip/"
how solve error message? checked paths, should correct, not typos etc.
also, don't miss (symlinks?) in deploy.rb "refresh" /etc/nginx/sites-enabled/default file?
thank every advance.
Comments
Post a Comment