If pages on your website keep showing old content, you may need to clear the Nginx cache. The right method depends on how Nginx is configured: it might cache responses from an application server, cache PHP responses through FastCGI, or simply pass requests through without storing anything. Clearing the wrong directory—or assuming a reload removes cached files—can waste time or cause problems.
This guide explains how to check whether Nginx is caching, find the configured cache directory, safely remove cached data, and prevent stale pages from returning. It also covers cache purging, Docker, and common troubleshooting steps.
How Nginx caching works
Nginx can store copies of responses on disk and serve them to later visitors. This is commonly used with proxy_cache for content received from an upstream server, such as a Node.js or Python application, and fastcgi_cache for responses from PHP-FPM. A cache can reduce response times and the workload on your application server.
Nginx does not necessarily cache anything just because it is installed. Caching must be enabled and configured. A configuration may define a cache zone with a directive such as proxy_cache_path or fastcgi_cache_path, then apply that zone to a server or location block.
Clearing the Nginx cache means removing the stored response files or using a configured purge feature. It is different from clearing a visitor’s browser cache. It is also different from clearing a CDN cache, such as Cloudflare’s, which may store its own copies of your content.
Check whether Nginx is caching
Before deleting files, inspect the active Nginx configuration. On many Linux systems, configuration files are in /etc/nginx, but the location may differ depending on your operating system, package, or container image.
sudo nginx -T
This prints the configuration Nginx has loaded, including files brought in with include directives. Search the output for cache-related directives:
sudo nginx -T 2>&1 | grep -E 'proxy_cache|fastcgi_cache|cache_path|proxy_cache_purge'
Look for directives such as proxy_cache_path, fastcgi_cache_path, proxy_cache, and fastcgi_cache. A configured path might look like /var/cache/nginx, but do not assume that is the path on your server. The value after keys_zone is a shared-memory zone name, not the on-disk directory.
If you cannot find cache directives, Nginx may not be responsible for the stale content. Check the application, browser, and any CDN or hosting-platform cache instead. You can also inspect response headers for clues, though headers vary by configuration. Some sites expose a header such as X-Cache: HIT or X-Proxy-Cache: HIT; others hide cache status.
Find the Nginx cache directory
The configured cache directory is usually declared in a directive like one of these:
proxy_cache_path /var/cache/nginx/proxy_cache
keys_zone=site_cache:10m
inactive=60m;
fastcgi_cache_path /var/cache/nginx/fastcgi_cache
keys_zone=php_cache:10m
inactive=60m;
In this example, the proxy cache and FastCGI cache are stored in separate directories. Your configuration may use a different path, a different cache type, or more than one cache zone. Check every relevant server and location block to understand which cache applies to the affected URL.
Do not delete the zone name or try to remove files based on a guessed path. First confirm the exact directory from the active configuration. If your configuration uses variables or includes, review the expanded output from nginx -T.
Clear the Nginx cache by removing cached files
For a straightforward cache reset, remove the contents of the configured cache directory. The following example uses a placeholder path. Replace it with the directory you verified in your configuration:
sudo find /var/cache/nginx/proxy_cache -mindepth 1 -delete
To clear a FastCGI cache, use its configured directory instead:
sudo find /var/cache/nginx/fastcgi_cache -mindepth 1 -delete
-mindepth 1 tells find to remove the directory’s contents without removing the directory itself. Keeping the directory in place helps avoid startup or permission issues. Be especially careful when copying commands: a mistaken path or broad pattern could delete unrelated files.
Alternatively, if you have confirmed the path and understand the effect, you can remove its contents with a shell glob. The find approach is often safer for handling hidden files. Avoid deleting an entire parent directory that also contains logs, runtime files, or other application data.
Nginx may be serving requests while you clear the cache. New requests can create fresh cache files immediately, so the directory may not stay empty. For a routine cache flush, removing the contents is often sufficient. For a controlled maintenance window, temporarily prevent caching or stop the relevant traffic while clearing the files—but do not stop a production service without considering availability and your operational requirements.
Check ownership and permissions
Nginx must be able to write to the cache directory after it has been cleared. The service account varies by system; common names include www-data and nginx. Check the directory before changing ownership:
ls -ld /var/cache/nginx/proxy_cache
If the directory has incorrect permissions, determine the correct service user from your Nginx configuration or operating-system package before fixing it. For example, a system using the www-data account might require:
sudo chown -R www-data:www-data /var/cache/nginx/proxy_cache
Do not apply that example blindly; using the wrong account can prevent caching or create a security problem. In many cases, deleting only the cache contents leaves the directory ownership unchanged, so no ownership adjustment is needed.
Do you need to reload or restart Nginx?
Usually, no. Removing cached files does not require an Nginx reload. A reload applies configuration changes; it is not a general command for clearing cache data. Running systemctl reload nginx alone will not reliably remove the files in the cache directory.
If you changed the configuration—for example, to disable caching temporarily—test it and then reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
The configuration test checks for syntax errors before the reload. On systems that do not use systemd, the service command may differ. A restart is more disruptive than a reload and is rarely necessary just to flush a cache.
Clear only selected cached content
Deleting a whole cache directory is simple, but it removes every cached response in that directory. This can temporarily increase application-server load as Nginx fetches content again. If you need to invalidate only one page, product, or URL, consider a cache purge mechanism rather than clearing everything.
Nginx Open Source does not provide a universal built-in HTTP purge feature for every cache configuration. Some deployments use a third-party purge module, a commercial Nginx feature, or an application-level endpoint. A purge location should be restricted so that untrusted visitors cannot erase arbitrary cached content. Follow the documentation for the specific module or platform you use.
Cache keys also matter. Nginx commonly builds a key from request details such as the scheme, host, and URI. If you purge a URL using a method that does not match the configured cache key, the old entry may remain. Test purge behavior on a staging system and confirm that the feature returns the expected result before relying on it in production.
Clear Nginx cache in Docker
In a Docker deployment, the cache may live inside the container’s writable filesystem or in a mounted volume. First identify which container is running Nginx and inspect its configuration and cache path from inside that container. For example:
docker exec -it nginx-container nginx -T
After confirming the path, you can remove its contents inside the container:
docker exec nginx-container sh -c \
'find /var/cache/nginx/proxy_cache -mindepth 1 -delete'
Replace the container name and path with your actual values. If the cache directory is a volume, deleting files inside the container removes data from the mounted volume too. If it is not persistent, recreating the container may discard its cache, but that is not a dependable substitute for understanding your deployment: bind mounts, named volumes, orchestration settings, and image entry points can change the result.
In Kubernetes or another orchestrated environment, check whether the cache is on a persistent volume and whether multiple Nginx replicas are serving traffic. Clearing one pod’s local cache will not clear caches on other pods or external CDNs.
Why the old content may still appear
If you cleared the Nginx cache but still see stale content, check the other layers in the delivery path. A browser can keep images, scripts, or pages locally. A CDN or reverse proxy may have a separate cache. Your application may also cache rendered pages or database results before Nginx receives the response.
Try a private browser window or a request with a cache-busting query string where appropriate. Compare the response from your origin with the public URL, and inspect response headers. If the origin shows new content but the public site does not, investigate the CDN, load balancer, or hosting provider. If the origin itself is stale, check the application’s own cache and deployment process.
Also review Nginx cache rules. Directives such as proxy_cache_valid, fastcgi_cache_valid, proxy_cache_bypass, and fastcgi_cache_bypass affect when responses are stored or skipped. Cache-control headers, cookies, and configured bypass conditions may change which requests are cached. Clearing files without fixing a rule that stores outdated responses can make the problem return.
Prevent stale Nginx cache problems
A clear cache policy makes future troubleshooting easier. Use cache durations that fit the content: static assets with versioned filenames can often be cached for a long time, while frequently changing HTML may need a shorter lifetime or a reliable invalidation method. Avoid caching personalized pages or responses containing private user data unless the rules have been carefully designed.
Consider adding a cache-status response header in a controlled way so administrators can distinguish a cache hit from a fresh upstream response. Do not expose sensitive internal details publicly. Document the cache path, service account, cache key, purge method, and any CDN behavior alongside your deployment instructions.
For planned releases, choose a repeatable process: update the application, invalidate the relevant cache layer if required, and verify the new response. Versioned asset names are often more reliable than repeatedly purging generic filenames because browsers and CDNs can treat each version as a distinct resource.
Conclusion
To clear the Nginx cache, first confirm that Nginx is caching and identify the exact on-disk path from the active configuration. Then remove the contents of the relevant cache directory, taking care not to delete unrelated files or change permissions unnecessarily. A reload is generally not needed unless you have changed the configuration.
If stale content remains, check browser, application, CDN, and container or replica caches as well. For recurring updates, use a deliberate cache policy and a secure, tested purge method. These steps help you clear Nginx cache safely while minimizing downtime and avoiding unnecessary load on your application.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.