2008-10-23 31 views
3

我正在使用Mongrel :: DirHandler來控制靜態文件的響應頭 - 這在我的開發機器上效果很好。我的生產機器使用Passenger,所以我的標題沒有設置。如何在使用Passenger時控制靜態文件的標題?從我的environment.rbMongrel :: DirHandler等效於Passenger

片段:

if defined? Mongrel::DirHandler 
    module Mongrel 
    class DirHandler 
     def send_file_with_expires(req_path, request, response, header_only=false) 

     if req_path =~ /((\/images)|javascripts|stylesheets)/ 
      response.header['Cache-Control'] = 'max-age=315360000' 
      response.header['Expires'] = (Time.now + 10.years).rfc2822 
     else 
      response.header["Last-Modified"] = Time.now.httpdate 
      response.header["Expires"] = 0 
      # HTTP 1.0 
      response.header["Pragma"] = 'no-cache' 
      # HTTP 1.1 ‘pre-check=0, post-check=0′ (IE specific) 
      response.header["Cache-Control"] = 'no-store, no-cache, must-revalidate, max-age=0, pre-check=0, post-check=0' 
     end 

     send_file_without_expires(req_path, request, response, header_only) 
     end 
     alias_method :send_file_without_expires, :send_file 
     alias_method :send_file, :send_file_with_expires 
    end 
    end 
end 

回答

2

由於您使用的乘客,我假設你在Apache,因此您的申請不通過雜種狗去了。如果是這樣,您可以在應用程序的public目錄中的.htaccess文件中建立規則。

Here's an explination on how to do it.