Search code examples
httpnginxbrowser-cachecache-controletag

nginx ETag invalidation


I'm caching static resources using the following code in my nginx.conf:

http {
    ...
    gzip on;
    gzip_types *;
    gzip_vary on;
    ...
    server {
        ...
        location /static {
            alias /opt/static_root;
            expires max;
        }
    }
}

This is sufficient to set the following http headers:

$ curl -I example.com/static/css/bootstrap.min.css
Content-Length: 97874
Last-Modified: Mon, 21 Nov 2016 18:30:33 GMT
ETag: "58333d49-17e52"
Expires: Thu, 31 Dec 2037 23:55:55 GMT
Cache-Control: max-age=315360000

However, even though the Last-Modified date is later than the cached version the browser has, I'm still seeing the old version of the file (tested on Firefox 50.0 and Chrome 54.0.2840.98).

How can I invalidate the ETag so that whenever I deploy diffs to my static files, the browser understands to reload them?

I have tried nginx -s reload, to no avail.


Solution

  • ETags are used when a client makes a Conditional Request to revalidate an expired resource. But in your case the resource won't expire until 2037! The browser will continue to serve the resource from its cache until then without ever checking with the server. That's what you told it to with your Expires header.

    Typically if you're going to do far-future expires like that you have to version the resource by changing the name. Or you can change the Expires to something shorter, in which case the ETags will be used when the client tries to revalidate.