【问题标题】:Rails 4.2.6 app deployed using puma not loading assets and images.使用 puma 部署的 Rails 4.2.6 应用程序不加载资产和图像。
【发布时间】:2017-03-08 17:26:06
【问题描述】:

之前我使用乘客和独角兽以及 capistrano 和 nginx 部署了相同的源代码。那里工作正常。但现在我正在尝试使用 Puma 服务器做同样的事情。资产根本没有加载。

环境/生产.rb

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.
  # Code is not reloaded between requests.
  config.cache_classes = true


  config.eager_load = true

  # Full error reports are disabled and caching is turned on.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = true
  config.action_mailer.raise_delivery_errors = true


  config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

  config.assets.js_compressor = :uglifier

  config.assets.compile = true

  config.assets.digest = true



  # config.force_ssl = true

  # Use the lowest log level to ensure availability of diagnostic information
  # when problems arise.
  config.log_level = :debug

  config.action_mailer.default_url_options = { host: ENV["SMTP_HOST"] }
  config.action_mailer.asset_host = ENV["SMTP_HOST"]
  # config.action_mailer.delivery_method = :letter_opener
  config.action_mailer.raise_delivery_errors = false

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    #Enter the smtp provider here ex: smtp.mandrillapp.com
    address: ENV["SMTP_ADDRESS"],
    port: ENV['SMTP_PORT'].to_i,
    #Enter the smtp domain here ex: xxx.com
    domain: ENV["SMTP_DOMAIN"],
    #Enter the user name for smtp provider here
    user_name: ENV["SMTP_USERNAME"],
    #Enter the password for smtp provider here
    password: ENV["SMTP_PASSWORD"],
    authentication: 'plain',
    enable_starttls_auto: true
  }

  config.i18n.fallbacks = true

  config.active_support.deprecation = :notify

  config.log_formatter = ::Logger::Formatter.new

  config.active_record.dump_schema_after_migration = false
end

capfile

# Load DSL and set up stages
require "capistrano/setup"

# Include default deployment tasks
require "capistrano/deploy"

# Load the SCM plugin appropriate to your project:
#
# require "capistrano/scm/hg"
# install_plugin Capistrano::SCM::Hg
# or
# require "capistrano/scm/svn"
# install_plugin Capistrano::SCM::Svn
# or
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git

require 'capistrano/rails'
#require 'capistrano/passenger'

# If you are using rvm add these lines:
require 'capistrano/rvm'
set :rvm_type, :user
set :rvm_ruby_version, '2.2.4'

# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
#   https://github.com/capistrano/rvm
#   https://github.com/capistrano/rbenv
#   https://github.com/capistrano/chruby
#   https://github.com/capistrano/bundler
#   https://github.com/capistrano/rails
#   https://github.com/capistrano/passenger
#
require "capistrano/rvm"
# require "capistrano/rbenv"
# require "capistrano/chruby"
require "capistrano/bundler"
require "capistrano/rails/assets"
require "capistrano/rails/migrations"
# require "capistrano/passenger"
require 'capistrano/puma'

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }

【问题讨论】:

    标签: ruby-on-rails nginx puma


    【解决方案1】:

    您可以检查您的Nginx config file 是否允许您的应用程序使用资产(图像、字体等)。

    location ^~ /(assets|fonts|swfs|images)/ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
      }
    

    希望这会对你有所帮助。

    【讨论】:

      【解决方案2】:

      如果你还没有,添加 rails_12factor gem 正在为我解决这种类似的问题

      希望对你有帮助。

      gem 'rails_12factor', group: :production
      

      -- 更新--

      您的 capfile 中也有重复的要求。下面是 Capfile 的干净版本。您需要将 rvm 设置移动到 deploy.rb。如果您包含 capistrano/rails,则无需使用捆绑器、资产和迁移

      # Load DSL and set up stages
      require "capistrano/setup"
      
      # Include default deployment tasks
      require "capistrano/deploy"
      
      # Load the SCM plugin appropriate to your project:
      #
      # require "capistrano/scm/hg"
      # install_plugin Capistrano::SCM::Hg
      # or
      # require "capistrano/scm/svn"
      # install_plugin Capistrano::SCM::Svn
      # or
      require "capistrano/scm/git"
      install_plugin Capistrano::SCM::Git
      
      # Include tasks from other gems included in your Gemfile
      #
      # For documentation on these, see for example:
      #
      #   https://github.com/capistrano/rvm
      #   https://github.com/capistrano/rbenv
      #   https://github.com/capistrano/chruby
      #   https://github.com/capistrano/bundler
      #   https://github.com/capistrano/rails
      #   https://github.com/capistrano/passenger
      #
      require 'capistrano/rvm'
      require 'capistrano/rails'
      require 'capistrano/puma'
      # require "capistrano/rvm"
      # require "capistrano/rbenv"
      # require "capistrano/chruby"
      # require "capistrano/bundler"
      # require "capistrano/rails/assets"
      # require "capistrano/rails/migrations"
      # require "capistrano/passenger"
      
      # Load custom tasks from `lib/capistrano/tasks` if you have any defined
      Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
      

      【讨论】:

      • 我将它添加到 gem 文件中。部署了它。重新启动了服务器。但资产仍然没有加载
      • 编译后的资产是否存在不同的名称,或者它甚至没有被编译?您应该能够在部署期间在 capistrano 输出中看到它
      【解决方案3】:

      您应该在部署文件之前运行预编译命令,这样可以提高性能。

      RAILS_ENV=production rails assets:precomile
      

      之后,只需提交更改并部署您的文件。

      见:http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets

      【讨论】:

        猜你喜欢
        • 2016-04-06
        • 1970-01-01
        • 2018-01-30
        • 1970-01-01
        • 2017-10-13
        • 2017-03-16
        • 1970-01-01
        • 2013-04-29
        • 2014-09-23
        相关资源
        最近更新 更多