How do I clear fastcgi_cache with PHP?
13,342
If you have
fastcgi_cache_path /tmp/nginx keys_zone=myzone:8m
just call
rm -Rf /tmp/nginx/*
It's really as simple as this: When you want to clean the cache, clean the cache :) (That in this case is just a folder)
Author by
amandawulf
Updated on June 09, 2022Comments
-
amandawulf almost 2 years
Is there a built-in way to clear the Nginx fastcgi_cache with PHP? I know I can write a PHP script that goes through and manually deletes all the cache files, but that seems too much like a hack.
-
amandawulf over 11 yearsThanks! I'm trying to do this with PHP on the fly, though. Is there a built-in PHP function, or would you just have to use the PHP unlink function to delete the files?
-
KingCrunch over 11 yearsehm... technically
unlink()
is the built-in function. When you are talking about the recursion, have a look at this comment: php.net/manual/de/function.unlink.php#101261 The factcgi-cache has a fixed depth, thus you can just do something likearray_map('unlink', glob("/tmp/nginx/*/*/*"));
. The number of*
depends on your configuration. You can keep the empty directories, they usually doesn't hurt :) -
SkarXa about 9 yearsWarning: Deleting the cache manually will cause a lot of entries in error.log, because when nginx wants to purge them they dont exist. Example [crit] 8282#0: unlink() "/etc/nginx/cache/route" failed (2: No such file or directory)
-
Matt Fletcher over 4 yearsI found I had to also restart nginx as it went completely nuts when I deleted the folder
-
Zane Claes over 3 yearsIf you're doing this with PHP, your site has a security vulnerability. The nginx and PHP user spaces should be separate. This setup would allow an attacker to potentially manipulate the cache.