502 Bad Gateway - Nginx - Unicorn 突然停止工作,Nginx 日志为空

502 Bad Gateway - Nginx - Unicorn suddenly stopped working, Nginx logs empty

对答案进行大编辑

如果您的 Nginx 应用程序向您抛出“502 Bad Gateway”,请检查您的 Nginx conf 文件 /etc/nginx/conf.d/*,可能您可能在不同 [=14= 中声明了相同的域两次] 文件。 Nginx 错误日志不会显示任何内容。

=====

我在同一个 Centos VPS 上有几个 Rails 应用程序 运行。这些应用程序是 运行 Nginx 和 Unicorn。今天我注意到一个应用程序抛出“502 Bad Gateway”错误。其他Rails申请都可以

因此,我不确定是什么导致了这个错误,但是昨天我对VPS做了一些修改,我改变了我的ssh端口,然后执行了关机。其他应用程序都很好,只有一个不是。但是,我不确定这是否是原因。

我一直在尝试重启 Unicorn 和 Nginx,但网站仍然没有响应。 真正令人沮丧的是 /var/log/nginx 错误日志是空的,Rails 生产日志也是空的。只有 Unicorn stderr 日志有某种信息:

myweb/current/log/unicorn.stderr.log

INFO -- : worker=1 ready
INFO -- : reaped #<Process::Status: pid 595 exit 0> worker=0
INFO -- : reaped #<Process::Status: pid 598 exit 0> worker=1
INFO -- : master complete
INFO -- : Refreshing Gem list
INFO -- : unlinking existing socket=/var/sockets/unicorn.myweb_production.sock
INFO -- : listening on addr=/var/sockets/unicorn.myweb_production.sock fd=11
INFO -- : worker=0 ready
INFO -- : master process ready
INFO -- : worker=1 ready

我认为我的 Nginx.conf 文件没有问题,它是模板生成的,适用于所有网站。开始了:

/etc/nginx/conf.d/miweb.conf

upstream unicorn_myweb_production {
  server unix:/var/sockets/unicorn.myweb_production.sock fail_timeout=0;
}
server {
  listen 80;
  client_max_body_size 4G;
  keepalive_timeout 10;

  error_page 500 502 504 /500.html;
  error_page 503 @503;

  server_name myweb.com www.myweb.com;
  root /var/www/myweb/current/public;
  try_files $uri/index.html $uri @unicorn_myweb_production;

  location @unicorn_myweb_production {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://unicorn_myweb_production;
    # limit_req zone=one;
    access_log /var/log/nginx/myweb_production.access.log;
    error_log /var/log/nginx/myweb_production.error.log;
  }

  location = /50x.html {
    root html;
  }

  location = /404.html {
    root html;
  }

  location @503 {
    error_page 405 = /system/maintenance.html;
    if (-f $document_root/system/maintenance.html) {
      rewrite ^(.*)$ /system/maintenance.html break;
    }
    rewrite ^(.*)$ /503.html break;
  }

  if ($request_method !~ ^(GET|HEAD|PUT|POST|DELETE|OPTIONS)$ ){
    return 405;
  }

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }

  location ~ \.(php|html)$ {
    return 405;
  }
}

独角兽档案:

myweb/shared/config/unicorn.rb

working_directory "/var/www/myweb/current"
pid "/var/www/myweb/shared/tmp/pids/unicorn.pid"
stdout_path "/var/www/myweb/shared/log/unicorn.stdout.log"
stderr_path "/var/www/myweb/shared/log/unicorn.stderr.log"

listen "/var/sockets/unicorn.myweb_production.sock"

worker_processes 2
timeout 30

preload_app true

before_exec do |server|
  ENV["BUNDLE_GEMFILE"] = "/var/www/myweb/current/Gemfile"
end

before_fork do |server, worker|
  # Disconnect since the database connection will not carry over
  if defined? ActiveRecord::Base
    ActiveRecord::Base.connection.disconnect!
  end

  # Quit the old unicorn process
  old_pid = "#{server.config[: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
      # someone else did our job for us
    end
  end

  if defined?(Resque)
    Resque.redis.quit
  end

  sleep 1
end

after_fork do |server, worker|
  # Start up the database connection again in the worker
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
  end

  if defined?(Resque)
    Resque.redis = 'localhost:6379'
  end
end

几个小时后,我发现问题的根源是(错误地)我有两个不同的 /etc/nginx/conf.d/ 文件使用相同的 server_name。那是我的 502 Bad Gateway 的来源。