php Laravel 响应 Cache-Control 标头始终包含“无缓存”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32360492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:55:36  来源:igfitidea点击:

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

phplaravelheaderresponseno-cache

提问by Riesjart

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.

出于某种原因,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;
     }
}

Even when I use a 'after-middleware' and die and dump the response, I still get this, what seems to be right to me.

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

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
}

The method $response->isCacheable() als returns true. But when I receive the response, Firebug shows the following:

方法 $response->isCacheable() 也返回 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

I use xampp, but on this same server when I just load an html-page (no Laravel/PHP), it does not send these Cache-Control headers.

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

How can I achieve that the browser does not receive the Cache-Control headers "no-store, no-cache" when I set the last-modified and expires headers?

当我设置 last-modified 和 expires 标头时,如何实现浏览器不接收 Cache-Control 标头“no-store, no-cache”?

Thanks!

谢谢!

回答by Geoff Salmon

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

我相信您的幻影缓存控制标头来自 PHP。

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

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

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

当 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

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).

几天来,我一直在为 apache 上的 laravel 中的缓存控制而苦苦挣扎:我发现在 laravel 中设置标头只是将它们附加到由 php.ini 设置的标头上。我尝试在 apache.conf 中设置一些规则,以允许缓存通过 laravel 访问的 .js 和 .css 文件,同时阻止缓存对 .php 文件的请求,但是这些规则失败了,因为 apache 将看到任何文件通过 laravel 作为 .php 文件提供(因为它是通过 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()

最后,我决定在 php.ini 中将 session.cache_limiter 设置为 ''(从而跳过 PHP 对缓存标头的处理),并将以下内容添加到 app:after() 中的 filters.php

     /*
     * 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");
    }

回答by Jan D

Although I do not know your exact configuration, I would assume that this is due to your Apache configuration, as header values can be overwritten there.

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

Have a look through all Apache configuration files and look out for lines starting with Header Set Cache-Control, e.g. Header Set Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"

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

Probably such a directive is set to affect only your PHP files, which would be the reason why other files are delivered with other headers.

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

However: Watch out when changing this. Maybe you would want this to be set for security reasons. Consider the problems with caching dynamic, authenticated content by proxies (link for detail)

但是:更改此设置时要小心。出于安全原因,您可能希望设置此项。考虑通过代理缓存动态、经过身份验证的内容问题(详细链接)