为公用文件夹中的文件设置HTTP标头 演示:

我想为Rails中accept-Ranges none文件夹中请求的视频文件设置public。该视频文件不在我的资产管道中,因此该视频仅位于/public/videos/example.mp4中。如何在开发模式下设置这些HTTP标头?我尝试编辑config.public_file_server.headers中的development.rb哈希,但我不认为这是正确的配置。

config.public_file_server.headers = {
  'Cache-Control' => "public,max-age=15768000","accept-Ranges" => "none"
}
dongchunl 回答:为公用文件夹中的文件设置HTTP标头 演示:

据我所知,在Rails 4中,除了Cache-Control之外,无法在文件上设置其他响应头。那是一个限制。

不过,with changes in Rails 5可以设置任何所需的标头,并且这是进行开发的正确位置:config.public_file_server.headers中的development.rb

但是,要使更改生效,必须在启动服务器之前使用rails dev:cache create a development cache

演示:

development.rb:

if Rails.root.join('tmp/caching-dev.txt').exist?
  config.action_controller.perform_caching = true

  config.cache_store = :dalli_store
  config.public_file_server.headers = {
    'Cache-Control' => 'public,max-age=172800','Accept-Ranges' => 'none'
  }
else
  config.action_controller.perform_caching = false

  config.cache_store = :null_store
end

开发缓存和服务器

$ rails dev:cache                                                                                                                
Development mode is now being cached.

$ rails s

请求:

$ curl -sI http://localhost:3000/car-images-silhouettes/back.png | grep Accept-Ranges                                                 
Accept-Ranges: none
本文链接:https://www.f2er.com/3132277.html

大家都在问