How to Disable Cache in Apache Server

Apache is a powerful web server used by millions of high traffic websites and blogs. It provides a robust caching service that is commonly used by websites to serve commonly requested content fast. However, sometimes you may need to disable this caching service. In this article, we will learn how to disable cache in Apache server.

How Apache Cache Works

When cache is enabled in Apache, then it stores recent responses in memory(cache) for sometime. When a web browser sends request to Apache server, it will check if this request was recently received. It does so by check if the request’s response is present in the cache. If that is also true, then Apache will simply return the cached data, instead of actually processing the request. This reduces server load by preventing unnecessary processing of requests.

One of the common uses of caching is serving of web site’s home page, which is frequently requested and doesn’t change much over time.

Why Disable Apache Cache

There are several reasons why you may need to disable Apache cache. Let us look at the most common ones.

  1. Errors/Malfunction – Sometimes, though rare, Apache cache may start malfunctioning and serve stale or wrongly cached content. This can happen if your cache itself has been corrupted. It can also happen, if you are programmatically overwriting Apache cache content and there is a bug in your program. So you will need to disable caching service to figure what’s going on.
  2. Development/Testing – If you are running your website or application in a testing/development environment then you will be frequently changing your code and input data. In such cases, you may not want Apache to serve same old cached content, even after making changes to your files. In such cases, you will need to turn off Apache cache.
  3. Dynamic Content – Some of the URLs on your website may be serving dynamic content such as stock market data, or charts. If you run an ecommerce site, then the shopping cart and checkout pages need to be dynamic and not served from cache, since they change frequently. So even if most pages on your website are cached, there may be specific URLs where you need to turn off caching.
  4. Refresh or Flush Cache – If you run a large ecommerce or news site that has tons of cached content, then you may need to refresh or flush cache once in a while to ensure it is not stale. For example, the client browsers may continue to serve old content such as old CSS, JS pages, even after you have updated them. In such cases, you will need to force the browser to serve latest content by disabling server cache.
  5. Security Access – If there are resources on your website, that are meant to be accessed by only authorized users, then such URLs should not be cached. Otherwise, even unauthorized users will be able to access them. On a different note, sometimes attackers may even poison your website’s cache. In such cases, you may need to disable caching in Apache.

How to Disable Apache Cache

There are different ways to disable caching in Apache. Each has a different purpose. Pick the one according to your requirement.

1. Disable Caching Globally

In this approach, we comment out the caching module directly in Apache configuration file. This will disable caching on all virtual hosts, directories, files and URLs on all websites that are running on the server. Use this method only if you want to disable caching across all sites.

For this, open Apache configuration file in a text editor.

sudo vi /etc/apache2/apache2.conf
OR
sudo vi /etc/httpd/httpd.conf

Comment the following lines, by adding # at its beginning. They basically load caching modules whenever you start or reboot your Apache server. This will disable caching.

#LoadModule cache_module modules/mod_cache.so
#LoadModule cache_disk_module modules/mod_cache_disk.so
#LoadModule cache_mem_module modules/mod_cache_mem.so

Restart Apache server to apply changes.

sudo service apache2 restart

2. Using CacheDisable or .htaccess

In this method, you can disable caching for a particular virtual host, directory or location by placing ‘CacheDisable on’ directive in <VirtualHost>,<Directory>,<Location> tags respectively. Here is an example to disable cache on a single Virtual Host and not all the ones running on your server.

<VirtualHost *:80>
DocumentRoot /var/www/html
CacheDisable on
...
</VirtualHost>

You can also add this directive in the .htaccess file. In this case, it will disable caching for all locations in that directory and its sub directories.

Save and close your Virtual Host Configuration or .htaccess file and restart Apache server.

This is a very powerful method to disable cache since it allows you to precisely disable caching for one or more URLs, files, directories or virtual hosts.

3. Force Client Browser to Not Cache

Sometimes, even after you disable caching on server-side, some of the client browsers may retain cached content. This happens, if you change file content but continue to serve the same file URL in web page.

For this purpose, we set specific HTTP response headers, which force the web browsers to request non-cached, fresh content. Also, it instructs the browser to not store the response content in local cache.

Add the following lines to main configuration file or .htaccess file.

<FilesMatch "\.(html|htm|js|css|php)$">
FileETag None
Header Unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Fri, 19 Dec 2025 05:00:00 GMT"
</FilesMatch>

Let us look at the above code in detail. First, the FilesMatch directive looks for HTML, HTM, JS, CSS, PHP files only. For matching requests, we set 4 response headers that tell the web browser not to use locally cached content but use the server response data instead. Sometimes, the users may need to clear their browser cache to apply these changes.

On the server side, you need to restart server to implement these changes.

Points to Remember

  1. If you need to disable caching across all URLs on all virtual hosts, use solution #1.
  2. If you need to disable caching only for certain URLs, documents, directories or virtual hosts, then use solution #2
  3. If you need to force client browsers to discard local cache and use server response instead, then use solution #3,

Conclusion

In this article, we have learnt how Apache cache works, why you may need to disable it and how to disable it. We have covered different ways to disabling caching in Apache server. We learnt how to globally disable cache for entire website. We learnt how to disable caching for specific URLs or VirtualHosts by using CacheDisable or .htaccess file. You can use the appropriate method as per your requirement.

Also read:
How to Restrict Access by IP in Apache Server
How to Upgrade Apache Version
How to Increase Max URL Length in Apache

Leave a Reply

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