图像资产未显示为使用capistrano 3部署的rails 4 app

时间:2016-01-02 06:58:57

标签: ruby-on-rails ruby-on-rails-4 capistrano asset-pipeline capistrano3

我有一个非常基本的rails 4应用程序,可显示静态图像。我使用上限3设置部署。部署似乎是成功的,但图像资产没有显示在网站上。在我的服务器上,/ home / deploy / project_name / current / public / assets文件夹包含png文件,因此看起来资产管道正在以某种方式工作。来自主机IP地址的图像资源的URL是什么?我该如何进一步调试?

nginx错误日志在请求资产时显示以下内容:

2016/01/02 08:16:52 [error] 27418#0: *19 open() "/home/deploy/myapp/public/assets/det1-766379905061a5c18b06fd8bae4d21e69304a6e3cedd5d5665a0f953d5743f0e.png" failed (2: No such file or directory), client: 45.50.73.43, server: localhost, request: "GET /assets/det1-766379905061a5c18b06fd8bae4d21e69304a6e3cedd5d5665a0f953d5743f0e.png HTTP/1.1", host: "52.23.158.11", referrer: "http://[ip]/det"

此文件存在于具有完全读取权限的current / public / assets文件夹中,但nginx仍无法找到它。

Capfile

require 'capistrano/setup'
require 'capistrano/deploy'
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/rails'
require 'capistrano/rails/assets' # for asset handling add
require 'capistrano/rails/migrations' # for running migrations
require 'capistrano/puma'

配置/ deploy.rb

lock '3.4.0'

set :application, 'allenwoot'
set :repo_url, 'git@github.com:allenwoot/allenwoot.git'
set :branch, :master
set :deploy_to, '/home/deploy/allenwoot'
set :pty, true
set :linked_files, %w{config/database.yml config/application.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
set :keep_releases, 5
set :rvm_type, :user
set :rvm_ruby_version, 'ruby-2.2.1'

set :puma_rackup, -> { File.join(current_path, 'config.ru') }
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock"    #accept array for multi-bind
set :puma_conf, "#{shared_path}/puma.rb"
set :puma_access_log, "#{shared_path}/log/puma_error.log"
set :puma_error_log, "#{shared_path}/log/puma_access.log"
set :puma_role, :app
set :puma_env, fetch(:rack_env, fetch(:rails_env, 'production'))
set :puma_threads, [0, 8]
set :puma_workers, 0
set :puma_worker_timeout, nil
set :puma_init_active_record, true
set :puma_preload_app, false

编辑:我注意到nginx日志表明它正在寻找位置的文件 /home/deploy/myapp/public/assets/det1-766379905061a5c18b06fd8bae4d21e69304a6e3cedd5d5665a0f953d5743f0e.png 它出现在 /home/deploy/myapp/current/public/assets/det1-766379905061a5c18b06fd8bae4d21e69304a6e3cedd5d5665a0f953d5743f0e.png

我在capistrano中添加了一个post deploy钩子来复制它,这确实修复了它,但它感觉很hacky。有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

资产管道通过处理和复制文件到服务器来工作,因此每个文件都有使用文件内容和其他一些信息计算的哈希值。文件名到散列文件名的映射存储在public/assets/.sprockets-manifest-[somehash].json

因此,如果引用了错误的资产,则意味着以下其中一项很可能属实:

  • Sprockets manifest指向错误的文件。如果sprockets编译缓存未正确清除,则会发生这种情况:删除tmp/cache/assets的内容,然后重新部署/重新编译资产。如果修复它,您可能需要升级链轮,有一个bug in an older version
  • 自部署以来服务器尚未重新启动,因此未获取新文件。