【问题标题】:Apache won't server static assets for rails appApache 不会为 Rails 应用程序提供静态资产
【发布时间】:2013-08-28 02:38:04
【问题描述】:

我正在尝试配置我的 apache 服务器以从我的 rails 应用程序提供静态资产。我已经尝试了建议的配置,但我的资产仍然没有显示,当尝试直接访问它们时,我只是得到一个 rails 错误,没有找到匹配的控制器,但我认为资产的东西应该由 apache 直接处理。 我的 apache 配置如下所示:

<VirtualHost *:80>
ServerName xxx
DocumentRoot /home/xxx/test/public
PassengerEnabled off

<LocationMatch "^/assets/.*$">
Header unset ETag
FileETag None
ExpiresActive On
ExpiresDefault "access plus 1 year"
</LocationMatch>
ProxyPass / http://127.0.0.1:9292/
ProxyPassReverse / http://127.0.0.1:9292/
</VirtualHost>

我错过了什么吗?

【问题讨论】:

  • 资产,如.css?你对 public/assets 文件夹做了 rake 吗?
  • 是的,我编译了我的资产,它们都已经到位
  • 你能解决这个问题吗?

标签: ruby-on-rails apache asset-pipeline


【解决方案1】:

这直接来自有关 Apache 服务器的 Rails 资产管道文档:

http://guides.rubyonrails.org/asset_pipeline.html

4.1.1 远期过期标头

预编译资产存在于文件系统中,并由您的网络服务器直接提供服务。默认情况下,它们没有未来的标头,因此要获得指纹识别的好处,您必须更新服务器配置以添加这些标头。

对于阿帕奇:

# The Expires* directives requires the Apache module
# `mod_expires` to be enabled.
<Location /assets/>
  # Use of ETag is discouraged when Last-Modified is present
  Header unset ETag
  FileETag None
  # RFC says only cache for 1 year
  ExpiresActive On
  ExpiresDefault "access plus 1 year"
</Location>

【讨论】:

    【解决方案2】:

    我用过,

    RAILS_ENV=production bundle exec rake assets:precompile
    

    为了让一切正常,我将其添加到 config/application.rb...

    module MyApp
      class Application < Rails::Application
    .
    .
        config.assets.precompile += ['custom.css']    
        config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)
    .
    .
      end
    end
    

    (我创建了 custom.css.scss。但 Rails 无法识别 .scss,正如您在上面看到的。)我假设您的所有资产在预编译后都出现在 public/assets 文件夹中。我不明白你在用 LocationMatch 做什么,请原谅我的无知。此外,我没有使用端口 80。我使用了 8000。不确定这是否会有所不同。

    另外,config/environments/production.rb 中有一个设置,

    # Disable Rails's static asset server (Apache or nginx will already do this).
    config.serve_static_assets = false
    

    【讨论】:

    • 对不起,但这并不适合我的问题。我已经完成了你提到的所有步骤,但如果我理解它,在提供资产时不应该涉及正确的轨道。 Apache 应该自己做这件事。因此是LocationMatch。但是 rails 仍在处理对资产目录的请求,这应该由 apache 直接完成
    猜你喜欢
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 2015-06-12
    • 2019-09-05
    • 2014-08-26
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多