什么可以添加“Pragma:no-cache”?我的回复标题? (阿帕奇、PHP)
我有一个由我继承维护的网站,这是一个大混乱。
我正在做的事情之一就是提高性能。除此之外,我还向图像添加 Expires
标头。
现在,有一些图像是通过 PHP 文件提供的,我注意到它们确实有 Expires
标头,但它们每次也会加载。
查看响应标头,我看到了这一点:
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
这显然解释了问题。
现在,我已经查看了整个代码库,并且它没有在任何地方说“pragma”。 .htaccess 似乎也没有任何相关内容。
有什么想法可以设置这些“pragma”(和“缓存控制”)标头,以及如何避免它?
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.
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.
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. .htaccess doesn't seem to have anything related either.
Any ideas what could be setting those "pragma" (and "cache-control") headers, and how can I avoid it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
罪魁祸首可能是 php.ini,其中 session.cache_limiter=nocache。将值更改为空白或公共以避免反缓存标头。
The culprit may be php.ini, where session.cache_limiter=nocache. Change the value to blank or public to avoid the anti-cacheing headers.
在
session_start();
之前,我对Pragma: nocache
session_cache_limiter(false);
也有类似的问题,似乎抑制了它。
I had a similar problem with
Pragma: nocache
session_cache_limiter(false);
prior tosession_start();
seemed to suppress it.创建一个简单的文件,该文件不包含任何 PHP 库,但与通过 PHP 文件提供图像的文件位于同一文件夹中。
通过浏览器请求该文件并检查标头。如果您看到不想要的响应标头,您就知道它们是通过 apache 配置的,而不是通过 PHP 文件生成的,您可以将搜索集中在目录树中的 .htaccess 文件和 http.confg 上和其他包含的 apache 配置文件。您需要搜索
可能
适用于您的网站的部分。
如果您在该简单 PHP 文件的请求中没有看到标头,则说明 PHP 正在某处设置标头。在图像服务文件的末尾(或在它回显图像并退出之后),但以下 PHP 片段)
通过图像服务 URL 请求图像。上面的代码片段将打印出请求中使用的所有 PHP 文件。 (您可能需要查看源代码或使用curl来查看原始输出,因为浏览器将报告无效图像)
将文件的子集设置为工作文件,在它们中搜索对函数的调用
。
header
函数是原始 PHP 代码设置响应标头的唯一方法(我认为)。您还需要搜索页面上是否存在使用 PHP 元编程功能来调用
header
函数的动态代码。祝你好运!
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.
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. You'll want to search for
and
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. At the end of your image serving file (or right after it echos the image and exits), but the following PHP snippet)
Request an image through the image serving URL. That above snippet will print out all the PHP files used in the request. (you'll probably need to view source or use curl to see the raw output, as the browser will report an invalid image)
Having a subset of your files to work file, search through them for calls to the
function. The
header
function is the only way (I think) that raw PHP code can set Response headers. You'll also want to search forin case there's any dynamic code on the page that's using PHP's meta-programming capabilities to call the
header
function.Good luck!
尝试取消
.htaccess
中的标头设置。下面的示例将为所有匹配扩展名ico
、jpeg
、png
、gif
、的文件取消设置它们>js
、css
:您可以在 this 中找到一些提示文章。
Try unsetting the headers in
.htaccess
. The below example will unset them for all files matching the extensionsico
,jpeg
,png
,gif
,js
,css
:You can find some hints in this article.
我在运行时使用以下命令执行此操作:
这强制脚本取消设置 Pragma 标头。
I did this at runtime with this:
which forced the script to unset the Pragma header.
如果它不在 .htaccess 中,它可能位于主 Apache 配置文件中 - 或其包含文件之一,具体取决于设置。
If it's not in .htaccess it may be in the main Apache config file - or one of its includes, depending on the setup.
对于有类似问题的人来说,值得注意的是,许多框架会自动添加标头,尤其是缓存标头。在框架库或应用程序中重载它们相当容易。
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.