1

I'm using Gandi.net's hosting service and quite recently they started doing something alarming. They are caching the output of PHP programs, apparently to avoid running the PHP code, and sending that whenever user(s) at a particular IP address request the page. As a result, I can fetch a page in Firefox e.g. one that prints my user agent, and it looks fine but if I put that URL into Chrome or Safari or even fetch the page with Curl, I will see the output that I saw in Firefox. This runs 180 degrees contrary to how I thought web servers are supposed to operate. After all, PHP is a programming language and it can output different HTML from second to second. Who are they to presume it will always output the same data and cache it? My PHP files have the .php extension by the way.

Is there a way around this caching mechanism e.g. can I tell PHP to put something in the header that will tell the cache to not cache the HTML?

2 Answers 2

3

All request to Gandi Simple Hosting instances go through Varnish cache system, so it's not your web server that is caching the responses, but this documented caching feature of your hosting service.

Simple Hosting instances and Web Accelerators benefit from a powerful cache system powered by Varnish. This allows you to distribute the content of your website to a larger number of visitors without using the resources of your instance or server.

This documentation suggest using Cache-Control: max-age=1, but Cache-Control: no-cache and even Cache-Control: private (intended for a single user) should be fine. (See Cache-Control.)

For example, in php:

header("Cache-Control: max-age=1");

If you don't want to add it to every PHP script separately, you can use the .htaccess solution provided, which also works in server config, virtual host, and directory context:

Example for an .htaccess file if it is a static website:

Header add Cache-Control "max-age=1"

For completeness, here's an example for Nginx, limited to .php files:

location ~* \.php$ {
    add_header Cache-Control "no-cache";
}
0

I found an answer. Let it be known far and wide to all people, short and tall, skinny and fat, there is a solution.

 header ('Cache-Control: no-cache');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .