title: Define specific cache control header for selected file only
body: <p>I'm setting up a Nginx sever (version 1.17.1) for Gatsby following up the recommendation at <a href="https://www.gatsbyjs.org/docs/caching/" rel="nofollow noreferrer">https://www.gatsbyjs.org/docs/caching/</a>.</p>
  
  <p>The snippet below is the portion my <a href="https://nginx.org/en/docs/http/ngx_http_core_module.html#server" rel="nofollow noreferrer"><code>server {}</code></a> block attempting implementing the recommended caching configuration;</p>
  
  <pre><code>location ~* \.(?:html)$ {
      add_header Cache-Control "public, max-age=0, must-revalidate";
  }
  
  location /static {
      add_header Cache-Control "public, max-age=31536000, immutable";
  }
  
  location ~* \.(?:css|js)$ {
      add_header Cache-Control "public, max-age=31536000, immutable";
  }
  
  location /sw\.js {
      add_header Cache-Control "public, max-age=0, must-revalidate";
  }
  </code></pre>
  
  <p>Equally tried an <a href="https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if" rel="nofollow noreferrer">if statement</a> in place of the <a href="https://nginx.org/en/docs/http/ngx_http_core_module.html#location" rel="nofollow noreferrer"><code>location {}</code></a> block for defining cache configuration for the service worker file, <code>sw.js</code>, as below;</p>
  
  <pre><code>if ($request_uri ~ ^sw\.(?:js)$) {
      set $no_cache 1;
  }
  </code></pre>
  
  <p>Unfortunately, all files get cached successfully as expected except <code>sw.js</code>.</p>
  
  <p>What am I doing wrong and how can I fix it so as to effectively set cache control header for <code>sw.js</code> to <code>public, max-age=0, must-revalidate</code>?</p>