Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloudflare exclusions do not appear to be working 4.7.1, craft 4.5.5 #580

Closed
joepagan opened this issue Nov 8, 2023 · 8 comments
Closed
Labels
question Further information is requested

Comments

@joepagan
Copy link

joepagan commented Nov 8, 2023

Hey,

I have just migrated an app to another host. This used to work on the old host so not sure if that may play a part but, it looks like the excluded URIs are no longer honoured and I have had to setup a page rule in Cloudflare to explicitly ignore a checkout uri pattern.

It looked like we had a temp issue where blitz not being able to connect with cloudflare in the blitz settings area but that appears to have gone for now.

Do you have any ideas about what this could be?

@joepagan joepagan added the question Further information is requested label Nov 8, 2023
@bencroker
Copy link
Collaborator

Can you share your URI pattern settings and what specifically is not working?

@joepagan
Copy link
Author

joepagan commented Nov 8, 2023

Visiting a uri such as booking/checkout/extras will be cached on cloudflare.

Here is our blitz config:

<?php

use craft\helpers\App;

$env = App::env('ENVIRONMENT');

return array_merge(
    [
        // With this setting enabled, Blitz will log detailed messages to `storage/logs/blitz.log`.
        'debug' => $env !== 'production',

        // With this setting enabled, Blitz will begin caching pages according to the included/excluded URI patterns. Disable this setting to prevent Blitz from caching any new pages.
        'cachingEnabled' => $env !== 'dev',

        // Determines when and how the cache should be refreshed.
        // - `0`: Expire the cache, regenerate manually or organically
        // - `1`: Clear the cache, regenerate manually or organically
        // - `2`: Expire the cache and regenerate in a queue job
        // - `3`: Clear the cache and regenerate in a queue job
        'refreshMode' => 1,

        // The URI patterns to include in caching. Set `siteId` to a blank string to indicate all sites.
        'includedUriPatterns' => [
            [
                'siteId' => 1,
                'uriPattern' => '.*',
            ],
        ],

        // The URI patterns to exclude from caching (overrides any matching patterns to include). Set `siteId` to a blank string to indicate all sites.
        'excludedUriPatterns' => [
            [
                'siteId' => 1,
                'uriPattern' => 'booking/.*',
            ],
            [
                'siteId' => 1,
                'uriPattern' => 'feed/.*',
            ],
            [
                'siteId' => 1,
                'uriPattern' => 'preview-holiday-listing',
            ],
        ],

        // The storage type to use.
        'cacheStorageType' => 'putyourlightson\blitz\drivers\storage\YiiCacheStorage',

        // The maximum length of URIs that may be cached. Increasing this value requires manually updating the limit in the `uri` column of the `blitz_caches` database table. Note that the prefix length limit is 3072 bytes for InnoDB tables that use the DYNAMIC or COMPRESSED row format. Assuming a `utf8mb4` character set and a maximum of 4 bytes for each character, this is 768 characters.
        // https://dev.mysql.com/doc/refman/8.0/en/column-indexes.html#column-indexes-prefix
        // Warning: if using the File Storage driver, this value should not exceed 255 unless using a file system that supports longer file names.
        // https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits
        'maxUriLength' => 600,

        // Whether URLs with query strings should be cached and how.
        // - `0`: Do not cache URLs with query strings
        // - `1`: Cache URLs with query strings as unique pages
        // - `2`: Cache URLs with query strings as the same page
        'queryStringCaching' => 1,

        // The query string parameters to exclude when determining if and how a page should be cached (regular expressions may be used).
        'excludedQueryStringParams' => [
            [
                'siteId' => '1',
                'queryStringParam' => 'success',
            ],
            [
                'siteId' => '1',
                'queryStringParam' => 'noblitz',
            ],
        ],

        // Whether pages containing query string parameters should be generated.
        'generatePagesWithQueryStringParams' => true,

        // Whether the cache should automatically be refreshed after a global set is updated.
        'refreshCacheAutomaticallyForGlobals' => false,

        // Whether the cache should be refreshed when an element is moved within a structure.
        'refreshCacheWhenElementMovedInStructure' => false,

        // Whether the cache should be refreshed when an element is saved but unchanged.
        'refreshCacheWhenElementSavedUnchanged' => false,

        // Whether the cache should be refreshed when an element is saved but not live.
        'refreshCacheWhenElementSavedNotLive' => false,

        // Whether an `X-Powered-By: Blitz` header should be sent.
        'sendPoweredByHeader' => false,
    ],
    $env === 'dev'
    ? []
    // Production & Staging - Cloudflare
    : [
        // The generator type to use.
        'cacheGeneratorType' => 'putyourlightson\blitz\drivers\generators\HttpGenerator',

        // The generator settings.
        'cacheGeneratorSettings' => ['concurrency' => 3],

        // The purger type to use.
        'cachePurgerType' => 'putyourlightson\blitz\drivers\purgers\CloudflarePurger',

        // The purger settings (zone ID keys are site UIDs).
        'cachePurgerSettings' => [
            'zoneIds' => [
                // This key is the "siteuid" assigned to the chosen site in the `sites` database table
                'xxx-xxx-xxx-xxx-xxx' => [
                    'zoneId' => App::parseEnv('$CLOUDFLARE_ZONE_ID'),
                ],
            ],
            'authenticationMethod' => 'apiKey',
            'email' => App::parseEnv('$CLOUDFLARE_EMAIL'),
            'apiKey' => App::parseEnv('$CLOUDFLARE_API_KEY'),
        ],
    ],
);

@bencroker
Copy link
Collaborator

bencroker commented Nov 8, 2023

Is the page being cached on Cloudflare only (and not by Blitz)? If that’s the case then perhaps “Browser Cache TTL” is not set to ​“Respect Existing Headers”? See the docs at https://putyourlightson.com/plugins/blitz#cloudflare-page-rules

@joepagan
Copy link
Author

joepagan commented Nov 8, 2023

It worked on the prior host, it's just since migrating to servd that doesn't seem to be working.

Had this set for 2 years
image

Set the page rule for my exclusion temporarily, but never needed this previously
image

Their support suggested they had a look over your source code and they were not sure how it works with reverse proxies now, used to use cache-control and s-maxage to control that but doesn't seem to do that any more. Only reference to cache control header is set to a fixed value which is set in the blitz config file.

@bencroker
Copy link
Collaborator

Can you send me a URL to the page in question, either here or via email at support@putyourlightson.com?

@joepagan
Copy link
Author

joepagan commented Nov 8, 2023

Just emailed you, thanks!

@bencroker
Copy link
Collaborator

bencroker commented Nov 13, 2023

Thanks for your patience with this. The next version of Blitz adds a new defaultCacheControlHeader config setting that is sent by default if no other cache headers are sent. This will ensure that pages excluded from caching are sent with no-cache in the cache control header and therefore never cached on reverse proxies.

You can test this by running composer require "putyourlightson/craft-blitz:dev-develop as 4.8.0".

@bencroker
Copy link
Collaborator

Released in 4.8.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants