angularnginx

Disable caching of a single file with try_files directive


I'm serving Angular 2 application with nginx using location section this way:

location / {
  try_files $uri $uri/ /index.html =404;
}

try_files directive tries to find the requested uri in root directory and if it fails to find one it simply returns index.html

How to disable caching of index.html file?


Solution

  • Found a solution using nginx named locations:

    location = / {
        add_header Cache-Control no-cache;
        expires 0;
        try_files /index.html =404;
    }
    
    location / {
        gzip_static on;
        try_files $uri @index;
    }
    
    location @index {
        add_header Cache-Control no-cache;
        expires 0;
        try_files /index.html =404;
    }