FRiCKLE/ngx_cache_purge

Hi, How can use PHP code to clear fastcgi_cache file??

nottellyou opened this issue ยท 13 comments

Hi, How can use PHP code to clear fastcgi_cache file??

Just send an HTTP request to the correct URL using curl or your favorite library.

mtx-z commented

Here someone did a PHP script that, using URL, re-build the cache filename to delete it (selective purge).
Not tested yet, I'll. Also i'll check how filename are generated to see if patterns match.

EDIT: here we can see

Both the key and file name in a cache are a result of applying the MD5 function to the proxied URL. The levels parameter defines hierarchy levels of a cache. For example, in the following configuration

fastcgi_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;
file names in a cache will look like this:

/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c

I'm trying to match something

EDIT 2: i'm not able to build the correct cache filepath/filename using the linked script. I contacted the author to know more about how and where he found the filepath build pattern. You can see my last comment on the digitalocean post.

EDIT3: on typical purge system, the parameter (url) sended to the purge is build like $scheme$request_method$host$1 wich give us something like httpGETwww.domain.com (homepage) but PHP script is still wrong on the filename/path.

Some test i made : http://img4.hostingpics.net/pics/827481fastcgifilenamecache.png

For now, it seem that building the cache filepath/name using MD5 value of the fastcgi_cache_key of the given url is not working.

Achieving the right cache file delete from PHP would more lot more efficient than curl-ing the cache purge URL location i think.

Hi @mtx-z,

I'm the author of that article, I've replied to your comment.

Are you adding the slash at the end of the domain when doing a curl request like

curl -d 'url=http://www.example.com/' http://domainname.com/purge.php
mtx-z commented

Hi, thanks for answering.

Actually I'm not using the script with a curl, I want to use it directly from some php script.
That's why I'm testing the script on some URL to test if I can match the cache file name.

Have you looked at the screenshot I made? I set the cache key as header to then pass it in your script (just for test) in a sandbox but it 'ever match the name.

From the cache key, I tried removing the last slash, add it, remove some part... To pass the string to your script but never matched the actual cache file name and path.

I also tried using all your script with a full URL, and just from the $hash variable creation with the cache key directly.

I wasn't able to find any code from fastcgi module from where the filename and filepath are builded. Have you?

Thx a lot.

Ps: when solved or' or, I'll make a full report on your post.

Yes I checked those screenshots, there is "www.domain.com" in the PHP script but during the actual testing some real domain (censored)?

mtx-z commented

Yes domain was hided (censored for sharing it) . I can give you full url by email if you want.
But md5 result and cache files are from real url, not changed.

Can you send it using this contact form? Also send me your Nginx virtual host file.

mtx-z commented

Sure. But as you, I'm not using a cache purge location.
I want to be able to do a selective delete url based of the corresponding cache file.

mtx-z commented

Thanks a lot @jesinwp. The PHP script you made work perfectly.

Error was entierly mine: resetting the cache_key further in the config file, so not the same as the builded one...
Thanks a lot :)

This also resolve the issue @nottellyou !

@mtx-z great ! Share the code and close the ticket !

mtx-z commented

Here is the PHP code to delete the file cache related to the given URL.

$cache_path = '/etc/nginx/cache/';
$url = parse_url($_POST['url']);
if(!$url)
{
    echo 'Invalid URL entered';
    die();
}
$scheme = $url['scheme'];
$host = $url['host'];
$requesturi = $url['path'];
$hash = md5($scheme.'GET'.$host.$requesturi);
var_dump(unlink($cache_path . substr($hash, -1) . '/' . substr($hash,-3,2) . '/' . $hash));

hi, @mtx-z

I tried server times , but failed.

server
{
listen 80;
server_name clear.xxxxxxxxxxxxx.net;
index index.html index.htm index.php default.html default.htm default.php;
root /usr/local/nginx/fastcgi_cache;

add_header request-filename $request_filename;
add_header requestfilename  $document_root$fastcgi_script_name;

    location ~ [^/]\.php(/|$)
    {
            try_files $uri =404;
            fastcgi_pass  unix:/dev/shm/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
    }

}
In the dir /usr/local/nginx/fastcgi_cache, only one php file, but visit clear.xxxxxxxxxxxxx.net can't run php file, other file can run.

Do you know why???

mtx-z commented

Hey @nottellyou

Sorry for the huuge delay.
Your fastcgi_cache folder should not contain .php file, are you sure your cache is working correctly ?

On this screen, there is a capture of my cache folder (along with nginx conf).
Also take a look at the original post here, you can search for "mtxz" in the page.