How to clear NGINX cache

January 10, 2024 • Category - NGINX, Ubuntu • 2,324 views
ShareShare on FacebookShare on Twitter

Overview

One major feature of the NGINX web server is caching, which can significantly improve website performance by storing frequently accessed data and delivering it quickly to users. However, there are situations where you might need to clear the NGINX cache, such as updating content or resolving caching issues. In this guide, we'll walk you through the steps on how to clear the NGINX cache.

Unfortunately, NGINX doesn't provide a direct CLI command to delete the entire cache. So the process to delete the cache has to be done manually.

Step 1: Identify NGINX Cache Directory

The first step is to locate the NGINX cache directory on your server. The cache path is typically defined in the NGINX configuration file, often named nginx.conf or default.conf which can be found in /etc/nginx. Open the file using a text editor and search for the "proxy_cache_path" directive (or "fastcgi_cache_path" if using fastcgi). This directive specifies the location of the cache on your server.

So in the example below, the cache path is: /etc/nginx/cache

# NGINX config file - typically found in /etc/nginx/nginx.conf

# Cache path.
proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m max_size=5g inactive=60m use_temp_path=off;

# Alternatively, if using fastcgi, cache path.
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m max_size=5g inactive=60m use_temp_path=off;

Step 2: Remove Cached Files

Log in to your server via SSH and run the following command using the correct path that you retrieved in step 1:

# Clear entire cache using directory retrieved in step 1.
sudo rm -rf /etc/nginx/cache

Note: Exercise caution when using the 'rm -rf' command, as it will permanently delete files and directories without confirmation.

Step 3: Restart NGINX

After clearing the cache, it's essential to restart NGINX to apply the changes. You can do this by using the following command:

sudo service nginx restart

# or alternatively
sudo systemctl restart nginx

Conclusion

Clearing the NGINX cache is a straightforward process that ensures your web server delivers the latest content to users. Whether you're updating your website or troubleshooting caching issues, following these steps will help you effectively manage your NGINX cache.


0 Comments


Leave a Comment

Share your questions, thoughts and ideas while maintaining a considerate tone towards others, thank you.

All fields are required - your email address will not be published.