Couldflare won't cache page via CURL request

caching
wordpress

#1

I’m writing a simple caching plugin for our site and I’d like to use CURL to preload the cache. If I access the system via a browser, the caching works fine. But my CURL requests are not being cached. I have provided the details of my trials on stack overflow, but I’ll repeat some of them here:

One.php makes two separate CURL requests to two.php . I want Cloudflare to cache the first response, and to serve that cache in the second response. But for some reason, Cloudflare is not caching either response. If I then visit two.php using my browser, it’s still not cached. If I then refresh two.php in my browser, I finally receive a cache HIT. How do I trigger caching on Couldflare via CURL requests?

Bare Bones Test:

./one.php

header("Cache-Control: no-store");
$url = "http://www.example.com/two.php";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_exec($ch); // prints current timestamp, see two.php
curl_close($ch);

sleep(1);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_exec($ch); // prints current timestamp, see two.php
curl_close($ch);

./two.php

header( 'Cache-Control: public, max-age=10' );
print( time()."<br>\r\n" );

#2

Unless you have a page rule with “Cache Everything”, Cloudflare will only cache the following file extensions:

This means unless the PHP script is being called as example.com/fake.js, Cloudflare won’t cache the response because it isn’t static content. Set up a page rule for your URL and set it to “Cache Everything” and then it’ll respect the Cache-Control header for non-static content.


#3

I have enabled “Cache Everything” with the duration set to respect existing headers. As I said, I do receive a cache HIT when I access two.php via the brwoser, but not when using CURL.


#4

I tried setting the User-Agent header and using Cloudflare’s cfduid cookie. I tried issuing the CURL requests on and between two different websites on two different servers. I tried HTTP and HTTPS. I tried running one.php from direct.example.com (which bypasses cloudflare).

I also have an open bounty for this on Stack Exchange. Any insight would be appreciated.


#5

This topic was automatically closed after 14 days. New replies are no longer allowed.