Skip to main content

Restricting Cache and Setting Headers

A guide to restricting cache and managing HTTP headers at Seravo. Learn how to disable caching for specific pages or file types using PHP or nginx configurations.

Updated today

In some cases, you may need to restrict server-side caching for specific files or URLs. This is particularly useful for dynamic files that change frequently, ensuring visitors always receive the latest version.

Important: Environments and Scenarios Without HTTP Caching

Before troubleshooting cache restrictions, please note that HTTP cache is automatically disabled in the following scenarios:

  • Staging environments (Shadow): To ensure developers see changes immediately during testing, caching is disabled by default.

  • WP_DEBUG Mode: If define('WP_DEBUG', true); is set in your wp-config.php, the nginx cache layer is bypassed.

  • WooCommerce Pages: Standard e-commerce pages like Cart, Checkout, and My Account are automatically excluded from the cache to ensure dynamic functionality.

  • Sessions and Cookies: If a theme or plugin calls session_start() or sets a cookie (via setcookie()), the HTTP cache is automatically disabled for that request to prevent serving private data to other users.

Using WordPress and PHP

The most flexible way to control caching in WordPress is by using the send_headers action.

Option A: Conditional Caching (via functions.php)

You can target specific pages using WordPress conditional tags (like is_page()). Add this to your theme's functions.php:

add_action('send_headers', 'seravo_disable_page_cache');
function seravo_disable_page_cache() {
if (is_page('my-dynamic-page')) {
header("Cache-Control: no-cache, no-store, must-revalidate, max-age=0");
header("Expires: Wed, 11 Jan 1984 05:00:00 GMT");
}
}

Option B: Single PHP File

For standalone PHP scripts outside of WordPress, add these headers at the very top of the file:

<?php 
header("Cache-Control: no-cache, no-store, must-revalidate, max-age=0");
header("Expires: Wed, 11 Jan 1984 05:00:00 GMT");

Web Server Configuration (nginx)

To restrict caching for static files (like .json or .pdf) or specific directories, create a custom configuration file in /data/wordpress/nginx/.

Example: Disabling cache for a specific directory

Create a file, e.g., /data/wordpress/nginx/cache-control.conf:

# Prevent caching for all files in /data-exports/ directory
location ^~ /data-exports/ {
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.

A Note on Object Cache (Redis)

Restricting the Object Cache (database queries and PHP objects) on a per-page basis is not recommended. If you need to ensure specific data is always fresh β€” for example, when displaying real-time stock levels, fluctuating exchange rates, or highly dynamic user-specific data β€” it is best to bypass the cache programmatically in your code:

// Example: Bypass cache for a specific WP_Query
$args = array(
'post_type' => 'product',
'cache_results' => false,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
);
$query = new WP_Query($args);

Testing Your Cache Settings

You can verify if your cache restrictions are working correctly using Seravo's built-in command-line tool. You can test the front page or a specific sub-page:

# Test the front page
wp-check-http-cache

# Test a specific URL
wp-check-http-cache https://example.com/my-dynamic-page/

The tool will tell you if the page was served from the cache (HIT) or if it bypassed the cache (MISS or BYPASS).

Best Practice

Whenever possible, we recommend using a short cache duration (e.g., 60 seconds) instead of disabling the cache entirely. This keeps your content fresh while still protecting the server from high traffic spikes or DoS attacks.

To set a 60-second cache duration via PHP:

header("Cache-Control: public, s-maxage=60, max-age=60");

For more information on how our caching architecture works and why short cache times are beneficial, see our guide: How Does Caching Work?

Did this answer your question?