0
0
NginxHow-ToBeginner · 4 min read

How to Purge Cache in Nginx: Clear Cached Content Easily

To purge cache in nginx, you typically configure a cache purge method using the ngx_cache_purge module or manually delete cached files from the cache directory. Then, you can send a special HTTP request or remove cache files to clear cached content.
📐

Syntax

The basic syntax to purge cache in Nginx involves either configuring a cache purge location or manually deleting cache files.

Using ngx_cache_purge module:

  • location /purge/ { ... }: Defines a URL path to trigger cache purge.
  • proxy_cache_purge: Directive to enable purge requests.

Manual deletion: Remove cached files from the cache directory, usually specified by proxy_cache_path.

nginx
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache_zone:10m inactive=60m max_size=1g;

location /purge/ {
    allow 127.0.0.1;
    deny all;
    proxy_cache_purge my_cache_zone $scheme$proxy_host$request_uri;
}

# To manually delete cache:
# rm -rf /var/cache/nginx/*
💻

Example

This example shows how to configure Nginx with ngx_cache_purge to allow cache purging via HTTP requests from localhost.

After setup, sending a PURGE request to http://localhost/purge/your-cached-url will clear that cached content.

nginx
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache_zone:10m max_size=1g inactive=60m use_temp_path=off;

server {
    listen 80;

    location / {
        proxy_pass http://backend;
        proxy_cache my_cache_zone;
        proxy_cache_valid 200 1h;
    }

    location /purge/ {
        allow 127.0.0.1;
        deny all;
        proxy_cache_purge my_cache_zone $scheme$proxy_host$request_uri;
    }
}
Output
No direct output; sending a PURGE request to /purge/your-cached-url clears that cache entry.
⚠️

Common Pitfalls

  • Missing ngx_cache_purge module: Nginx does not support cache purging by default; you must compile or install the ngx_cache_purge module.
  • Incorrect permissions: Cache directory must be writable by Nginx user to delete files manually.
  • Wrong purge URL: The purge request URL must exactly match the cached URL.
  • Security risks: Allow purge requests only from trusted IPs to avoid unauthorized cache clearing.
nginx
## Wrong way: No purge location
location / {
    proxy_cache my_cache_zone;
}

## Right way: Add purge location
location /purge/ {
    allow 127.0.0.1;
    deny all;
    proxy_cache_purge my_cache_zone $scheme$proxy_host$request_uri;
}
📊

Quick Reference

Summary tips for purging cache in Nginx:

  • Use ngx_cache_purge module for HTTP purge requests.
  • Configure proxy_cache_path and proxy_cache properly.
  • Restrict purge access to trusted IPs.
  • Manual cache clearing requires deleting files from cache directory.
  • Reload Nginx after configuration changes with nginx -s reload.

Key Takeaways

Nginx does not support cache purging by default; use ngx_cache_purge module or manual deletion.
Configure a purge location with proper access control to safely clear cached content via HTTP.
Manual cache purge involves deleting files from the cache directory defined by proxy_cache_path.
Ensure purge URLs exactly match cached URLs to successfully clear cache entries.
Reload Nginx after changes to apply cache purge configurations.