How to clear Nginx cache
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.
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;
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.
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
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.