How to Delete a Single Nginx Cache File for a Specific URL: FastCGI Cache Management


8 views

When using Nginx's FastCGI cache, files are stored in a specific directory structure based on the cache key. In your configuration:

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

The cache files are stored with MD5 hashes of the cache key. For http://example.com/, the key would be exactly that URL.

To locate the specific cache file for http://example.com/:

# Generate the MD5 hash of the cache key
echo -n "http://example.com/" | md5sum
# Sample output: d41d8cd98f00b204e9800998ecf8427e

Then navigate to the cache directory. With levels=1:2, the file will be in:

/backup/cache/8/cd/d41d8cd98f00b204e9800998ecf8427e

Once located, simply remove the file:

rm /backup/cache/8/cd/d41d8cd98f00b204e9800998ecf8427e

For frequent operations, create a script:

#!/bin/bash
URL="http://example.com/"
CACHE_KEY=$(echo -n "$URL" | md5sum | awk '{print $1}')
CACHE_DIR="/backup/cache"
FILE_PATH="$CACHE_DIR/${CACHE_KEY: -1}/${CACHE_KEY: -3:2}/$CACHE_KEY"

if [ -f "$FILE_PATH" ]; then
    rm "$FILE_PATH"
    echo "Cache for $URL deleted successfully"
else
    echo "Cache file not found for $URL"
fi

For more advanced setups, consider installing ngx_cache_purge module:

location ~ /purge(/.*) {
    fastcgi_cache_purge my-cache "$scheme://$host$1";
}

Then you can purge via HTTP request:

curl -X PURGE http://example.com/purge/

After deletion, check if Nginx creates a fresh cache:

curl -I http://example.com/
# Look for X-Cache: MISS in headers

When working with Nginx's FastCGI cache, the cache files are stored with hashed names based on your cache key configuration. In your case, the cache key is defined as:

fastcgi_cache_key "$scheme://$host$request_uri";

This means each URL generates a unique cache file based on the scheme (http/https), host, and request URI.

To find the specific cache file for http://example.com/, follow these steps:

# First, generate the MD5 hash of your cache key
echo -n "http://example.com/" | md5sum

# The output will look something like:
# d41d8cd98f00b204e9800998ecf8427e

Now navigate to your cache directory and build the path from the hash:

cd /backup/cache
ls -la d/41/d41d8cd98f00b204e9800998ecf8427e

Once you've located the file, you can remove it:

rm /backup/cache/d/41/d41d8cd98f00b204e9800998ecf8427e

For convenience, here's a bash script that automates the process:

#!/bin/bash
CACHE_PATH="/backup/cache"
URL="http://example.com/"

# Generate MD5 hash
HASH=$(echo -n $URL | md5sum | awk '{print $1}')

# Build file path
LEVEL1=${HASH:0:1}
LEVEL2=${HASH:1:2}
FILE_PATH="$CACHE_PATH/$LEVEL1/$LEVEL2/$HASH"

# Delete the file
if [ -f "$FILE_PATH" ]; then
rm "$FILE_PATH"
echo "Cache file for $URL has been deleted"
else
echo "Cache file not found"
fi

If you're using Nginx Plus, you can implement cache purging:

location / {
fastcgi_cache_purge my-cache "$scheme://$host$request_uri";
}

Then you can send a PURGE request:

curl -X PURGE http://example.com/

To avoid similar issues in the future, consider these configuration tweaks:

# Add ?nocache parameter bypass
set $skip_cache 0;
if ($query_string ~* "nocache") {
set $skip_cache 1;
}

# Add .php extension fallback
location = / {
try_files $uri $uri/ /index.php?$args;
}

This ensures your root URL won't get cached separately from index.php.