My Laravel, Nginx, Cloudflare nightmare.

Laravel, nginx and cloudflare, what could go wrong?

laravel
cache
nginx
cloudflare

Published 4 years ago

2 minutes read

I've just about recovered from the headache that was caused from configuring cache for a side project recently.

The TLDR:

  • Disabled nginx cache headers
  • Handled cache control headers with Laravel
  • Changed setting to make sure Cloudflare respected my custom headers

My original setup:

  • Nginx cache expires
  • Laravel as the application
  • Cloudflare saving the cache

I have my main website which I wanted to cache for X number of hours, with some pages needing less time than others. I also had some API routes for even less time. The site in the end would also be cached by Cloudflare to reduce server origin requests.

Things I tried and failed:

  • Countless attempts at setting different expires for different location blocks within the nginx conf.
  • Setting headers in the response within Laravel controllers to having it not update at all.

Nothing was working and nothing was updating.

Giving up on handling this within nginx, I moved to see how I could just handle this within my Laravel codebase, and looked to see how Laravel handles cache control headers.

Configuring Laravel with SetCacheHeaders middleware

It turns out there's a less known middleware (not mentioned in the documentation) specifically to handle cache on routes. You can find your middlewares within your App\Http\Kernel.php and see 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class under the route middleware group.

  • This allowed me to apply custom cache headers to my route groups or endpoints Route::get('/','SiteController@index')->name('home')->middleware('cache.headers:public;max_age=1800');
  • Thinking I'd cracked it I went to check my response headers, and saw 14400 coming out of the response headers, even though my value was 1800.
  • Convinced this was actually working and something was being overwritten, I looked into what Cloudflare was doing.

Configuring Cloudflare to respect origin headers

  • Under Page Rules within Cloudflare, it's important when you're wanting to set custom cache levels that within Cloudflare you have "Origin Cache Control", switched on.
  • Also under Caching > Configuration, also make sure you have "Browser Cache TTL" set to "Respect Existing Headers".
  • If either of those are not set, Cloudflare will set its own cache level and will ignore anything you've set.

After taking cache into my own hands, and configuring Cloudflare to work with it correctly I've now got different endpoints with different levels of cache time.

Looking for a Laravel Developer on your next project?

I help agencies, startups, and individuals stand out by creating exceptional websites, mobile apps, and digital experiences. Based in Surrey, I work with clients in London and internationally, providing flexible, tailored solutions for every project.

Let's chat about your next project

Whether you need a single contractor or a full team, I collaborate with a trusted network of designers and developers. Having worked on large-scale projects, I'm equally committed to supporting businesses of all sizes, locally in Surrey, London, and internationally.

See also...

CnCNet - Website & App Development

Comprehensive design and development of responsive web and desktop apps for CnCNet, enhancing user experience and accessibility.

AR Configurator for E-Bikes

Bespoke augmented reality web configurator for E-Bikes, offering full customization options, including color choices and feature configurations.

Stock Investment App

Real-time stock investment app for web and mobile, built with TypeScript, React, and WebSockets, delivering fast, responsive, and data-driven experiences.

KickTown Football - Website

Custom-built website and API's for KickTown Football, integrating a merchandise store, booking system, and seamless user experience.

Tempest Rising - Official Website

Website development for Slipgate Ironworks' Tempest Rising, crafted to deliver a sleek, immersive experience for fans and players.

Cosmetic Visualiser - Web App

React and TypeScript-powered web app for visualizing facial cosmetic treatments, offering a cutting-edge, interactive user experience.

C&C Community Website

Command & Conquer Community platform with improved SEO, a sleek interface, and content integrations like Twitch and Steam, creating a hub for fans and creators.

React Native Health App

Custom Android and iOS health app developed with React Native and TypeScript, tailored for a health-tech startup, ensuring cross-platform compatibility.

Oriental Garden Restaurant - Website Redesign

Website redesign featuring an online ordering system that boosts sales and enhances customer engagement with a modern, intuitive interface.