search

Home  >  Q&A  >  body text

node.js - 关于nginx的ETag的一些问题

关于nginx的Etag问题,nginx默认是有Etag的,但是问题就在于:无论你对源代码做了什么改动,比如说改动了注释,改动了空行什么的,ETag都会变。
(但实际上,比如我改动了注释,但是这个注释可能对程序员很重要,对用户来讲是无所谓的,因此ETag是没有必要变化的)

而http权威指南中说Etag解决了这个问题:

有些文档可能被修改了,但所做修改并不重要,不需要让世界范围内的缓存都重装数据(比如对拼写或注释的修改)。

而nginx默认也加了前缀“W/”来标识弱验证器

那这样的话ETag显然没有解决这个问题。

由于笔者对nginx停留在仅供使用的阶段,并没有源码查看和插件编写的经历,所以想问:

nginx有没有办法配置或者现成的插件或者自定义增加一些内容,从而让ETag并不是任意更改都变化,有一定的变通性?或者说 nginx有没有办法自定义ETag的生成规则?

迷茫迷茫2867 days ago620

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 16:00:54

    It is best not to control this with nginx. It can be controlled during automated build. For example, webpack removes all comments when packaging. In addition, do not negotiate caching through ETag, and set hash through webpack to force caching.

    Additional: Why not use Etag

    1. The situation you listed

    2. Negotiation cache requires 304, still needs to send a request

    3. During load balancing, if the inodes of the same file on different physical machines are different, different ETags will be generated (not tested)

    Reference: The gap caused by configuration errors: 200 OK (FROM CACHE) and 304 NOT MODIFIED

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 16:00:54

    The correct approach is not to rely on etag to verify whether the file has been modified.
    If you want to deal with the problem of static file caching, you should add special request parameters to the web link so that the browser will think that the link has changed and re-request the latest version of the file

    If you want to modify the annotation without causing etag changes, you should configure the front-end automation process, separate the code running in the production environment from the development code, and then compress and obfuscate the annotated code with one click and publish it to the production environment

    reply
    0
  • Cancelreply