3

This is a complicated subject for me because I'm brand new to server work so I will do my best to explain what I have done and what the issue is. The bottom line is that as you can see below, my percentage of cloudflare cache requests is extremely low

enter image description here

So I followed the steps at this url: https://support.cloudflare.com/hc/en-us/articles/236168808

That seems to be the reason for the few cache requests from cloudflare but when I contacted cloudflare support, they believe the reason is because when they do a test on my website, they can see the following information which is preventing cloudflare from doing cache requests

enter image description here

As you can see, it shows Cache-Control twice. The bottom one is what I did to try to fix this. The top one is obviously the issue and from my research it looks like Magento sets it in the code below

code found in vendor/magento/framework/App/Response/Http.php

public function setNoCacheHeaders()
{
    $this->setHeader('pragma', 'no-cache', true);
    $this->setHeader('cache-control', 'no-store, no-cache, must-revalidate, max-age=0', true);
    $this->setHeader('expires', $this->getExpirationHeader('-1 year'), true);
}

So with all of that being said:

What can I do to increase the percentage of cache requests through cloudflare? Am I missing something?

2 Answers 2

3
+250

The article you mention is something that was written for Magento 1.8 onwards. At the time of Magento 1, there was no full page caching solution other than varnish. For instance, this article mentions VCL and Turpentine and these were common jargons used when we were tuning Magento with Varnish.

Instead of answering to your question by explaining this article, I may attempt to describe how Cloudflare can be thought of in the answer below:

CloudFlare is a CDN that can cache media file, css/js files; This is a big percentage of your website content. This is the section caching static html content and I believe you have this covered

--> in Magento 2, caching the non-static content is done using either full page cache (default in Magento 2). Varnish is also a powerful solution but varnish integration may be a subject by itself

Now, the above describes how cloudflare handles the issues that we have grown to know with insights from the past; these issues are still relevant today and it is useful to have a system like Cloudflare to resolve these in a plug and play fashion.

But, I may complete my answer by highlighting a niche activity that Cloudflare can perform on your site whilst developers/devops may have challenges to contain the problem easily: Website can encounter DDOS attack, malicious activities. If a non genuine user reaches the server and triggers Magento framework to load for each of its request, the user can be detrimental to the network; subsequently the user experience can become poor because the server is busy loading Magento again and again.

--> To address this phenomenon of traffic hits; Cloudflare can act as a thin layer in front of the webserver; In this context, Cloudflare filters and helps to catch this traffic so that only genuine shop users visit the site

EDIT: I recognise my answer is not quite the solution you may look for. I may summarise the findings made so far:

  • CloudFlare is a CDN system and as a consequence is primarily designed to serve requests that were made before without calling the webserver.
  • Magento is a dynamic ecommerce system and as a consequence serves primarily dynamic content (event cms page are dynamic as they need to show the login status of the user for instance)
  • Finally, caching HTML content is a basic need that we'd think Magento / Cloudflare should be great at. For this matter, Varnish and Magento Full page cache solutions are both solutions available. Varnish concept is to use ESI blocks mechanisms. By doing this, some blocks in Magento can be defined as public/private/with ttl setting .. Essentially, caching a page in Magento consists in caching blocks with each page for which their caching metadata allows the Magento caching system to tell CloudFlare these are cacheable.

--> It may be important to note that blocks in Magento are by default not cached; It is possible easily to get a block to be cached by assigning a cache_lifetime property. This practice was valuable before Magento full page cache was not OOTB with Magento. It is still important to bear this in mind today even though this practice may be seen as cache micro-management method that will bring challenges to be a scalable/viable solution

class MyBlock extends \Magento\Framework\View\Element\Template
{
    public function __construct(Template\Context $context, array $data = [])
    {
        $data['cache_lifetime'] = 10;
        parent::__construct($context, $data);
    }

..

4
  • The idea would be to also efficiently cache HTML content with Cloudflare. This is possible I believe, but Magento thwarts the plan ?
    – Alex
    Commented Aug 18, 2022 at 9:08
  • @Alex I'd like to add some info to ensure whether the full page caching system you have in place works or not and subsequently whether cloudflare listens to Magento correctly or whether Magento thwarts cloudlfare.. Are you able to specify the caching solution you use: is it varnish or Magento FPC? Commented Aug 19, 2022 at 9:00
  • Magento FPC, no varnish
    – Alex
    Commented Aug 20, 2022 at 11:07
  • thanks @alex, feel free to ping me whether you have some answers after these 2 answers I have made and I'd love to see your problem resolved Commented Aug 20, 2022 at 18:11
1

My first answer does not provide a solution and rather attempts to convince that Cloudflare is not the system to look at to find this solution. This answer assumes you are using Magento built-in Full Page cache solution rather than Varnish. It tries this time to ascertain whether Magento works correctly.

I will now focus at helping you to understand whether the cache works on your Magento site. The solution consists in isolating Magento and zoom on how it behaves in a very specific part of the core codebase. The principle described below requires to validate the behaviour of the method \Magento\PageCache\Model\App\FrontController\BuiltinPlugin::aroundDispatch. To perform the test, you may either use a debug solution like xdebug or add log in this part of the system. Either way, this experiment requires a developer to execute it.

First scenario: the page is hit for the first time

  1. flush Magento cache
  2. open the browser console, load the network tab for it and filter the Doc entries enter image description here
  3. hit a url in Magento

Within the aroundDispatch method, the line $result = $this->kernel->load(); fetches a cached version for this url. If none is found, the line $result = $proceed($request); is triggered; this will lead to call \Magento\Framework\App\FrontController::dispatch and we know at this point that the page is not cached.

enter image description here

Second scenario: hit the same page another time still as a anonymous user

Within the aroundDispatch method, the line $result = $this->kernel->load(); fetches a cached version for this url; something is found and the front controller is not called this time

enter image description here

To have the HIT or MISS flag, Magento mode needs to be the developer mode. My suggestion is to perform this test in your development environment and it is indeed independant to Cloudflare.

We want to validate 2 things from this experience. The first thing is that the cache works in your environment (For instance, it is known to have modules that disable the cache everywhere in the site..). The second thing would be to validate that when you hit one url, the network tab shows this url as the only one Doc entry (I have come across Magento environment triggering several network document queries for a single url). Whilst the latter case is not impossible to work with, it can help to discover that some customisations are changing the way Magento load works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.