6

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?

Nakilon
  • 128
  • 1
  • 1
  • 8
imastar
  • 71
  • 1
  • 3
  • 8
  • Why do you also include `$scheme://` in your cache-key? The scheme shouldn't make a difference with respect to the cached content, usually. – maxschlepzig May 30 '19 at 14:01
  • @maxschlepzig HTTP could completely hide user information whereas HTTPS would show some info... Although the cookie should not travel along HTTP since that's a liability. – Alexis Wilke Mar 07 '22 at 17:03

6 Answers6

13

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
cadmi
  • 7,308
  • 1
  • 17
  • 23
  • 1
    Because i'm not using proxy, i changed to fastcgi_cache_bypass $http_X_Update; and then curl -s -o /dev/null -H "X-Update: 1" mydomain.com but still not working – imastar Mar 29 '13 at 07:28
  • @imastar for fastcgi must `fastcgi_cache_bypass $cookie_x_update` directive. my answer slightly too late :) but can be useful to someone else – cadmi May 06 '16 at 17:40
  • 5
    This doesn't delete anything, it just helps you to bypass the cache. Your users still would see the cached version. Also, that curl command already has "silent" flag why are you trying to save the output to "/dev/null"? that makes no sense. – xecute Sep 10 '18 at 03:17
  • @xecute, the `-s` option just suppresses curl diagnostics - i.e. without `-o /dev/null` you still get the body printed to stdout (if any). Example: `curl -s https://www.heise.de` (compare this with the output when adding `-o /dev/null` to that call). – maxschlepzig May 30 '19 at 13:43
  • This command does not delete stuff but it does INVALIDATE the cache. If you deleted the page from your site and want it to return a 404 - make sure nginx is set to cache 404 responses, otherwise you will keep geeitng the page – jitbit Jun 07 '19 at 19:05
3

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.

Satys
  • 183
  • 1
  • 2
  • 11
2

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) .

1

You can write a small shell function to compute the cache file location, e.g. for your levels=1:2 configured cache:

function nxcacheof {
    local x=$(echo -n "$1" | md5sum)
    echo "${x:31:1}/${x:29:2}/${x:0:32}"
}

Taking your cache-path and cache-key setting into account you can delete the cached entry for - say - https://example.org/foo like this:

$ rm /backup/cache/$(nxcacheof https://example.org/foo)
maxschlepzig
  • 895
  • 7
  • 16
  • New to bash. Is the function something inside a .sh file? Can you show full example? Not sure how the $(...) is working in this context. – Steve Tomlin Jan 12 '22 at 09:11
  • @SteveTomlin You can copy'n'paste the function into your term to make it available. Or you copy it into a file `xyz.sh` and source it in your shell (`source xyz.sh`) to make it available. `$()` is command substitution syntax, i.e. the shell executes whats inside the parentheses in a sub-shell, captures its output and inserts it there. To see what's the result of the substitution you can also directly call `nxcacheof https://example.org/foo` in your shell. To get an idea for how this mechanism work you can also execute something like `echo /backup/cache/$(nxcacheof https://example.org/foo)`. – maxschlepzig Jan 12 '22 at 22:10
0

Create shell script to iterate over all found cache items for this domain

purgecache.sh

#!/bin/bash

function purgecache {
  arr=($(grep -lr $1 /var/cache/nginx*))
  for val in ${arr[@]}; do
    rm $val
    echo "cleared cached for $val"
  done
}

function main {
  purgecache "$@"
}

main "$@"

to call this method

sudo bash ./purgecache.sh mywebsite.com
0

Another potential solution, if you are looking for the actual file(s) in your file system would be to grep through the cache directory and print out the files one by one this way (assuming you're using the default /var/cache/nginx directory:

sudo grep -lr 'example.com/' /var/cache/nginx*

This should then print out the list of files to inspect and/or delete:

/var/cache/nginx/0/74/c7de22912j735dc5d2206ffd37ba6740
/var/cache/nginx/d/15/9df468b0z77187f1fc020f9226ac515d
/var/cache/nginx/7/47/8sh3nlkjs8833f83383b67df9a2ca477
simoes
  • 101
  • 1