title: let nginx cache files depending on filename only (instead of full path)
body: <p>This question is about caching of nginx only, not about the cache headers and browser cache.</p>
  
  <p>I am operating an application that displays details of items on URLs of the form </p>
  
  <pre><code>http://my-server/details/&lt;id&gt;/
  </code></pre>
  
  <p>where <em>id</em> is a unique identifier of the item. While the resulting HTML page is auto-generated and unique to the item, the page relatively includes other JavaScript and CSS files. Those files are static and do <strong>not</strong> depend on the item id. the application is very slow and I want nginx to cache whatever is possible.</p>
  
  <p>nginx acts as a reverse proxy. Is it possible to set the <a href="http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_key" rel="nofollow">proxy_cache_key</a> property for all .js and .css subrequests to the filename only (omitting the id from the url)? </p>
  
  <p>Example: Accessing <em><a href="http://my-server.tld/details/abc/style.css" rel="nofollow">http://my-server.tld/details/abc/style.css</a></em> first and <em><a href="http://my-server.tld/details/def/style.css" rel="nofollow">http://my-server.tld/details/def/style.css</a></em> afterwards should therefore result in a cache hit. But accessing <em><a href="http://my-server.tld/details/abc/" rel="nofollow">http://my-server.tld/details/abc/</a></em> and <em><a href="http://my-server/details/def/" rel="nofollow">http://my-server/details/def/</a></em> afterwards should not (generated HTML depends on id).</p>
  
  <p>This is what I am thinking of as base configuration</p>
  
  <pre><code>server {
    listen 80;
    server_name my-server.tld;
  
    location / {
      # root, doesn't matter
    }
  
    location /details {
      # request to non-asset, cache as usual
    }
  
    location ~ ^/details/[a-z0-9]+/[a-z0-9]+\.(js|css)$ {
      # request to static asset, use filename only as cache key
    }
  }
  </code></pre>