什么可以在我的回复标题中添加“Pragma:no-cache”? (Apache,PHP)

时间:2021-07-07 03:52:33

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. .htaccess doesn't seem to have anything related either.

现在,我已经查看了整个代码库,并没有在任何地方说“pragma”。 .htaccess似乎没有任何相关的东西。

Any ideas what could be setting those "pragma" (and "cache-control") headers, and how can I avoid it?

有什么想法可以设置那些“pragma”(和“缓存控制”)标题,我该如何避免它?

8 个解决方案

#1


9  

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. You'll want to search for

通过浏览器请求此文件并检查标头。如果您看到不需要的响应头,您知道它们是通过apache配置的,而不是通过PHP文件生成的,您可以将搜索集中在目录树中的.htaccess文件和http.confg上。和其他包括apache配置文件。你想要搜索

<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. At the end of your image serving file (or right after it echos the image and exits), but the following PHP snippet)

如果在请求该简单PHP文件时没有看到标题,您就知道PHP正在某处设置标题。在您的图像服务文件的末尾(或在它回显图像并退出之后),但以下PHP代码段)

var_dump(get_included_files());

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)

通过图像服务URL请求图像。上面的代码片段将打印出请求中使用的所有PHP文件。 (您可能需要查看源或使用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. You'll also want to search for

功能。头函数是(我认为)原始PHP代码可以设置响应头的唯一方法。你也想搜索

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的元编程功能来调用头函数。

Good luck!

祝你好运!

#2


27  

The culprit may be php.ini, where session.cache_limiter=nocache. Change the value to blank or public to avoid the anti-cacheing headers.

罪魁祸首可能是php.ini,其中session.cache_limiter = nocache。将值更改为空白或公共以避免反缓存标头。

#3


11  

I had a similar problem with Pragma: nocache

我和Pragma有类似的问题:nocache

session_cache_limiter(false); prior to session_start(); seemed to suppress it.

session_cache_limiter(假);在session_start()之前;似乎压制了它。

#4


6  

Try unsetting the headers in .htaccess. The below example will unset them for all files matching the extensions ico, jpeg, png, gif, js, css:

尝试在.htaccess中取消设置标题。以下示例将为与扩展名ico,jpeg,png,gif,js,css匹配的所有文件取消设置:

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

You can find some hints in this article.

您可以在本文中找到一些提示。

#5


3  

I did this at runtime with this:

我在运行时用这个做了这个:

header("Pragma:");

which forced the script to unset the Pragma header.

这迫使脚本取消设置Pragma标头。

#6


1  

If it's not in .htaccess it may be in the main Apache config file - or one of its includes, depending on the setup.

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

#7


0  

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.

值得注意的是,对于有类似问题的人来说,许多框架会自动添加标头,特别是缓存标头。在框架库或应用程序中重载它们相当容易。

#8


-1  

Run grep -R pragma . to look through the files.

运行grep -R pragma。查看文件。

#1


9  

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. You'll want to search for

通过浏览器请求此文件并检查标头。如果您看到不需要的响应头,您知道它们是通过apache配置的,而不是通过PHP文件生成的,您可以将搜索集中在目录树中的.htaccess文件和http.confg上。和其他包括apache配置文件。你想要搜索

<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. At the end of your image serving file (or right after it echos the image and exits), but the following PHP snippet)

如果在请求该简单PHP文件时没有看到标题,您就知道PHP正在某处设置标题。在您的图像服务文件的末尾(或在它回显图像并退出之后),但以下PHP代码段)

var_dump(get_included_files());

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)

通过图像服务URL请求图像。上面的代码片段将打印出请求中使用的所有PHP文件。 (您可能需要查看源或使用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. You'll also want to search for

功能。头函数是(我认为)原始PHP代码可以设置响应头的唯一方法。你也想搜索

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的元编程功能来调用头函数。

Good luck!

祝你好运!

#2


27  

The culprit may be php.ini, where session.cache_limiter=nocache. Change the value to blank or public to avoid the anti-cacheing headers.

罪魁祸首可能是php.ini,其中session.cache_limiter = nocache。将值更改为空白或公共以避免反缓存标头。

#3


11  

I had a similar problem with Pragma: nocache

我和Pragma有类似的问题:nocache

session_cache_limiter(false); prior to session_start(); seemed to suppress it.

session_cache_limiter(假);在session_start()之前;似乎压制了它。

#4


6  

Try unsetting the headers in .htaccess. The below example will unset them for all files matching the extensions ico, jpeg, png, gif, js, css:

尝试在.htaccess中取消设置标题。以下示例将为与扩展名ico,jpeg,png,gif,js,css匹配的所有文件取消设置:

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

You can find some hints in this article.

您可以在本文中找到一些提示。

#5


3  

I did this at runtime with this:

我在运行时用这个做了这个:

header("Pragma:");

which forced the script to unset the Pragma header.

这迫使脚本取消设置Pragma标头。

#6


1  

If it's not in .htaccess it may be in the main Apache config file - or one of its includes, depending on the setup.

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

#7


0  

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.

值得注意的是,对于有类似问题的人来说,许多框架会自动添加标头,特别是缓存标头。在框架库或应用程序中重载它们相当容易。

#8


-1  

Run grep -R pragma . to look through the files.

运行grep -R pragma。查看文件。