Anshul Kai
Anshul Kai

Reputation: 4088

nginx: Force browser to revalidate

Below is my nginx server config. The Cache-Control header was recently added. My browser still has the old file cached and is NOT fetching it from the server - I verify this by tailing the access.log. Is there any way to force all users to fetch updated files from my server?

    location /webapp {
        root /usr/share/nginx/html;
        try_files $uri $uri/ /webapp/index.html;
        add_header  Cache-Control must-revalidate always;
    }

Upvotes: 1

Views: 2134

Answers (1)

cnst
cnst

Reputation: 27218

Once the file is cached, there is really not much you can do to invalidate the entry.

The user would generally have to issue a forced reload, which can often be accomplished by holding the SHIFT key together with pressing the RELOAD button.

Alternatively, if the resources at stake are loaded from within a webpage that you still do control, then you can often add a random string as an argument to the resource (e.g., replace .png in your webpage with .png?v=$sha1), which will often be ignored for static resources by nginx, yet will ensure that the resource has a new URL, and thus couldn't have been cached yet. (The same trick, with adding a random noop parameter to the URL can also be used by the user to effectively force a new request regardless of whether the resource at stake might already have been cached.)

Upvotes: 2

Related Questions