How to Disable Caching in NGINX


how to disable caching in nginx

Sometimes, you may need to disable caching on your website because you are updating it, or because it is giving errors. Here’s how to disable caching in NGINX web server.

 

How to Disable Caching in NGINX

Here are the steps to disable caching in NGINX.

 

1. Open NGINX config file

Open NGINX config file in a text editor. You will typically find it in /etc/nginx/nginx.conf


$ sudo vim /etc/nginx/nginx.conf

 

2. Disable caching

You can easily disable caching by adding the following two lines in your location block


add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
expires off;

 

For example, if you want to disable caching all over your website,


location / {
add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
expires off;
}

 

If you want to disable caching for a specific directory (e.g /static/)


location /static/ {
add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
expires off;
}

 

If you want to disable caching for a specific file type (e.g pdf)


location ~* (.*\.pdf) {
add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
expires off;
}

 

If you want to disable caching for all .jpg files in a directory


location ~* /uploads/(.+\.jpg)$ {
add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
expires off;
}

 

3. Reload NGINX web server

Reload NGINX server to apply changes.

$ sudo service nginx reload

 

That’s it! NGINX caching will be disabled.

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!