How to clear NGINX cache (4 approaches)

Updated: January 20, 2024 By: Guest Contributor Post a comment

Introduction

NGINX is a popular web server used for delivering web content with increased speed and efficiency. Often it is configured as a reverse proxy with caching enabled to reduce latency and load on application servers. However, in development or in response to stale content, you may need to clear the NGINX cache. This tutorial will guide you through multiple methods to achieve this.

Understanding NGINX Cache

Before clearing the cache, it’s important to understand that NGINX can cache data in two ways: fastcgi_cache for dynamic content (PHP), and proxy_cache for static and dynamic content served from an upstream server. The cache files are usually stored in a directory defined in the NGINX configuration file.

Method 1: Delete Cache Files from the Filesystem

This is the most direct method. It involves going to the directory where NGINX stores its cache files and manually deleting them.

# Identify the cache directory
echo "
$(grep -r 'proxy_cache_path' /etc/nginx/ | awk '{print $2}')
"

# Navigate to the cache directory (e.g., /var/cache/nginx)
cd /var/cache/nginx

# Caution! This command deletes all files in the cache directory!rm -rf *

After deletion, no restart of NGINX is usually necessary, as it will automatically start writing new cache files.

Method 2: Using NGINX cache purging modules

Some NGINX versions come with cache purging modules, like ngx_cache_purge, which can be configured to clear the cache for specific URLs.

First, you’ll need to install the module:

# For Ubuntu/Debian-based systems
sudo apt-get install nginx-extras

Then, update your NGINX configuration:

location ~ /purge(/.*) {
    allow 127.0.0.1;
    # IP of the server allowed to purge the cache
deny all;
fastcgi_cache_purge MYCACHE $scheme$host$1$is_args$args;
}

To purge a URL, simply send a PURGE request:

curl -X PURGE -D - http://your-domain.com/purge/path/to/purge

Method 3: Using NGINX Reload or Restart

Another way is to clear cache using ‘nginx -s reload’ which sends a signal to NGINX to reload configurations, potentially including cache settings.

sudo nginx -s reload

Keep in mind that simply reloading NGINX will not always clear existing cache files, and sometimes a restart of the NGINX service might be necessary.

sudo service nginx restart

However, this could interrupt active connections to your server and should be performed with caution.

Method 4: Configuring Cache Lifespan

Rather than clearing the cache manually, you can configure NGINX to do it automatically by setting an appropriate cache lifetime.

Here’s how you configure cache validity in your NGINX configuration file:

location ~* \.?php$ {
    etag off;
    add_header Last-Modified "";
    add_header Cache-Control "no-cache, no-store, must-revalidate";
    expire -1;
fastcgi_no_cache 1;
fastcgi_cache_bypass 1;
fastcgi_expire 0;
}

In this configuration, we are effectively telling NGINX not to cache PHP files at all. You could alternatively set a specific expiry time using the ‘expires’ directive.

Conclusion

This guide covered several methods of clearing the NGINX cache. Depending on your workflow and configuration, some methods might be more suitable than others. Regular cache management is crucial for making sure users receive the most up-to-date content without unnecessary waiting times, so implementing a suitable cache strategy and knowing how to clear your cache efficiently are important skills to have as a web administrator or developer.

Please note that this guide assumes a certain level of familiarity with the command line, NGINX configuration, and system administration. Always back up configurations before making changes and be cautious when deleting files from the system. Clearing NGINX cache can be a powerful tool but used improperly, it can also lead to downtime or loss of data.