Sling Academy
Home/DevOps/How to clear NGINX cache (4 approaches)

How to clear NGINX cache (4 approaches)

Last updated: January 20, 2024

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.

Next Article: NGINX: How to Increase the Upload File Size Limit

Previous Article: NGINX Error 405: Method Not Allowed: Solutions Guide

Series: NGINX Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide