Disable nginx cache for JavaScript files Disable nginx cache for JavaScript files javascript javascript

Disable nginx cache for JavaScript files


I have the following nginx virtual host (static content) for local development work to disable all browser caching:

server {    listen 8080;    server_name localhost;    location / {        root /your/site/public;        index index.html;        # kill cache        add_header Last-Modified $date_gmt;        add_header Cache-Control 'no-store, no-cache';        if_modified_since off;        expires off;        etag off;    }}

No cache headers sent:

$ curl -I http://localhost:8080HTTP/1.1 200 OKServer: nginx/1.12.1Date: Mon, 24 Jul 2017 16:19:30 GMTContent-Type: text/htmlContent-Length: 2076Connection: keep-aliveLast-Modified: Monday, 24-Jul-2017 16:19:30 GMTCache-Control: no-storeAccept-Ranges: bytes

Last-Modified is always current time.

Note: nginx's $date_gmt format is not per the HTTP spec (see changing the format).


The expires and add_header directives have no impact on NGINX caching the files, those are purely about what the browser sees.

What you likely want instead is:

location stuffyoudontwanttocache {    # don't cache it    proxy_no_cache 1;    # even if cached, don't try to use it    proxy_cache_bypass 1; }

Though usually .js etc is the thing you would cache, so perhaps you should just disable caching entirely?


What you are looking for is a simple directive like:

location ~* \.(?:manifest|appcache|html?|xml|json)$ {    expires -1;}

The above will not cache the extensions within the (). You can configure different directives for different file types.