0

I'm using Varnish on my Magento 2.4.1.

I've enabled Cloudlfare now, it works well but when I do some changes on the backend (like adding text on category pages, etc), it is not reflecting on the frontend anymore => I have to clear Varnish cache completely to make the changes appear.

I've asked my hosting company to apply this: https://support.cloudflare.com/hc/en-us/articles/200169376-Can-I-use-Cloudflare-and-Varnish-together-

They did but that doesn't make any difference.

Is it possible to make Varnish & Cloudflare to work together on M2 ?

Many thanks !

3 Answers 3

1

What are your configuration for cloudflare? Any special settings? Have you modiefied cloudflare to cache html? (its not standard)

from cloudflare documentation:

Cloudflare does not cache HTML resources automatically. This prevents us from unintentionally caching pages that often contain dynamic elements. For example, the content on certain HTML pages may change based on specific visitor characteristics, such as authentication, personalization, and shopping cart information.

In your setup with cloudflare and varnish i recommend using cloudflare für picture, js, css, etc. (the standard) and varnish for the html. This way M2 only have to invalidate varnish.

We are using also cloudflare and varnish with M2 and are quite happy with that setup, that i described. We have only defined some page rules for admin (bypass) and /wysiwyg/.thumbs/ (Ignore Query String) for better usability in the backend.

8
  • Hi, thanks for your reply. On Cloudflare, I have a pro plan, but nothing special configured here.
    – Jack
    Commented Feb 15, 2021 at 14:18
  • I've enabled basic features, have no rule to cache html
    – Jack
    Commented Feb 15, 2021 at 14:20
  • Problem is since Cloudflare is ON, Varnish doesn't invalidate the cache anymore - of there is a conflict with IP, I don't know. Did you do something to restore real IP, or something ?
    – Jack
    Commented Feb 15, 2021 at 14:24
  • this is strange, is it possible that this is an issue with how you communicate with varnish (cloudflare shouldn't interfere with that)? what are the settings there (env.php)? are you using names or public ip's for http_cache_hosts? and also use internal ip's in acl purge directive in varnish. not sure what your setup is..
    – moonhawk
    Commented Feb 15, 2021 at 15:47
  • hum, would it be possible to share your Varnish configuration here or in private ? I guess I should have something wrongly set here, but I'm not so familiar with these hosting configuration...
    – Jack
    Commented Feb 15, 2021 at 17:16
0

There is a bug report here which seems to be closed at the moment: https://github.com/magento/magento2/issues/5980

I agree with @moonhawk comment that cloudflare won't be the problem. Instead, you might be experiencing that github issue as many other people (including me).

0

In order to purge specific objects from cache, the Magento back-end sends HTTP PURGE requests which Varnish would act upon.

The problem arises when CloudFlare proxies are enabled, as these proxies silently disregard HTTP PURGE requests, and do NOT proxy them to your host.

I believe that by default, Magento will send the HTTP PURGE request to itself on its external domain name, meaning it would send the HTTP PURGE request to the CloudFlare proxies if CloudFlare is enabled for the domain.

To remediate this, you would need to ensure the HTTP PURGE request is sent directly to Varnish, using bin/magento setup:config:set --http-cache-hosts=<hostname/ip:port>, specifying where Varnish is directly available.

Some things to note:

  • Magento cannot handle certificate mismatches in this outbound request. If there is an SSL certificate presented, it will need to match for this to work.

  • The VCL generated by Magento seems to expect an X-Real-IP header when handling a HTTP PURGE request, which will not be present if you connect directly to Varnish, as this header is usually added by an upstream loadbalancer/proxy. You might need to alter your VCL for this to work.

Original:

    if (req.method == "PURGE") {
        if (!std.ip(req.http.X-Real-IP, "0.0.0.0") ~ purge) {
            return (synth(405, "Method not allowed"));
        }

(untested) edit:

    if (req.method == "PURGE") {
        if (!client.ip ~ purge) {
            return (synth(405, "Method not allowed"));
        }

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.