在上游“app”nginx中找不到主机,而不是在欢迎页面上停留的应用程序

时间:2015-01-15 17:18:53

标签: ruby-on-rails-4 nginx unicorn

这是我第一次设置nginx和独角兽。

我的capistrano部署已经完成,一切都成功了。

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

我的nginx conf在/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/*;
} 

我的默认文件位于/ 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;
    }

当我这样做时

deploy@localhost:~$ sudo nginx -s reload
nginx: [emerg] host not found in upstream "app" in /etc/nginx/sites-enabled/default:46

当我进入

/shared/tmp/sockets

我没有文件。我不认为我应该手动创建它。我正在使用capistrano 3.我想生成这个文件吗?

我正在使用

require 'capistrano3/unicorn' #in capfile
在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

我把我的上限文件放在这里是因为我注意到我的上限生产部署日志中没有登录unicorn重启。我不确定这是否有帮助。

我确保work_directory与默认nginx页面中的root匹配。 我确保unicorn中的listen与默认页面中的上游应用服务器unix相匹配。 我确保nginx.conf文件包含启用站点的默认配置nginx页面。

1 个答案:

答案 0 :(得分:6)

这已经有6个月了,但无论如何我都会回答这个问题。问题是sites_enabled / default中@app的proxy_pass。它试图传递给上游服务器http://app,但是您没有上游设置,您将其命名为app_server。

您需要重命名: proxy_pass http://app

于: proxy_pass http://app_server