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

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

问题描述

我最近遇到了ETag HTTP标头的概念. (),但是我仍然有一个问题负责生成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服务器/负载平衡器(例如: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.

当然,通常我们使用工具(服务器,负载平衡器,应用程序框架等)来帮助我们履行这些职责.但是,没有任何规范定义期望与应用程序相对应的"Web服务器"提供的内容,这只是一个实际问题,即您正在使用的工具中哪些功能可用.

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服务器级别的静态文件)和

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:

  • 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天全站免登陆