CloudFlare + nginx = cache everything on a free plan


    Everything is great in the free version of Cloudflare (by golly a fairy tale!), But the list of cached file formats is very limited.
    Fortunately, caching everything in a row (up to 512 MB per file) can be configured in one of two steps.



    The first step is to create a Page rule for your CDN subdomain in the cloudflare panel.
    Below is an example rule for my subdomain. Heavy statics are stored there (up to 500MB per file).

    The most important line is Cache everything .
    TTL everyone puts their choice. In my case, this is static, which never changes .

    After that, any request from your subdomain will be cached.

    An example, in the form of a 7z archive of more than 400 MB.


    And the second stage is nginx.

    In the config server {}for CDN you need to add 2 lines:

    if ($args !~ ^$){
       return 404;
    }
    if ($request ~* (^.*\?.*$)){
       return 404;
    }
    

    The first one ifis Google Spreadsheet-style DDoS protection , as in the case of Cache everything, when requesting archive.7z?ver=killemmallCloudFlare, your channel will be dragged down (if you have not set bloodthirsty restrictions for CDN servers).
    Here for this, in the case of requests for files with arguments ( $args), this condition is introduced if.

    But that's not all!

    Second if: archive.7z? ! =archive.7z (for lovers of the classics <>, “unequal!”) in the case of the Cache everything option in Page rules. And this request easily slips past the first check, because it is $argsempty! It would seem okay that an archive of 400 MB in size would be requested repeatedly onetimes, the server will not drop.
    In fact, not once, but up to 42 ( forty-two ) times.
    I requested a file through a server in different countries and noticed that the file was cached for country # 1, and when I requested from country # 2, it was cached for a new one.
    A support question was asked and an answer was received: "CloudFlare has 42 PoPs, so the static file could be requested 42 times."
    Accordingly, the file with the parameter "?" (Cache everything!) Can be requested 42 more times and the file itself 41 more times at least. Total 83 requests. Accordingly, a 400 MB file is transformed into the maximum possible 33 GB of traffic during your TTL and the load on your channel from the CDN provider.

    So that there are no additional maximum possible spurious 42 requests and a second check is introduced.
    Wonderful result:


    What we get:
    +28 data centers for our universal CDN
    + colossal decrease in load on the channel
    + traffic savings

    What we lose:
    -the ability to send files over 512 MB to cache on the CDN provider (free tariff limit)
    -files for which requests contain "?" (in this case, the regularity in the second ifmust be reworked).

    We receive, but with reservations:
    + -Caching video files for distribution.You can play / share files, but you cannot rewind .

    CloudFlare , like any other caching and proxy CDN provider, is a very powerful tool, and for this tool to be better than without, you must use it correctly, even if it is not described in the official documentation. Otherwise, you run the risk of getting a negative result (the Internet is full of opinions about which CDN is bad).

    I wish you success and a great uptime!

    Also popular now: