Laravel response Cache-Control headers always containing 'no-cache'

后端 未结 2 2061
小鲜肉
小鲜肉 2021-02-13 10:02

For some reason Laravel seems to be manipulating the response headers \'Cache-Control\' on the very last moment. I want to make browser caching possible.

class T         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-13 10:26

    I believe your phantom cache-control headers are coming from PHP.

    http://php.net/manual/en/function.session-cache-limiter.php

    when php.ini has session.cache_limiter set to nocache (default), PHP sets the following headers:

    Expires: Thu, 19 Nov 1981 08:52:00 GMT 
    Cache-Control: no-store,no-cache, must-revalidate, post-check=0, pre-check=0 
    Pragma: no-cache
    

    I have been struggling with cache-control in laravel on apache for a few days now: I found that setting the headers within laravel simply appended them on to the headers set by php.ini. I tried setting up some rules in apache.conf in order to allow caching of .js and .css files that were being accessed via laravel while preventing the caching of requests to .php files, but these rules failed as apache will see any file being served via laravel as a .php file (because it is being accessed via index.php).

    In the end I settled for setting session.cache_limiter to '' in php.ini (thereby skipping PHPs handling of cache headers), and adding the following to filters.php in app:after()

         /*
         * Custom cache headers for js and css files
         */
        if ($request->is('*.js') || $request->is('*.css')){
            $response->header("pragma", "private");
            $response->header("Cache-Control", " private, max-age=86400");
        } else {
            $response->header("pragma", "no-cache");
            $response->header("Cache-Control", "no-store,no-cache, must-revalidate, post-check=0, pre-check=0");
        }
    

提交回复
热议问题