How to Disable NGINX Cache

NGINX cache is a powerful way to speed up websites and improve web performance. It allows you to quickly serve commonly or recently requested content without sending the request to upstream server. But sometimes, you may need to disable NGINX cache. In this article, we will learn how to disable NGINX cache.

How NGINX Cache Works

NGINX is typically used as a reverse proxy along with an application server such as Apache server. When caching is enabled in NGINX, then it stores the responses of commonly received requests, as well as recently received requests. When NGINX receives a request from user’s client web browser, it will first check if its response is present in the cache. If it is present and is still valid, then it will directly return the response without sending the request to application server. If the response is not present or the stored cached response is stale, only then the request is sent to upstream server. In this case, it will update the cached response with latest information. This reduces server load by preventing unnecessary requests.

Caching is commonly used to serve static files such as CSS, JS, as well as images and videos.

Why Disable NGINX Cache

While caching is definitely useful, there are some instances when you may need to disable NGINX cache.

1. Development/Testing – In a development or testing environment, you are frequently changing inputs to see how it affects the result. In such cases, you do not want to serve cached content every time. So you will need to temporarily disable caching.
2. Malfunction or errors – Sometimes, the NGINX cache may become corrupted. In this case, it may serve stale or even wrong content. This may occur because of a bug in your program. In this case, you will need to disable caching till the issue is fixed.
3. Refresh or Flush Cache – Sometimes website administrators want to forcibly refresh or flush cache once in a while, just to make sure NGINX is not serving stale content. Here too, you will need to temporarily disable cache.
4. Specific URLs or File Types – You may not need to cache all pages on a website. In every website, there will always be certain pages such as checkout page, where you need to show latest content dynamically. For these pages, you will need to disable caching.
5. Authenticated Pages – Some web pages may be accessed by only authorized users. Such pages cannot be cached otherwise they will be available for all. In this case, you will need to disable caching for those pages.

How to Disable NGINX Cache

Here are some ways to disable cache in NGINX.

1. Disable Cache Globally

Generally, NGINX is used as a reverse proxy along with another backend server like Apache. If you want to completely disable caching across all URLs on your website, then add the following location block in your NGINX configuration file at /etc/nginx/nginx.conf or virtual host file at /etc/nginx/sites-available. Replace backend_server_ip below with the IP address or hostname of your backend server.

location / {
proxy_no_cache 1;
proxy_cache_bypass 1;
proxy_cache off;
proxy_pass http://backend_server_ip;
}

In the above code, proxy_no_cache tells NGINX not to cache response. proxy_cache_bypass forces NGINX not to lookup response in cache and send the request to backend server. proxy_caching off explicitly turns off proxy caching.

2. Disable Caching for Specific Folder

If you want to disable caching for specific folder instead of entire website, then replace ‘location /’ above with the location block pointing to the folder. Here is an example to disable caching for folder /products.

location /products {
proxy_no_cache 1;
proxy_cache_bypass 1;
proxy_cache off;
proxy_pass http://backend_server_ip;
}

3. Disable Cache for Specific File Type

Sometimes, you may want to disable caching for specific type of files such as PDF, CSS, JS, etc. In such cases, add the following location block to you NGINX configuration file.

location ~* \.(css|js|pdf)$ {
expires -1;
}

In the above code, we use expires -1 directive to immediately disable caching for css, js and pdf file types.

4. Force Client Browser to Not Cache

For development environments, you will need to prevent client browser from caching static files. For such cases, add Cache-Control and Expires header in location block.

location / {
add_header Last-Modified $date_gmt;
add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate;
if_modified_since off;
expires off;
etag off;
}

The add_header directive will set required response headers. When client web browser reads this response, it will immediately stop caching it. If you want compatibility with old HTTP headers then you need to also add ‘add_header Pragma “no-cache”‘ in above configuration.

5. Disable OS Level Caching

Sometimes NGINX file serving may conflict with OS based optimizations. In such cases, you need to disable NGINX file serving using sendfile and directio directives.

Add ‘sendfile off’ in http or server block.

Alternatively, use ‘directio 0’ in location block.

6. Disable Caching via cPanel/WHM

If your website uses cPanel or WHM to manage NGINX instance, then here are the steps to disable caching in NGINX.

Log into cPanel as root user.

Go to Home->Software->NGINX Manager.

Set ‘Use caching option by default’ to Disabled.

Clear Cache Directory

After you disable caching, you need to also clear cache directory. Otherwise, NGINX may continue to serve cached content even after caching is disabled. For this, the first step is to identify the cache directory. It is typically located at /var/cache/nginx.

Run the rm command to clear cache directory.

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

Points to Remember

Here are some important points to remember when you disable caching in NGINX.

1. Disable caching selectively for development or specific folders, instead of doing it for all URLs across your website.
2. It is advisable to use separate NGINX configurations and virtual host files for development and production environments.
3. Regularly purge cache so that is it fresh and does not serve stale content.
4. If you have set NGINX response headers to disable cache then make sure they are working as expected using Developer Tools in client web browser.
5. If cached response is still served even after you disable cache, then clear NGINX cache directories and restart server.

Conclusion

In this article, we have learnt how NGINX cache works. We have learnt why you may need to disable NGINX caching and finally we looked at the different ways to disable NGINX cache. You can use any of these methods depending on your requirement. After you disable caching, please clear the cache directory otherwise NGINX may still continue to serve cached content for some time.

Also read:
How to Fix 502 Bad Gateway Error in NGINX
How to Rewrite URL Parameters in NGINX
How to Redirect Location to Another Domain in NGINX

Leave a Reply

Your email address will not be published. Required fields are marked *