简体   繁体   English

什么可以将“Pragma:no-cache”添加到我的响应标头中? (阿帕奇,PHP)

[英]What could be adding “Pragma:no-cache” to my response Headers? (Apache, PHP)

I have a website which maintenance I've inherited, which is a big hairy mess.我有一个网站,我继承了它的维护,这是一个大毛茸茸的烂摊子。
One of the things I'm doing is improving performance.我正在做的一件事是提高性能。 Among other things, I'm adding Expires headers to images.除其他外,我正在向图像添加Expires标头。

Now, there are some images that are served through a PHP file, and I notice that they do have the Expires header, but they also get loaded every time.现在,有一些图像是通过 PHP 文件提供的,我注意到它们确实有Expires标头,但它们也每次都被加载。

Looking at Response Headers, I see this:查看响应标头,我看到了:

Expires Wed, 15 Jun 2011 18:11:55 GMT
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma  no-cache

Which obviously explains the problem.这显然解释了这个问题。

Now, I've looked all over the code base, and it doesn't say "pragma" anywhere.现在,我已经查看了所有代码库,它在任何地方都没有说“pragma”。 .htaccess doesn't seem to have anything related either. .htaccess 似乎也没有任何相关性。

Any ideas what could be setting those "pragma" (and "cache-control") headers, and how can I avoid it?任何想法可以设置那些“pragma”(和“cache-control”)标头,我该如何避免它?

The culprit may be php.ini, where session.cache_limiter=nocache.罪魁祸首可能是 php.ini,其中 session.cache_limiter=nocache。 Change the value to blank or public to avoid the anti-cacheing headers.将值更改为空白或公共以避免反缓存标头。

I had a similar problem with Pragma: nocache我对Pragma: nocache有类似的问题Pragma: nocache

session_cache_limiter(false); prior to session_start();session_start();之前session_start(); seemed to suppress it.似乎压制了它。

Create a simple file that includes none of your PHP libraries but lives in the same folder as the file that serves up your images through a PHP file.创建一个不包含任何 PHP 库但与通过 PHP 文件提供图像的文件位于同一文件夹中的简单文件。

file: test.php

Request this file through a browser and check the headers.通过浏览器请求此文件并检查标题。 If you see the Response headers that you don't want, you know that they're configured via apache and not generated via a PHP file and you can concentrate your searches on .htaccess file in the directory tree, and on the http.confg and other included apache config files.如果您看到不想要的响应头,您就知道它们是通过 apache 配置的,而不是通过 PHP 文件生成的,您可以将搜索集中在目录树中的 .htaccess 文件和 http.confg和其他包含的 apache 配置文件。 You'll want to search for你会想要搜索

<Directory....

and

<VirtualHost

sections that may apply to your site.可能适用于您网站的部分。

If you don't see the headers in a request for that simple PHP file, you know that PHP is setting the headers somewhere.如果您在对那个简单 PHP 文件的请求中没有看到标头,那么您就知道 PHP 正在某处设置标头。 At the end of your image serving file (or right after it echos the image and exits), but the following PHP snippet)在图像服务文件的末尾(或在它回显图像并退出之后),但以下 PHP 代码段)

var_dump(get_included_files());

Request an image through the image serving URL.通过图像服务 URL 请求图像。 That above snippet will print out all the PHP files used in the request.上面的代码段将打印出请求中使用的所有 PHP 文件。 (you'll probably need to view source or use curl to see the raw output, as the browser will report an invalid image) (您可能需要查看源代码或使用 curl 来查看原始输出,因为浏览器会报告无效图像)

Having a subset of your files to work file, search through them for calls to the将文件的子集作为工作文件,搜索它们以查找对

header();

function.功能。 The header function is the only way (I think) that raw PHP code can set Response headers. header函数是(我认为)原始 PHP 代码可以设置 Response 标头的唯一方法。 You'll also want to search for您还想搜索

call_user_func
eval
$$

in case there's any dynamic code on the page that's using PHP's meta-programming capabilities to call the header function.以防页面上有任何动态代码使用 PHP 的元编程功能来调用header函数。

Good luck!祝你好运!

Try unsetting the headers in .htaccess .尝试取消设置.htaccess的标头。 The below example will unset them for all files matching the extensions ico , jpeg , png , gif , js , css :下面的示例将为与扩展名icojpegpnggifjscss匹配的所有文件取消设置它们:

<FilesMatch "\.(ico|jpeg|png|gif|js|css)$">
    Header unset Cache-Control
    Header unset Pragma
</FilesMatch>

You can find some hints in this article .您可以在本文中找到一些提示。

I did this at runtime with this:我在运行时这样做了:

header("Pragma:");

which forced the script to unset the Pragma header.这迫使脚本取消设置 Pragma 标头。

如果它不在 .htaccess 中,则它可能在主 Apache 配置文件中 - 或其包含之一,具体取决于设置。

It's worth noting for people with similar problems that many frameworks will auto-add headers especially caching ones.对于有类似问题的人来说值得注意的是,许多框架会自动添加标头,尤其是缓存标头。 It's fairly easy to overload them either in the framework library or within your app.在框架库或应用程序中重载它们相当容易。

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

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