How to delete/clear Nginx cache?

Nginx is a powerful webserver which can act as a reverse proxy for caching static pages. Keeping Nginx in the frontend will reduce the load on the backend servers as frequent requests will be served by Nginx cache. In this article we will see how to clear Nginx cache. 

1. Clear Nginx Proxy cache

Nginx cache the responses from Backend in the cache folder, by default it is “/var/nginx/cache/”

You can clear nginx cache by deleting the files under “/var/nginx/cache/”

1 2 3   rm rf /var/nginx/cache/*  

There is no need to restart nginx server after this. Make sure you are NOT deleting the “cache” folder itself. 

2. Cleaning the browser cache by header input

Whenever nginx serves static contents, browsers store this responses in its cache. Let us see how to force browser to invalidate the cached files after x number of days

1 2 3 4 5 6   location ~* ^.+\.(css|js|jpg|gif|png|txt|ico|swf|xml)$ { root /usr/local/nginx/htdocs; expires modified +2d; }  

Add the above config block in the nginx configuration. By this browser will invalidate the cache after 1 day  and fetch the content from the server. This is helpful when we modify the content often on your website.  

Restart nginx webserver after making the above changes.

1 2 3 4 5 6   [root@test.test.com nginx1.11.9]# /etc/init.d/nginx restart Stopping nginx: [ OK ] Starting nginx: [ OK ] [root@test.test.com nginx1.11.9]#  

Or Using systemctl

1 2 3   systemctl restart nginx  

 

3. Disable “sendfile”

When you use nginx webserver to serve locally stored static files, sendfile is totally essential to speed your Web server. But when you use it as app server to serve dynamic requests using fcgi or as a proxy server, keep it disabled

SUMMARY

In this article we explained how to clear nginx cache at different stages, on server and in the browser.

As always, feel free to drop us a note if you have any questions or feedback using our comment form below. Always Happy to help ???? 

 
Author: , 0000-00-00