What Cache Headers should you use on your PHP site with CloudFlare so that "Always Online" works and your images get cached by their CDN?
CloudFlare's documentation is atrocious, and their support team doesn't seem to know.
The Cache-Control header is set to public and max-age is greater than 0. Note that Cloudflare does cache the resource even if there is no Cache-Control header based on status codes.
Cloudflare only caches resources within the Cloudflare data center that serve the request. Cloudflare does not cache off-site or third-party resources, such as Facebook or Flickr, or content hosted on unproxied (grey-clouded) DNS records.
Always Online™ is a feature that caches a static version of your pages in case your server goes offline. If your origin server is ever unavailable, Cloudflare will serve a limited copy of your cached website to keep it online for your visitors.
You definitely don't want to have no-cache and private headers, if you want Always Online (and a lot of our caching) to work properly.
"your images get cached by their CDN?" We automatically cache on-site resources by file extension (we wouldn't cache off-site resources, obviously, such as Flickr or Facebook widgets).
Note: Always Online does have some limitations & all of those limitations are outlined in our documentation (server response codes, etc.). If you only recently added the site to CloudFlare as well, then there would be nothing really in cache to display (the Always Online crawler goes out at specific times).
A future iteration of Always Online will allow users to "pin" specific pages, which will (hopefully) get the few limitations we have right now out of the picture.
Usually, you don’t need to do anything – most “normal” web servers will serve static content with the appropriate static cache headers. However, if you are specifying your own or for some reason your server is sending headers that cause static content to not be cached, you will want to override or remove any header which is preventing the content from being cached.
Use a program that can see the headers of your content (the “Net” tab of Firebug in Firefox, the website http://web-sniffer.net/ or similar) to view what headers your content currently has. Check for things such as “Cache-Control” and “Expires”. If content has a cache control header preventing caching or has already expired, this will probably prevent Cloudflare (and any other cache) from caching it, and you will need to remove or change them.
As a very general rule, to allow something to be cached, specify an Expires time of something in the future, and a Cache-Control of public with a max-age=some time in the future, however the headers you send will depend on what you’re serving, what server you’re using, what the client is, etc.
The easiest way without learning and understanding the various cache headers is probably to use something like http://web-sniffer.net/ to look at some other content that is cached, and copy its headers. For example, http://web-sniffer.net/?url=ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js
Note that Always Online usually does not work, in my experience.
// set the most basic caching headers in PHP (cache for 1 month)
header('Cache-Control: public');
header('Expires: '.gmdate('D, d M Y H:i:s', strtotime('+1 month')).' GMT');
See http://hardanswers.net/dynamic-webpage-caching for a brief explanation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With