通过CloudFront提供CSS文件时出错,直接从原点工作正常

时间:2012-09-11 07:06:57

标签: ruby-on-rails heroku ruby-on-rails-3.2 amazon-cloudfront

我通过自定义域名www.shmocket.com在Heroku上运行Rails 3.2应用程序。我设置了一个CloudFront发行版,其中包含www.shmocket.com作为唯一来源。它的备用域名是cdn.shmocket.com,它只是配置为CloudFront分配网址d3n87gn5nko8eq.cloudfront.net的CNAME。

当我通过CloudFront申请资产时,Rails应用程序会给我一个错误:

> curl http://cdn.shmocket.com/assets/application-dce210940d2cd3c699c4809800ae2f0b.css

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
  <HEAD><TITLE>Internal Server Error</TITLE></HEAD>
  <BODY>
    <H1>Internal Server Error</H1>
    undefined method `each' for nil:NilClass
    <HR>
    <ADDRESS>
     WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20) at
     www.shmocket.com:80
    </ADDRESS>
  </BODY>
</HTML>
curl: (18) transfer closed with 100318 bytes remaining to read

在Rails日志文件中:

2012-09-11T06:53:28+00:00 app[web.1]: [2012-09-11 06:53:28] ERROR NoMethodError: undefined method `each' for nil:NilClass
2012-09-11T06:53:28+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/handler/webrick.rb:71:in `service'
2012-09-11T06:53:28+00:00 app[web.1]:   /app/vendor/ruby-1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
2012-09-11T06:53:28+00:00 app[web.1]:   /app/vendor/ruby-1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
2012-09-11T06:53:28+00:00 app[web.1]:   /app/vendor/ruby-1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
2012-09-11T06:53:28+00:00 heroku[router]: GET www.shmocket.com/assets/application-dce210940d2cd3c699c4809800ae2f0b.css dyno=web.1 queue=0 wait=0ms service=18ms status=500 bytes=325

直接访问http://www.shmocket.com/assets/application-dce210940d2cd3c699c4809800ae2f0b.css可以正常工作。

使用真正的CloudFront域时会发生同样的错误,即http://d3n87gn5nko8eq.cloudfront.net/assets/application-dce210940d2cd3c699c4809800ae2f0b.css

CloudFront配置:

http://screencast.com/t/A62AKBGX
http://screencast.com/t/u9Grgj5i
http://screencast.com/t/vzRduwxor50v

老实说,我很难过,根本不知道是什么原因引起的。我怀疑这是显而易见的,我忽略了......

任何建议都表示赞赏......

编辑:

有人建议,某些中间件会对CloudFront发送的标头做出奇怪的反应。我没有手动包含任何中间件并检查使用过的宝石列表,我也看不到任何内容:

source :rubygems

ruby '1.9.3'

gem 'rails', '~> 3.2.8'
gem 'haml', '~> 3.2.0.beta.3'
gem 'jquery-rails', '2.0.2'
gem 'slim_migrations', '~> 3.2.1'
gem 'redis', '~> 2.2.2'
gem 'redis-rails', '~> 3.2.3'
gem 'memoizer', '~> 1.0.1'
gem 'marginalia', '~> 1.1.0'
gem 'awesome_print', '~> 1.0.2'
gem 'log_buddy', '~> 0.7.0'
gem 'squeel', '~> 1.0.9'
gem 'airbrake', '~> 3.1.2'
gem 'httparty', '~> 0.8.3'
gem 'ember-rails', '~> 0.7.0'

group :assets do
  gem 'oily_png', '~> 1.0.2'
  gem 'bootstrap-sass', github: 'dszczyt/bootstrap-sass'
  gem 'font_awesome', '~> 2.0.0'
  gem 'sass-rails', '~> 3.2.5'
  gem 'compass-rails', '~> 1.0.3'
  gem 'coffee-rails', '~> 3.2.2'
  gem 'uglifier', '~> 1.2.7'
end

group :development do
  gem 'heroku', '~> 2.30.5'
  gem 'annotate', '~> 2.5.0'
  gem 'foreman', '~> 0.57.0'
  gem 'quiet_assets', '~> 1.0.1'
  gem 'sextant', '~> 0.1.3'
  gem 'right_aws', '~> 3.0.4'
end

group :development, :test do
  gem 'mysql2', '~> 0.3.12b4'
end

group :production do
  gem 'pg', '~> 0.14.0'
end

1 个答案:

答案 0 :(得分:0)

看起来这是Heroku的Rack :: Cache + Dalli宝石的问题 https://github.com/rails/rails/issues/8366

禁止在production.rb中加载Rack :: Cache修复了问题:

config.action_dispatch.rack_cache = nil