与答案的大编辑
如果您的Nginx应用程序抛出“502 Bad Gateway”,请检查您的Nginx配置文件/etc/nginx/conf.d/*
,可能您可能在不同的.conf
文件中声明了两次相同的域。 Nginx错误日志不会显示任何内容。
=====
我在相同的Centos VPS上运行了一些Rails应用程序。这些应用程序与Nginx和Unicorn一起运行。今天我注意到一个应用程序抛出“502 Bad Gateway”错误。其他Rails应用程序也没问题。
因此,我不确定是什么原因造成了这个错误,但昨天我对VPS进行了一些更改,我更改了ssh端口,然后执行了shutdown。其他应用程序都很好,只有一个不是。但是,我不确定这是不是原因。
我一直在尝试重启Unicorn和Nginx,但网站仍然没有回应。 真正令人沮丧的是/ var / log / nginx错误日志为空,Rails生产日志也是空的。只有Unicorn stderr日志有某种信息:
MyWeb即可/电流/日志/ 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;
}
}
Unicorn文件:
MyWeb即可/共享/配置/ 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
答案 0 :(得分:0)
经过几个小时,我发现问题的根源是(错误地)我使用相同的server_name有两个不同的/etc/nginx/conf.d/
文件。这是我的502 Bad Gateway的来源。