Cache Restrictions

In some cases it's useful to restrict the caching done on the server. For example if a file changes often and you want to make sure that the server provides always the latest version instead of a cached version. Below you'll find two examples for restricting cache on specific page or URL.

Restricting cache on PHP-page

You can add header information to a specific PHP page to force the server to emit these headers to the site visitors. You can find examples of this from PHP documentation: https://www.php.net/manual/en/function.header.php

One example from the documentation: 

<?php 
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

Restricting cache on the web server

If you're not dealing with a PHP page but with a static files in some specific directory, you can implement the restrictions in nginx-config file.

example@example:~$ cat /data/wordpress/nginx/cache-control.conf
location ^~ /static-files/ { 
   add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';   
   expires off; 
}

NOTE. After the change you have to restart the Nginx by running the command wp-restart-nginx.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us