I’m having an issue with setting the Cache-Control
header in my Laravel application. No matter what I try, the browser always shows the following header:
Cache-Control: max-age=0, must-revalidate, no-cache, no-store, private
I’ve created a custom middleware called AppHttpMiddlewareSetCacheHeaders
that looks like this:
class SetCacheHeaders
{
/**
* Handle an incoming request.
*/
public function handle(Request $request, Closure $next): Response
{
$response = $next($request);
if ($response instanceof IlluminateHttpResponse) {
$response->header('Cache-Control', 'public, max-age=7200');
$response->setEtag(md5($response->getContent()));
}
return $response;
}
}
I’ve also tried using the predefined Laravel middleware:
->middleware('cache.headers:public;max_age=7200;etag');
However, in both cases, only the Etag
header is being set, and the Cache-Control
header is not being modified.
I’ve read that this issue can be caused by PHP settings, so I’ve also updated my php.ini
file to set session.cache_limiter = ''
.
This application is hosted using Laravel Forge, so I’m wondering if that could be causing any issues with the cache settings.
I also tried setting the Cache-Control header directly in the Nginx configuration, which worked, but in this case, I ended up with two Cache-Control headers, which is not ideal.
Could you please help me understand why I’m unable to control the Cache-Control
header in my Laravel application? I need to set the Back/forward cache
functionality, and this is preventing me from doing so.
Any guidance would be greatly appreciated.