Skip to main content

Cache Restrictions

Updated today

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;
}

Attn! After the change you have to restart the nginx by running the command wp-restart-nginx.

Did this answer your question?