How to Force NGINX to Clear Cache

force browser to clear cache in nginx

You might have noticed that even after you update your website, sometimes it still shows old content. It may be because your web server is still serving the old, cached files instead of new ones. The solution to this problem is to simply clear the cache of your web server. Here’s how you can force NGINX to clear cache and serve new files.


How to Force NGINX to Clear Cache

Here are the steps to force NGINX to clear cache.


1. Clear /var/cache/nginx

NGINX stores its cached data at /var/cache/nginx. If you clear it, NGINX will automatically start serving latest files.

$ sudo rm -rf /var/cache/nginx/*



2. Disable Caching

You can completely disable caching by adding add_header directive in your NGINX config file

$ sudo vim /etc/nginx/nginx.conf


add_header Cache-Control no-cache;


3. Rename URL/Files

You can also invalidate the cache by simply renaming your static files to a newer version (e.g style_v0.01.css).

You can also change URL of your web page and add a redirect from your old page to new page. Here’s an example

location /old-login.php {
  rewrite ^ /new-login.php



About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!