257

如何为我的网站启用浏览器缓存?我是否像这样将 cache-control:public 放在我的标题中的某个地方?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
Cache-Control:public;
>

我正在使用在最新版本的 XAMPP 上开发的最新版本的 PHP。

4

8 回答 8

220

要在 HTML 中使用缓存控制,请使用元标记,例如

<meta http-equiv="Cache-control" content="public">

内容字段中的值定义为以下四个值之一。

头部的一些信息Cache-Control如下

HTTP 1.1。允许的值 = PUBLIC | 私人 | 无缓存 | 没有商店。

公共 - 可以缓存在公共共享缓存中。
Private - 只能缓存在私有缓存中。
No-Cache - 可能不被缓存。
No-Store - 可以缓存但不存档。

指令 CACHE-CONTROL:NO-CACHE 表示不应使用缓存信息,而应将请求转发到源服务器。该指令与 PRAGMA:NO-CACHE 具有相同的语义。

当无缓存请求被发送到不知道是否符合 HTTP/1.1 的服务器时,客户端应该包括 PRAGMA: NO-CACHE 和 CACHE-CONTROL: NO-CACHE。另请参阅到期。

注意:在 HTTP 中指定缓存命令可能比在 META 语句中更好,在 META 语句中它们可以影响的不仅仅是浏览器,还包括代理和其他可能缓存信息的中介。

于 2010-12-18T21:40:33.370 回答
145

您可以使用以下方法在 PHP 中设置标头

<?php
  //set headers to NOT cache a page
  header("Cache-Control: no-cache, must-revalidate"); //HTTP 1.1
  header("Pragma: no-cache"); //HTTP 1.0
  header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

  //or, if you DO want a file to cache, use:
  header("Cache-Control: max-age=2592000"); //30days (60sec * 60min * 24hours * 30days)

?>

请注意,使用的确切标头将取决于您的需求(以及是否需要支持HTTP 1.0和/或HTTP 1.1

于 2010-12-19T21:25:20.327 回答
67

正如我所写的,最好使用该文件.htaccess。但是请注意将内容留在缓存中的时间。

采用:

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

其中:604800 = 7 天

PS:这可用于重置任何标题

于 2011-10-05T15:55:24.020 回答
35

http://www.askapache.com/htaccess/apache-speed-cache-control.html上的页面建议使用如下内容:

添加缓存控制标头

这在您的根 .htaccess 文件中,但如果您可以访问 httpd.conf 那就更好了。

此代码使用 FilesMatch 指令和 Header 指令将 Cache-Control 标头添加到某些文件。

# 480 weeks
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=290304000, public"
</FilesMatch>
于 2011-05-01T11:28:50.387 回答
29

这是.htaccess我在实际网站中使用过的最好的:

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

##Tweaks##
Header set X-Frame-Options SAMEORIGIN

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##

<IfModule mod_headers.c>
    Header set Connection keep-alive
    <filesmatch "\.(ico|flv|gif|swf|eot|woff|otf|ttf|svg)$">
        Header set Cache-Control "max-age=2592000, public"
    </filesmatch>
    <filesmatch "\.(jpg|jpeg|png)$">
        Header set Cache-Control "max-age=1209600, public"
    </filesmatch>
    # css and js should use private for proxy caching https://developers.google.com/speed/docs/best-practices/caching#LeverageProxyCaching
    <filesmatch "\.(css)$">
        Header set Cache-Control "max-age=31536000, private"
    </filesmatch>
    <filesmatch "\.(js)$">
        Header set Cache-Control "max-age=1209600, private"
    </filesmatch>
    <filesMatch "\.(x?html?|php)$">
        Header set Cache-Control "max-age=600, private, must-revalidate"
      </filesMatch>
</IfModule>
于 2016-11-17T21:24:03.767 回答
16

对于 Apache 服务器,您应该检查mod_expires以设置 Expires 和 Cache-Control 标头。

或者,您可以使用Header指令自行添加 Cache-Control:

Header set Cache-Control "max-age=290304000, public"
于 2010-12-18T22:07:41.040 回答
5

元缓存控制标记允许 Web 发布者定义缓存应如何处理页面。它们包括声明什么应该是可缓存的、缓存可以存储什么、过期机制的修改以及重新验证和重新加载控制的指令。

允许的值为:

Public - 可能缓存在公共共享缓存中
Private - 可能只缓存在私有缓存中
no-Cache - 可能不缓存
no-Store - 可能缓存但不归档

请注意区分大小写。在您的网页源中添加以下元标记。标记末尾的拼写差异在于您使用“ /> = xml”或“> = html”。

    <meta http-equiv="Cache-control" content="public">
    <meta http-equiv="Cache-control" content="private">
    <meta http-equiv="Cache-control" content="no-cache">
    <meta http-equiv="Cache-control" content="no-store">

来源- >元标签

于 2012-12-05T11:25:03.540 回答
5

OWASP 建议如下,

尽可能确保缓存控制 HTTP 标头设置为 no-cache、no-store、must-revalidate、private;并且 pragma HTTP 标头设置为无缓存。

<IfModule mod_headers.c>
    Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
    Header set Pragma "no-cache"
</IfModule>
于 2015-10-22T17:47:14.340 回答