how to delete single nginx cache file?

when access http://example.com/ its blank but working good with http://example.com/index.php i need to delete nginx cache file for http://example.com/ how to do it? Here my nginx cache conf.

    fastcgi_cache_path  /backup/cache levels=1:2 keys_zone=my-cache:1000m inactive=1000m;
    fastcgi_temp_path /backup/cache/tmp 1 2;
    fastcgi_cache_key "$scheme://$host$request_uri";

How to get the cache file name in /backup/cache folder?


Solution 1:

one method, for example, but quite flexible an versatile

add this line to nginx.conf ...

proxy_cache_bypass $http_x_update;

... and you can anytime update any uri in cache with simple http request with "magic" header. for example

curl -s -o /dev/null  -H "X-Update: 1" mydomain.com

or

curl -s -o /dev/null  -H "X-Update: 1" mydomain.com/some/long/url/

for the security and satisfaction of paranoia :D you can change header name to any blablabla, for example

proxy_cache_bypass $http_x_gangnamstyle;
#proxy_cache_bypass $http_x_mycatsnickname;
#proxy_cache_bypass $http_x_b2ca678b4c936f905fb82f2733f5297f;

and

curl -s -o /dev/null  -H "X-GangnamStyle: 1" mydomain.com
curl -s -o /dev/null  -H "X-mycatsnickname: 1" mydomain.com
curl -s -o /dev/null  -H "X-b2ca678b4c936f905fb82f2733f5297f: 1" mydomain.com

Solution 2:

You must be having somewhere in your configuration this line,

fastcgi_cache_key *key*;

You need to find the key from there for your respective URL, and then calculate md5 for that key string.

Now suppose the md5 value comes to xm*****p3w, So, your cache file for the URL is /backup/cache/w/p3/xm*****p3w. Now delete it howsoever you wish.

The other automated way if you have nginx_cache_purge module with your nginx,

fastcgi_cache_purge CACHEREGION $cache_key;

where, CACHEREGION is the cache region defined by fastcgi_cache_path directive, and $cache_key is the value of fastcgi_cache_key directive.

Note: fastcgi_cache_purge directive is allowed in location block.

Solution 3:

I created a simple script to find cache file location quicly and delete it:

#Usage sample : ~/cache_location.sh static.example.com/contents/3145.jpg
CACHE_PATH=/home/static-example/cache
MD5=$(echo -n $1|md5sum|cut -d ' ' -f 1)
f1=$(printf $MD5 | tail -c 1)
f2=$(printf $MD5 | tail -c 3|head -c 2)
rm $CACHE_PATH/$f1/$f2/$MD5

It will calculate cache location based on Path and Filename. you need to install md5sum (yum install coreutils) .