设置公用文件夹中文件的HTTP标头

问题描述 投票:1回答:1

我想为从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"
}
ruby-on-rails ruby ruby-on-rails-5
1个回答
1
投票

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

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

但是,要使更改生效,必须在启动服务器之前使用create a development cache使用rails dev: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

dev-cache和服务器

$ 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
© www.soinside.com 2019 - 2024. All rights reserved.