How to clear Nginx cache

January 10, 2024 • Category - NGINX, Ubuntu • 1,343 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.

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.