![](/img/trans.png)
[英]Rails application not responding on DigitalOcean with Unicorn/nginx combination
[英]Server Ruby on Rails + unicorn + nginx stop responding
我的服務器Rails worker正常,但是10分鍾后沒有請求,它的響應是一個錯誤的網關。 我確實認為我的配置是合理的,但是它不起作用。 我沒有更多的想法怎么回事。
那我的配置:
unicorn.rb:
@dir = File.expand_path(File.dirname(__FILE__)) + "/.."
worker_processes 2
working_directory @dir
timeout 10
listen File.join('/tmp/nutrimais.sock')
listen File.join('/tmp/nutrimais_2.sock')
preload_app true# if ENV['RAILS_ENV'] != 'development'
GC.respond_to?(:copy_on_write_friendly=) and
GC.copy_on_write_friendly = true
check_client_connection false
before_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
Process.kill 'QUIT', Process.pid
end
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
end
after_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
end
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end
Nginx的配置:
upstream nutrimais {
# Path to Puma SOCK file, as defined previously
server unix:/tmp/nutrimais.sock max_fails=2 fail_timeout=10s;
server unix:/tmp/nutrimais_2.sock;
}
server {
listen 80;
server_name dev.nutrimais.com.br;
location / {
autoindex on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
# time out settings
proxy_next_upstream http_502 timeout;
proxy_next_upstream_timeout 0;
proxy_next_upstream_tries 0;
proxy_connect_timeout 159s;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_buffer_size 64k;
proxy_buffers 16 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_pass_header Set-Cookie;
proxy_redirect off;
proxy_hide_header Vary;
proxy_set_header Accept-Encoding '';
proxy_ignore_headers Cache-Control Expires;
proxy_set_header Referer $http_referer;
proxy_set_header Host $host;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://nutrimais;
}
}
的Gemfile:
source 'https://rubygems.org'
gem 'rails', '4.2.4'
gem 'unicorn-rails', '~> 2.2'
gem 'pg'
gem 'mysql2', '~> 0.3.18'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'duktape'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'bootstrap-sass'
gem 'devise'
gem 'simple_form'
gem 'minitest'
gem "paperclip", "~> 4.3"
gem 'aws-sdk', '< 2.0'
gem 'mail_form', '~> 1.5.0.rc'
gem 'sendgrid-ruby'
gem 'zopim_rails'
gem 'meta-tags'
gem 'ckeditor'
gem 'slick_rails'
group :development do
gem 'better_errors'
gem 'binding_of_caller', :platforms=>[:mri_20]
gem 'quiet_assets'
gem 'rails_layout'
gem 'spring-commands-rspec'
gem 'web-console', '~> 2.0'
gem 'spring'
end
group :production do
gem 'therubyracer'
end
group :development, :test do
gem 'factory_girl_rails'
gem 'faker'
gem 'pry-rails'
gem 'pry-rescue'
gem 'rspec-rails'
gem 'rubocop'
gem 'byebug'
end
group :test do
gem 'capybara'
gem 'database_cleaner'
gem 'launchy'
gem 'selenium-webdriver'
end
日志同時給出錯誤的網關:
Started GET "/menus" for 127.0.0.1 at 2016-01-20 17:25:17 +0000
I, [2016-01-20T17:25:17.580380 #9] INFO -- : Processing by MenusController#index as HTML
D, [2016-01-20T17:25:17.904933 #9] DEBUG -- : [1m[36mMenu Load (322.3ms)[0m [1mSELECT `menus`.* FROM `menus` ORDER BY created_at DESC[0m
I, [2016-01-20T17:25:20.006674 #9] INFO -- : Started GET "/menus" for 127.0.0.1 at 2016-01-20 17:25:20 +0000
它停留在Started GET
,什么也不做
這就是我所看到的。 單個unicorn
后端在兩個套接字上監聽Nginx正在它們之間進行負載平衡。 但是,兩個設置可能會導致負載平衡“卡住”。
# Sets unlimited tries before trying next server
proxy_next_upstream_tries 0;
# Unlimited time allowed before passing request to next server
proxy_next_upstream_timeout 0;
一些建議:
proxy_pass
直接傳遞給實際存在的單個后端應用程序即可。 最后,當您獲得502時,請嘗試測試直接連接后端。 然后,您將知道問題出在您的后端是否真的持續掉下來,或者您的Nginx配置是否存在問題。
要直接測試應用,可以使用socat
直接連接到套接字:
$ socat - UNIX-CONNECT:/you/socket/path.sock,crnl
-->GET / HTTP/1.1
-->Host: example.com.com
-->X-Forwarded-Proto: https
-->
我發現發生了什么,Linux防火牆終止了該連接。 當我將數據庫放置在同一台機器上時,它可以正常運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.