0
0
Nginxdevops~5 mins

Purging cached content in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes cached content in Nginx becomes outdated and needs to be removed so users get fresh content. Purging cached content means deleting stored files so Nginx fetches new versions from the source.
When you update a website but users still see old pages due to caching
When you fix a bug in a cached resource and want the fix to appear immediately
When you want to clear cache for a specific URL without restarting Nginx
When you want to free disk space used by old cached files
When you want to test changes to your site without cached interference
Config File - nginx.conf
nginx.conf
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=100m inactive=60m use_temp_path=off;

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_cache my_cache;
        proxy_pass http://backend_server;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
    }

    location ~ /purge(/.*) {
        allow 127.0.0.1;
        deny all;
        proxy_cache_purge my_cache $scheme$host$1;
    }
}

This configuration sets up a cache named my_cache stored in /var/cache/nginx. The location / block enables caching for normal requests. The location ~ /purge(/.*) block allows purging cached content by sending requests to URLs starting with /purge. Only localhost (127.0.0.1) can purge cache for security.

Commands
This command sends a PURGE request to Nginx to delete the cached content for the URL /path/to/cached/resource. It uses localhost because the purge location only allows local requests.
Terminal
curl -X PURGE http://localhost/purge/path/to/cached/resource
Expected OutputExpected
HTTP/1.1 200 OK Content-Type: text/plain Content-Length: 7 Purged
This command checks the headers of the resource after purging to confirm that Nginx fetches a fresh copy instead of serving cached content.
Terminal
curl -I http://localhost/path/to/cached/resource
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Thu, 01 Jun 2023 12:00:00 GMT Content-Type: text/html Content-Length: 1234 Last-Modified: Thu, 01 Jun 2023 11:59:00 GMT X-Cache-Status: MISS
-I - Fetch only HTTP headers without body
Key Concept

If you remember nothing else from this pattern, remember: purging cache deletes stored files so Nginx serves fresh content on next request.

Common Mistakes
Trying to purge cache from a remote IP not allowed in the config
Nginx denies purge requests from unauthorized IPs for security, so the purge fails.
Send purge requests only from allowed IPs like localhost or update config to allow your IP.
Using GET or POST instead of PURGE method to clear cache
Nginx only recognizes PURGE method in the purge location to delete cache.
Use the PURGE HTTP method explicitly with curl or other tools.
Not configuring proxy_cache_purge directive in nginx.conf
Without proxy_cache_purge, Nginx does not know how to remove cached files on PURGE requests.
Add proxy_cache_purge directive with correct cache zone and key.
Summary
Configure Nginx with proxy_cache_path and proxy_cache_purge to enable cache purging.
Send PURGE HTTP requests to the special purge location to delete cached content.
Verify cache is cleared by checking response headers show a cache MISS.