【问题标题】:cache: [GET /] miss on nginx unicorn in production缓存:[GET /] 错过生产中的 nginx 独角兽
【发布时间】:2012-03-15 01:21:07
【问题描述】:

我的 Rails 3.2 应用程序收到 cache: [GET /] miss 错误消息。

我使用 nginx 作为独角兽服务器的代理,并使用 capistrano 进行部署。当我启动服务器时,我得到了很多像上面这样的重复错误。 Capistrano 在部署期间肯定会预编译资产。我在下面包含了配置文件(对不起,很冗长)。

任何想法或至少提示找出问题所在?

application.rb

config.assets.enabled = true

production.rb

# Disable Rails's static asset server
# (Apache or nginx SHOULD already do this BUT THEY DON'T)
config.serve_static_assets = true

# Don't fallback to assets pipeline if a precompiled asset is missed
# If I disable this I don't get assets served at all
config.assets.compile = true

deploy.rb

require 'bundler/capistrano'

set :application, "network"
set :rails_env, "production"
set :deploy_to, "/var/www/#{application}"
set :repository,  "/var/repo/#{application}.git".
set :branch, "master"

set :use_sudo, false
set :user, "root"

set :unicorn_conf, "#{deploy_to}/current/config/unicorn.rb"
set :unicorn_pid, "#{deploy_to}/shared/pids/unicorn.pid"

set :bundle_roles, [:app]
set :normalize_asset_timestamps, false

set :scm, :git

server "mydomain.ru", :app, :web, :db, :primary => true

namespace :deploy do
  task :restart do
    run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -USR2 `cat #{unicorn_pid}`; else cd #{deploy_to}/current && bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D; fi"
  end
  task :start do
    run "cd #{deploy_to}/current && bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D"
  end
  task :stop do
    run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -QUIT `cat #{unicorn_pid}`; fi"
  end
end

unicorn.rb

DEPLOY_TO = "/var/www/network"

RAILS_ROOT  = "#{DEPLOY_TO}/current"
PID_FILE    = "#{DEPLOY_TO}/shared/pids/unicorn.pid"
SOCKET_FILE = "#{DEPLOY_TO}/shared/unicorn.sock"
LOG_FILE    = "#{RAILS_ROOT}/log/unicorn.log"
ERR_LOG     = "#{RAILS_ROOT}/log/unicorn_error.log"
OLD_PID     = PID_FILE + '.old'

listen SOCKET_FILE, :backlog => 1024

timeout 30
worker_processes 2
user "danchenkov", "danchenkov"

working_directory RAILS_ROOT
pid PID_FILE
stderr_path ERR_LOG
stdout_path LOG_FILE

preload_app true

GC.respond_to?(:copy_on_write_friendly=) and
  GC.copy_on_write_friendly = true

before_exec do |server|
  ENV["BUNDLE_GEMFILE"] = "#{rails_root}/Gemfile"
end

before_fork do |server, worker|
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!

    old_pid = "#{server.config[:pid]}.old"
    if File.exists?(old_pid) && old_pid != server.pid
      begin
        sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
        Process.kill(sig, File.read(old_pid).to_i)
      rescue Errno::ENOENT, Errno::ESRCH
      end
  end
  sleep 1
end

after_fork do |server, worker|
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

nginx.conf

###############################################################################
#
#    UNICORN UPSTREAM
#
###############################################################################
upstream unicorn_network_app_server {
  server unix:/var/www/network/shared/unicorn.sock fail_timeout=0;
}


###############################################################################
#
#     NETWORK - PROXY TO UNICORN ON UNIX SOCKET
#
###############################################################################
server {
  listen 80;
  server_name network.mydomain.ru;
  client_max_body_size 1G;

  access_log /var/log/nginx/network.access.log main;
  error_log /var/log/nginx/network.error.log notice;

  keepalive_timeout 5;

  root /var/www/network/current/public;

  try_files $uri/index.html $uri.html $uri @network_app;

  # Main location
  location @network_app {
    proxy_pass http://unicorn_network_app_server;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size 10m;
    client_body_buffer_size 128k;
    proxy_connect_timeout 90;
    proxy_send_timeout 90;
    proxy_read_timeout 90;
    proxy_buffer_size 4k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
  }

  error_page 500 502 503 504 /500.html;
  location = /500.html {
    root /var/www/network/current/public;
  }
}

【问题讨论】:

  • 在阅读您的问题后感谢 Alexel。我解决了我的问题。 :)

标签: ruby-on-rails ruby-on-rails-3 nginx capistrano unicorn


【解决方案1】:

我注意到我的网络服务器日志中有类似的消息,并注意到我必须让 Rails 知道我没有在 config/environments/production.rb 中使用缓存

config.action_controller.perform_caching = false

【讨论】:

  • 成功了,谢谢!我想知道为什么它默认不关闭。
【解决方案2】:

如果你在开发模式下运行,‘config.action_controller.perform_caching’设置为 false。此标志通常在相应的“config/environments/*.rb”中设置,并且默认情况下禁用缓存以进行开发和测试,并启用缓存。

【讨论】:

    猜你喜欢
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 2013-12-19
    相关资源
    最近更新 更多