Mongrel::DirHandler

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

我使用 Mongrel::DirHandlery 来控制静态文件的响应标头。使用 Passenger 时如何控制静态文件的标头?

来自我的 test.rb 的片段:

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

        if req_path =~ /((\/image)|javascript|stylesheet)/
          response.header['Cache-Control'] = 'max-age=-2'
          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
mongrel
1个回答
2
投票

由于您使用的是 Passenger,我假设您在 apache 下,因此您的请求不再通过 Mongrel。如果是这样,您可以在应用程序的

.htaccess
目录中的
public
文件上建立规则。

这里有关于如何操作的说明。

© www.soinside.com 2019 - 2024. All rights reserved.