如何生成和配置 ETag? [英] How ETags are generated and configured?

查看:24
本文介绍了如何生成和配置 ETag?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近了解了 ETag HTTP 标头的概念.(this)但我仍然有一个问题对于负责生成 ETag 的特定 HTTP 资源?

I recently came through the concept of ETag HTTP header. (this) But I still have a problem that for a particular HTTP resource who is responsible to generate ETags?

换句话说,就是实际的应用程序、容器(例如:Tomcat)、Web Server/负载均衡器(例如:Apache/Nginx)?

In other words, it is actual application, container (Ex:Tomcat), Web Server/Load balancer (Ex: Apache/Nginx)?

有人可以帮忙吗?

推荐答案

与 HTTP 规范的大多数方面一样,责任最终在于提供资源的人.

As with most aspects of the HTTP specification, the responsibility ultimately lies with whoever is providing the resource.

当然,在这种情况下,我们通常会使用工具(服务器、负载平衡器、应用程序框架等)来帮助我们履行这些职责.但是没有任何规范定义网络服务器"(与应用程序相反)应该提供什么,这只是您使用的工具中可用的功能的实际问题.

Of course, it's often that case that we use tools—servers, load balancers, application frameworks, etc.—that help us fulfill those responsibilities. But there isn't any specification defining what a "web server", as opposed to the application, is expected to provide, it's just a practical question of what features are available in the tools you're using.

现在,特别关注 ETags,一个常见的情况是框架或 Web 服务器可以配置为自动散列响应(主体或其他内容)并将结果放入ETag.然后,对于条件请求,它会生成一个响应并对其进行哈希处理以查看它是否已更改,如果没有更改,则自动发送条件响应.

Now, looking at ETags in particular, a common situation is that the framework or web server can be configured to automatically hash the response (either the body or something else) and put the result in the ETag. Then, on a conditional request, it will generate a response and hash it to see if it has changed, and automatically send the conditional response if it hasn't.

举两个我熟悉的例子,nginx可以做到这一点 使用 Web 服务器级别的静态文件,以及 Django 可以通过应用程序级别的动态响应来做到这一点.

To take two examples that I'm familiar with, nginx can do this with static files at web server level, and Django can do this with dynamic responses at the application level.

这种方法很常见,易于配置,而且效果很好.但是,在某些情况下,它可能并不最适合您的用例.例如:

That approach is common, easy to configure, and works pretty well. In some situations, though, it might not be the best fit for your use case. For example:

  • 要计算哈希值以与传入的 ETag 进行比较,您首先必须有一个响应.因此,尽管条件响应可以为您节省传输响应的开销,但它无法为您节省生成响应的成本.因此,如果生成响应的成本很高,并且您有 ETags 的替代来源(例如,存储在数据库中的版本号),您可以使用它来获得更好的性能.
  • 如果您打算使用 ETags防止意外覆盖状态更改方法,您可能需要添加自己的应用程序代码以使比较和设置逻辑原子化.
  • To compute a hash to compare to the incoming ETag you first have to have a response. So although the conditional response can save you the overhead of transmitting the response, it can't save you the cost of generating the response. So if generating your response is expensive, and you have an alternative source of ETags (for example, version numbers stored in the database), you can use that to achieve better performance.
  • If you're planning to use the ETags to prevent accidental overwrites with state-changing methods, you will probably need to add your own application code to make your compare-and-set logic atomic.

因此,在某些情况下,您可能希望在应用程序级别创建您的 ETags.再次以 Django 为例,它为您提供了一种简单的方法来提供您自己的函数来计算ETags.

So in some situations you might want to create your ETags at the application level. To take Django as an example again, it provides an easy way for you to provide your own function to compute ETags.

总而言之,为您控制的资源提供 ETags 最终是您的责任,但您很可能能够利用软件堆栈中的工具为您完成这些工作.

In sum, it's ultimately your responsibility to provide the ETags for the resources you control, but you may well be able to take advantage of the tools in your software stack to do it for you.

这篇关于如何生成和配置 ETag?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆