Apache无法为Rails应用程序提供静态资源服务

8

我想配置我的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文件?你是否已经执行了rake命令到public/assets文件夹中? - clyde
是的,我编译了我的资源,它们都放在了正确的位置上。 - soupdiver
你能修好这个问题吗? - nathanvda
2个回答

0

我使用了,

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

2
抱歉,但这并不符合我的问题。我已经完成了您提到的所有步骤,但如果我理解正确,当提供资产时,Rails 不应该参与其中。Apache 应该自行处理此操作。因此是 LocationMatch。但是 Rails 仍然处理应由 Apache 直接完成的资产目录的请求。 - soupdiver

0
这是直接来自于Rails资产管道(Asset-pipeline)文档有关Apache服务器的内容:

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

4.1.1 远期 Expires 头部

预编译资源存在于文件系统中,并由您的 web 服务器直接提供。它们默认不具有远期头,因此要享受指纹识别的好处,您需要更新服务器配置以添加这些头。

对于 Apache:

# 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>

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