Define specific cache control header for selected file only Define specific cache control header for selected file only nginx nginx

Define specific cache control header for selected file only


I ended up with the following nginx configuration regarding caching for Gatsby.js:

        location ~* \.(?:html)$ {          add_header Cache-Control "public, max-age=0, must-revalidate";        }        location /page-data {          add_header Cache-Control "public, max-age=0, must-revalidate";        }        location = /sw.js {          add_header Cache-Control "public, max-age=0, must-revalidate";        }        location /static {          add_header Cache-Control "public, max-age=31536000, immutable";        }        location ~* \.(?:js|css)$ {          add_header Cache-Control "public, max-age=31536000, immutable";        }

@OP: With which configuration did you end up? Maybe you can edit this answer to match the perfect solution after all; for people searching for "caching nginx gatsby".


The order of precedence of location is described here https://nginx.org/en/docs/http/ngx_http_core_module.html#location

When an exact match is found (using the = modifier) the search terminates and regular expressions will not be checked, so you can use that for your sw.js:

location = /sw.js {    add_header Cache-Control "public, max-age=0, must-revalidate";}