Nginx FastCGI Caching possible on custom URL's?

Mr.Boon asked:

I am trying to build a custom caching solution for my website with FastCGI caching.

I’m trying to cache only certain URL’s that get a boat load of traffic. I know from my logs exactly which URL’s that are, but they are always changing.

Is there a way I can specify certain URL’s that Nginx has to cache, but not cache everything else.

I’ve followed this tutorial, and I got caching to work. But I cannot work out how to cache custom URLs. Say for example.

Ideally I would store a list of URL’s in a .conf file, which Nginx loads, and it only uses cache for those URL’s, but doesn’t cache all other files. Is this possible with Nginx?

My answer:

You can use a map for this. Place it in a separate file which is included from the server’s http block, and then whenever it changes, reload nginx.

An example:

map $request_uri $my_no_cache {
    default           1;
    /b.php?q=123      0;

To use it, place this in the relevant server or location:

fastcgi_no_cache $my_no_cache;
fastcgi_cache_bypass $my_no_cache;

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.