1

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 url.com/b.php?q=123 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?

Mr.Boon
  • 1,471
  • 4
  • 24
  • 43

1 Answers1

3

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;
Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • Thanks for the answer! I just tried this setup, but got an error. I made a file cache_map.conf to include in the http block. But doing a configtest run, it show: nginx: [emerg] "map" directive is not allowed here in /etc/nginx/cache_map.conf:1 – Mr.Boon Apr 11 '15 at 08:21
  • That happens if you include it from the wrong place. It has to be directly within the `http` block, not within a `server` block. – Michael Hampton Apr 11 '15 at 08:24
  • My bad, just noticed that! Error is gone now. It's not caching some how, but I'm gonna keep bug hunting. – Mr.Boon Apr 11 '15 at 08:32