Ruby on Rails,如何禁用/关闭 ETag

发布于 2024-08-18 16:07:13 字数 125 浏览 6 评论 0原文

您好,

如何在 Ruby on Rails v2.3.5 中关闭 ETag

当我向 RoR/Mongrel 发出直接请求时,会出现 ETag 标头。

TIA,

-丹尼尔

Greetings,

How do I turn off ETag(s) in Ruby on Rails v2.3.5

When I do a direct request to to the RoR/Mongrel an ETag header is present.

TIA,

-daniel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

审判长 2024-08-25 16:07:13

更容易:

config.middleware.delete Rack::ETag

much easier:

config.middleware.delete Rack::ETag
拥抱影子 2024-08-25 16:07:13

response.etag = nil 放入 before_filter 中不起作用。 etag 是在发送响应之前生成的(它是根据正文计算的,因此是在所有渲染完成之后)。

禁用 etag 使用和生成(从而节省 md5 花费的时间)的正确解决方法是这个猴子补丁:

module ActionController
  class Request
    # never match any incomming etag
    def etag_matches?(etag)
      false
    end
  end

  class Response
    # fake rails that our response already has an etag set and so none is generated automatically
    def etag?
      true
    end
  end
end

Putting response.etag = nil in a before_filter does not work. The etag is generated just before the response is send (it's caluculated from the body so after all rendering has been done).

The proper workaround to disable etag use and generation (and so save the time spend in md5) it this monkey patch:

module ActionController
  class Request
    # never match any incomming etag
    def etag_matches?(etag)
      false
    end
  end

  class Response
    # fake rails that our response already has an etag set and so none is generated automatically
    def etag?
      true
    end
  end
end
季末如歌 2024-08-25 16:07:13

有一个 etag setter 方法ActionController::Response 对象,如果 ETag HTTP 标头为空,它将删除它,因此您应该能够在控制器中清除它(可能在 before 过滤器中):

response.etag = nil

There's an etag setter method on the ActionController::Response object, which deletes the ETag HTTP header if it's blank, so you should just be able to clear it in your controller (probably in a before filter):

response.etag = nil
翻身的咸鱼 2024-08-25 16:07:13

我不认为它们默认是打开的。

我的理解是,需要使用 stale?/fresh_when 调用或类似方法显式设置它们。

I don't think they are on by default.

My understanding is that they need to be explicitly set using stale?/fresh_when call or similar.

涙—继续流 2024-08-25 16:07:13

我正在 WEBrick 上的 Rails 4 中工作,尝试获取缓存响应,直到它在每天的特定时间过期。看起来自动生成的 ETag 正在干扰过期缓存,这就是我搜索这个答案的原因。我在这里没有找到任何有用的东西,但我确实解决了我的问题,所以我会分享。

tl;dr 设置 Last-Modified 标头

但将其设置为什么?在我的情况下,我试图优化一个 Web 服务,该服务返回每天同一时间运行的进程的结果。我的响应标头最终看起来像这样:

response.headers['Cache-Control'] = "max-age=86400"
response.headers['Expires'] = getCacheTime
response.headers['Last-Modified'] = getLastModified

首先,您要显式编写 Cache-Control 标头来覆盖默认值。我将我的设置为 24 小时,以与到期标头的最大值一致。我使用一个函数设置过期标头,如下所示:

def getCacheTime
    now = Time.now.utc
    cacheTime = Time.utc(now.year, now.month, now.day, 22, 00, 00)
    if now > cacheTime
        cacheTime = cacheTime + (60 * 60 * 24)
    end

    cacheTime.httpdate
end

getLastModified 函数返回的时间比 getCacheTime 函数正好少 24 小时。看来,设置此选项至少在我当前的开发环境中会抑制 ETag(另一个验证缓存标头)。

I am working in Rails 4 on WEBrick, trying to get a response to cache until it expires at a specific time each day. It looks like the auto-generated ETag is interfering with the expiration cache which is why I searched for this answer. I didn't find anything helpful here, but I did solve my problem, so I'll share.

tl;dr Set the Last-Modified header

But set it to what? In my situation I was trying to optimize a web service that returned the results of a process that runs at the same time every day. My response headers ended up looking like this:

response.headers['Cache-Control'] = "max-age=86400"
response.headers['Expires'] = getCacheTime
response.headers['Last-Modified'] = getLastModified

First you want to explicitly write the Cache-Control header to overwrite whatever the default is. I set mine to be 24 hours to coincide with the maximum of my expiration header. I set the expiration header with a function the looks something like this:

def getCacheTime
    now = Time.now.utc
    cacheTime = Time.utc(now.year, now.month, now.day, 22, 00, 00)
    if now > cacheTime
        cacheTime = cacheTime + (60 * 60 * 24)
    end

    cacheTime.httpdate
end

The getLastModified function returns exactly 24 hours less than the getCacheTime function. It seems that setting this will suppress the ETag (another validation caching header) at least in my current development environment.

蹲墙角沉默 2024-08-25 16:07:13

为什么不在应用程序控制器中添加一个 before_filter 将 etag 设置为 nil ?

Why not add a before_filter in your application controller which sets etag to nil?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文