如何在Rails生产环境中设置Webpacker?

3

我正准备在服务器上设置/部署我的项目,所以我先在本地机器上使用生产环境运行它,以确保它能正常运行。然而,我遇到了有关使用的资源的问题。

我正在使用Rails版本5.2,Webpacker版本5.4和Nginx。

目前在我的Windows PC上使用Docker和Docker-compose进行测试。

我清除了public/assetspublic/packs,然后执行了RAILS_ENV=production bundle exec rake assets:precompile

它能够生成和编译资产。

但是,如果在浏览器上查看,布局会出现问题,并且浏览器控制台会出现很多错误:

GET http://w.local/ 500 (Internal Server Error)
11Refused to apply style from '<URL>' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
w.local/:27 GET http://w.local/assets/activestorage.self-1ed4604ac2170045f1ffca4edb81a75246661555e4f9cf682bb8a21825e32e1c.js?body=1 net::ERR_ABORTED 500 (Internal Server Error)
w.local/:31 GET http://w.local/assets/application.self-12be097b9a2442b0b6cdcb5146d1d63c00abcde3675ff34d1de6126cb13e6714.js?body=1 net::ERR_ABORTED 500 (Internal Server Error)
w.local/:30 GET http://w.local/assets/cable.self-8484513823f404ed0c0f039f75243bfdede7af7919dda65f2e66391252443ce9.js?body=1 net::ERR_ABORTED 500 (Internal Server Error)
w.local/:28 GET http://w.local/assets/turbolinks.self-569ee74eaa15c1e2019317ff770b8769b1ec033a0f572a485f64c82ddc8f989e.js?body=1 net::ERR_ABORTED 500 (Internal Server Error)
w.local/:26 GET http://w.local/assets/rails-ujs.self-81c572f39b69ead02e3f97fe43b76954a434591bc2837e3a35af212315e67569.js?body=1 net::ERR_ABORTED 500 (Internal Server Error)
w.local/:29 GET http://w.local/assets/action_cable.self-69fddfcddf4fdef9828648f9330d6ce108b93b82b0b8d3affffc59a114853451.js?body=1 net::ERR_ABORTED 500 (Internal Server Error)
w.local/:35 GET http://w.local/packs/js/common-7e4e8325142b9e8c61a0.js net::ERR_ABORTED 500 (Internal Server Error)
w.local/:31 GET http://w.local/packs/js/utils/ajax/index-f3f37fd87a7659da86ce.js net::ERR_ABORTED 500 (Internal Server Error)
w.local/:31 GET http://w.local/packs/js/utils/modal/index-374c685fe01f171d6f5a.js net::ERR_ABORTED 500 (Internal Server Error)
w.local/:35 GET http://w.local/packs/js/modal-5ec40e25a92d9c39b790.js net::ERR_ABORTED 500 (Internal Server Error)
w.local/:35 GET http://w.local/packs/js/application-1ae2fd1d242e93138623.js net::ERR_ABORTED 500 (Internal Server Error)
w.local/:28 GET http://w.local/assets/turbolinks.self-569ee74eaa15c1e2019317ff770b8769b1ec033a0f572a485f64c82ddc8f989e.js?body=1 net::ERR_ABORTED 500 (Internal Server Error)
w.local/:29 GET http://w.local/assets/action_cable.self-69fddfcddf4fdef9828648f9330d6ce108b93b82b0b8d3affffc59a114853451.js?body=1 net::ERR_ABORTED 500 (Internal Server Error)
w.local/:26 GET http://w.local/assets/rails-ujs.self-81c572f39b69ead02e3f97fe43b76954a434591bc2837e3a35af212315e67569.js?body=1 net::ERR_ABORTED 500 (Internal Server Error)
w.local/:31 GET http://w.local/assets/application.self-12be097b9a2442b0b6cdcb5146d1d63c00abcde3675ff34d1de6126cb13e6714.js?body=1 net::ERR_ABORTED 500 (Internal Server Error)
w.local/:27 GET http://w.local/assets/activestorage.self-1ed4604ac2170045f1ffca4edb81a75246661555e4f9cf682bb8a21825e32e1c.js?body=1 net::ERR_ABORTED 500 (Internal Server Error)
w.local/:30 GET http://w.local/assets/cable.self-8484513823f404ed0c0f039f75243bfdede7af7919dda65f2e66391252443ce9.js?body=1 net::ERR_ABORTED 500 (Internal Server Error)
w.local/:35 GET http://w.local/packs/js/modal-5ec40e25a92d9c39b790.js net::ERR_ABORTED 500 (Internal Server Error)
w.local/:35 GET http://w.local/packs/js/common-7e4e8325142b9e8c61a0.js net::ERR_ABORTED 500 (Internal Server Error)
w.local/:35 GET http://w.local/packs/js/application-1ae2fd1d242e93138623.js 500 (Internal Server Error)
w.local/:31 GET http://w.local/packs/js/utils/modal/index-374c685fe01f171d6f5a.js net::ERR_ABORTED 500 (Internal Server Error)
w.local/:31 GET http://w.local/packs/js/utils/ajax/index-f3f37fd87a7659da86ce.js net::ERR_ABORTED 500 (Internal Server Error)
recaptcha__en.js:125 ReCAPTCHA couldn't find user-provided function: toggleSignUpSubmitBtn

如果我错过了一些配置, 这是我的config/environments/production.rb
Rails.application.configure do
    # Verifies that versions and hashed value of the package contents in the project's package.json
    config.webpacker.check_yarn_integrity = false
    # Settings specified here will take precedence over those in config/application.rb.
  
    # Code is not reloaded between requests.
    config.cache_classes = true
  
    # Eager load code on boot. This eager loads most of Rails and
    # your application in memory, allowing both threaded web servers
    # and those relying on copy on write to perform better.
    # Rake tasks automatically ignore this option for performance.
    config.eager_load = true
  
    # Full error reports are disabled and caching is turned on.
    config.consider_all_requests_local       = false
    config.action_controller.perform_caching = true
  
    # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"]
    # or in config/master.key. This key is used to decrypt credentials (and other encrypted files).
    config.require_master_key = true
  
    # Disable serving static files from the `/public` folder by default since
    # Apache or NGINX already handles this.
    # config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
    config.public_file_server.enabled = true
  
    # Compress JavaScripts and CSS.
    config.assets.js_compressor = :uglifier
    # config.assets.css_compressor = :sass
  
    # Do not fallback to assets pipeline if a precompiled asset is missed.
    config.assets.compile = false
    # config.assets.compile = true
  
    # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb
  
    # Enable serving of images, stylesheets, and JavaScripts from an asset server.
    # config.action_controller.asset_host = 'http://assets.example.com'
  
    # Specifies the header that your server uses for sending files.
    # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
    # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
  
    # Store uploaded files on the local file system (see config/storage.yml for options)
    config.active_storage.service = :local
  
    # Mount Action Cable outside main process or domain
    # config.action_cable.mount_path = nil
    # config.action_cable.url = 'wss://example.com/cable'
    # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]
  
    # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
    # config.force_ssl = true
  
    # Use the lowest log level to ensure availability of diagnostic information
    # when problems arise.
    config.log_level = :debug
  
    # Prepend all log lines with the following tags.
    config.log_tags = [ :request_id ]
  
    # Use a different cache store in production.
    # config.cache_store = :mem_cache_store
    # config.cache_store = :memory_store
  
    # Use a real queuing backend for Active Job (and separate queues per environment)
    # config.active_job.queue_adapter     = :resque
    # config.active_job.queue_name_prefix = "byouten-junior_#{Rails.env}"
  
    config.action_mailer.perform_caching = false
    host = { :host => ENV['DOMAIN_NAME'].presence || '-------', }
    config.action_mailer.default_url_options = host
    Rails.application.routes.default_url_options = config.action_mailer.default_url_options
    #config.action_controller.asset_host = "https://#{host[:host]}"
    config.action_mailer.asset_host = "https://#{host[:host]}"
  
    config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = {
      address: '-------',
      port: '-------',
      authentication: '-------',
      domain: '-------',
      user_name: '-------',
      password: '-------',
      enable_starttls_auto: true,
    }
  
    # Ignore bad email addresses and do not raise email delivery errors.
    # Set this to true and configure the email server for immediate delivery to raise delivery errors.
    # config.action_mailer.raise_delivery_errors = false
  
    # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
    # the I18n.default_locale when a translation cannot be found).
    config.i18n.fallbacks = true
  
    # Send deprecation notices to registered listeners.
    config.active_support.deprecation = :notify
  
    # Use default logging formatter so that PID and timestamp are not suppressed.
    config.log_formatter = ::Logger::Formatter.new
  
    # Use a different logger for distributed setups.
    # require 'syslog/logger'
    # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')
  
    if ENV["RAILS_LOG_TO_STDOUT"].present?
      logger           = ActiveSupport::Logger.new(STDOUT)
      logger.formatter = config.log_formatter
      config.logger    = ActiveSupport::TaggedLogging.new(logger)
    end
  
    # Do not dump schema after migrations.
    config.active_record.dump_schema_after_migration = false
  end
  

这是我的config/webpack/production.js文件:

process.env.NODE_ENV = process.env.NODE_ENV || 'production'

const environment = require('./environment')

module.exports = environment.toWebpackConfig()

我现在陷入了困境。

谢谢。


所有资源都显示 500 (内部服务器错误),您在任何日志中看到任何500错误吗? - arieljuod
这是否意味着问题的原因是由于500错误而不是webpack或其他原因引起的?因为当我尝试在开发模式下运行它时,一切都很好。 - dev test
500错误页面正在显示。有一张图片被呈现出来,但是页面的CSS样式表出现了问题。 - dev test
500服务器错误意味着在尝试提供请求的文件时,服务器出现了故障。您是否在/public/assets和/public/packs文件夹中看到这些文件?如果在运行assets:precompile标记后文件仍然存在,则问题不是webpack(一旦编译了资产,webpacker将不再使用,文件是静态的并按原样提供)。您有这个config.public_file_server.enabled = true,因此如果文件存在,则Rails应用程序应该找到它们并将其提供给浏览器。 - arieljuod
运行 assets:precompile 标签是否成功结束?还是显示了任何错误? - arieljuod
显示剩余3条评论
2个回答

1

在执行precompile之前,我必须首先运行yarn install,然后它就可以正常工作了。抱歉,我认为这就是我错过的。

感谢每个试图帮助我的人。


0

请问您能否分享一下您的docker-compose.yaml和Nginx配置文件是怎样的呢?我猜测这可能是由于Nginx配置不正确所致。


欢迎来到 Stack Overflow。发表答案 应仅用于实际答案。一旦您获得 50 的声誉值,您就可以使用评论来请求澄清。现在,请寻找不需要从 OP 得到澄清的问题。 - tdy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接