繁体   English   中英

Laravel响应Cache-Control标头始终包含'no-cache'

[英]Laravel response Cache-Control headers always containing 'no-cache'

出于某种原因,Laravel似乎在最后一刻操纵响应头“Cache-Control”。 我想使浏览器缓存成为可能。

class TestController extends Controller
{

    public function getTest()
    {
        $response = new \Illuminate\Http\Response('test', 200, array(
            'Cache-Control' => 'max-age='.(config('imagecache.lifetime')*60).', public',
            'Content-Length' => strlen('test'),
        ));

        $response->setLastModified(new \DateTime('now'));
        $response->setExpires(\Carbon\Carbon::now()->addMinutes(config('imagecache.lifetime')));

        return $response;
     }
}

即使我使用“中间件之后”并且死掉了转发响应,我仍然会得到这个,这对我来说似乎是正确的。

Response {#625 ▼
  +original: "test"
  +exception: null
  +headers: ResponseHeaderBag {#626 ▼
    #computedCacheControl: array:2 [▼
      "max-age" => "2592000"
      "public" => true
    ]
    #cookies: []
    #headerNames: array:5 [▶]
    #headers: array:5 [▼
      "cache-control" => array:1 [▼
        0 => "max-age=2592000, public"
      ]
      "content-length" => array:1 [▼
        0 => 4
      ]
      "date" => array:1 [▶]
      "last-modified" => array:1 [▼
        0 => "Sun, 16 Aug 2015 15:42:08 GMT"
      ]
      "expires" => array:1 [▶]
    ]
    #cacheControl: array:2 [▼
      "max-age" => "2592000"
      "public" => true
    ]
  }
  #content: "test"
  #version: "1.0"
  #statusCode: 200
  #statusText: "OK"
  #charset: null
}

方法$ response-> isCacheable()als返回true。 但是当我收到回复时,Firebug显示以下内容:

Cache-Control   
no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection  
Keep-Alive
Content-Type    
text/html
Date    
Sun, 16 Aug 2015 15:42:08 GMT
Expires 
Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive  
timeout=5, max=98
Pragma  
no-cache
Server  
Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.15
Transfer-Encoding   
chunked
X-Powered-By    
PHP/5.5.15

我使用xampp,但在我加载一个html页面(没有Laravel / PHP)时在同一台服务器上,它不会发送这些Cache-Control标头。

当我设置最后修改和过期的标头时,如何实现浏览器不会收到Cache-Control标头“no-store,no-cache”?

谢谢!

我相信你的幻像缓存控制头来自PHP。

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

当php.ini将session.cache_limiter设置为nocache(默认)时,PHP会设置以下标头:

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

我已经在apache的laravel中使用缓存控制几天了:我发现在laravel中设置标头只是将它们附加到php.ini设置的标头上。 我尝试在apache.conf中设置一些规则,以便允许缓存通过laravel访问的.js和.css文件,同时防止对.php文件的请求缓存,但这些规则失败,因为apache将看到任何文件正在通过laravel作为.php文件提供服务(因为它是通过index.php访问的)。

最后我决定将session.cache_limiter设置为''在php.ini中(从而跳过PHP处理缓存头),并将以下内容添加到app中的filters.php: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");
    }

虽然我不知道您的确切配置,但我认为这是由于您的Apache配置,因为可以在那里覆盖标头值。

浏览所有Apache配置文件并查看以Header Set Cache-Control开头的行,例如Header Set Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"

可能这样的指令设置为仅影响您的PHP文件,这也是其他文件与其他标头一起提供的原因。

但是:请注意更改此内容。 也许您希望出于安全原因设置此项。 考虑通过代理缓存动态的,经过身份验证的内容问题(链接详细信息)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM