Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nottellyou opened this issue Aug 22, 2016 · 13 comments
Closed

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

nottellyou opened this issue Aug 22, 2016 · 13 comments

Comments

@nottellyou
Copy link

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

@hpatoio
Copy link

hpatoio commented Aug 22, 2016

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

@mtx-z
Copy link

mtx-z commented Sep 4, 2016

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.

@jesinwp
Copy link

jesinwp commented Sep 4, 2016

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
Copy link

mtx-z commented Sep 4, 2016

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.

@jesinwp
Copy link

jesinwp commented Sep 4, 2016

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
Copy link

mtx-z commented Sep 4, 2016

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.

@jesinwp
Copy link

jesinwp commented Sep 4, 2016

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

@mtx-z
Copy link

mtx-z commented Sep 4, 2016

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
Copy link

mtx-z commented Sep 5, 2016

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 !

@hpatoio
Copy link

hpatoio commented Sep 5, 2016

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

@mtx-z
Copy link

mtx-z commented Sep 5, 2016

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

@nottellyou
Copy link
Author

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
Copy link

mtx-z commented May 21, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants