Search code examples
nginxcachingcloudflarecdn

NGINX caching and CDN


I have a website that uses Cloudflare CDN with NGINX origin server. The NGINX uses fastCGI connection to connect to PHP. The HTML pages do not change frequently. Each page can get url parameters and each set of values is considered a different page for CDN caching. It seems to me that the mostly used pages are already cached on the CDN. Is there any reason or benefit to use NGINX caching on the origin server and CDN caching?


Solution

  • First of all, Cloudflare will not cache HTML unless you have configured a "Cache Everything" page rule.

    To answer your question as it is, yes, it makes sense to have both Cloudflare CDN cache and NGINX FastCGI cache.

    In that scenario, your FastCGI cache can act as the primary cache. Cloudflare's caches are separate for each point of presence, meaning that it's not just one virtual cache.

    A visit from a different country will hit a different Cloudflare point of presence, and thus a different cache in their system. If requested page is not cached in that Cloudflare presence, then Cloudflare will make a request to your server. You will save CPU resources if Cloudflare's requests to your server are cached by FastCGI cache.

    You have to take care of the correct order of clearing your cache with two caches: clear your FastCGI cache first, and then Cloudflare's.