Cloudflare not caching images served by PHP?

So I’ve run into a problem. The CMS I use allows specification width and height values in the url for an image and if you do it htaccess redirects to a php file which serves the resized version of the image instead of the normal sized one. (It is an internal redirect which serves proper image headers, as far as I know)

Only problem is that cloudflare doesn’t seem to cache these images and I’m not sure why.

Take the following images as an example. The first one is a url directly to the image in question. The second one is a url to a resized version of the image by PHP. Note how much slower the second one loads as cloudflare doesn’t seem to cache it. (even though the file size is far smaller)

I’m grateful for any direction. Backend is not my area of expertise. :wink:

They both have respective caching headers in their responses.

No, it won’t do by default.

:warning: PHP are supposed to be dynamic, I would personally avoid that approach for serving images as it can create cache conflict. If that is WordPress, Photon may be worth trying instead of using PHP dynamic serving.

Still want to give a try? Here you go!

  • Login to Cloudflare
  • Select domain
  • Go to page rule, create a new rule

Page rule conditions

  • Edge cache: 1 month or any value (mandatory)
  • Browser cache: 4 hours or any value

  • Deploy and test in various screen resolution, devices.

Possible problem, unless it changes query string or file URL, there may be potential cache conflict. You have been warned! :grinning:

What does above page rule?

No matter what cache control is defined (even cache-control: no-cache), just do cache. It’s aggressive mode.

Can you post direct URLs to both images? But use preformatted text so Discource doesnt download and embed it’s own copy.
If your file names end in “.php”, that would be the issue. You’ll have to set up page rules. We can guide you, but please post the urls for us to do so.

As I mentioned originally, they both appear to be cached by Cloudflare.

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