Clearing the cache in Nginx depends on the type of caching you have configured. Nginx can cache content in various ways, including using its built-in caching mechanisms (like proxy caching or FastCGI caching) or through third-party modules. Here’s a guide to clearing different types of caches in Nginx:
1. Clearing Proxy Cache
If you are using Nginx’s proxy caching, you can clear the cache by deleting the cached files manually.
Steps:
Locate the Cache Directory:
The cache directory is defined in the
proxy_cache_path
directive in your Nginx configuration file.proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
In this example, the cache directory is
/var/cache/nginx
.
Delete Cached Files:
Remove the cached files from the directory.
sudo rm -rf /var/cache/nginx/*
Reload Nginx:
After clearing the cache, reload Nginx to apply the changes.
sudo systemctl reload nginx
2. Clearing FastCGI Cache
If you’re using Nginx’s FastCGI caching, you can also clear the cache by deleting the cached files.
Steps:
Locate the Cache Directory:
The cache directory for FastCGI is specified in the
fastcgi_cache_path
directive.fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=fastcgi_cache:10m max_size=1g inactive=60m;
In this example, the cache directory is
/var/cache/nginx/fastcgi
.
Delete Cached Files:
Remove the cached files from the directory.
sudo rm -rf /var/cache/nginx/fastcgi/*
Reload Nginx:
After clearing the cache, reload Nginx.
sudo systemctl reload nginx
3. Clearing Cache for Specific URLs
If you want to clear the cache for specific URLs rather than the entire cache, you can use the proxy_cache_purge
directive if configured.
Steps:
- Enable Cache Purge Module:
- Ensure the
ngx_cache_purge
module is installed and enabled in your Nginx configuration.
- Ensure the
Configure Cache Purge Location:
Add a
location
block to handle cache purging.location /purge/ { allow 127.0.0.1; # Allow only localhost to purge cache deny all; proxy_cache_purge my_cache $scheme$proxy_host$request_uri; }
Purge Cache via URL:
Use a tool like
curl
to send a purge request.curl -X PURGE <http://localhost/purge/your-cached-url>
4. Clearing Cache for Static Files
If you cache static files, you may need to clear the cache by deleting the cached static files.
Steps:
- Locate Static Cache Directory:
- Identify where your static files are cached.
Delete Cached Files:
Remove cached static files as needed.
sudo rm -rf /path/to/static/cache/*
5. Reload or Restart Nginx
After clearing the cache, it's generally a good practice to reload or restart Nginx to ensure the changes take effect.
Reload Nginx:
sudo systemctl reload nginx
Restart Nginx:
sudo systemctl restart nginx
Summary
- Proxy Cache: Clear by deleting files in the cache directory specified in
proxy_cache_path
. - FastCGI Cache: Clear by deleting files in the cache directory specified in
fastcgi_cache_path
. - Specific URL Cache: Use the
proxy_cache_purge
directive if configured. - Static File Cache: Remove cached static files from the specified directory.
By following these steps, you can effectively clear the cache in Nginx to ensure that outdated or incorrect cached content is removed.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github