如何为我的网站启用浏览器缓存?我只需像这样在头部中放置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。
如何为我的网站启用浏览器缓存?我只需像这样在头部中放置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。
要在HTML中使用缓存控制,可以使用meta标签,例如:
<meta http-equiv="Cache-control" content="public">
内容字段中的值被定义为以下四个值之一。
Cache-Control
头的一些信息如下所示:
HTTP 1.1。允许的值 = PUBLIC | PRIVATE | NO-CACHE | NO-STORE。
Public - 可以在公共共享缓存中缓存。
Private - 只能在私人缓存中缓存。
No-Cache - 不能被缓存。
No-Store - 可以被缓存但不会被存档。CACHE-CONTROL:NO-CACHE 指令表示不应使用缓存的信息,而应将请求转发到源服务器。此指令的语义与 PRAGMA:NO-CACHE 相同。
当向未知是否符合 HTTP/1.1 标准的服务器发送 no-cache 请求时,客户端应该包括 PRAGMA: NO-CACHE 和 CACHE-CONTROL: NO-CACHE。还请参见 EXPIRES。
注意:最好在 HTTP 中指定缓存命令,而不是在 META 语句中,在那里它们可能会影响更多的是代理和其他可能缓存信息的中间件,而不仅仅是浏览器。
<meta>
一直以来都是指定缓存的一种糟糕而不稳定的方式。 - Kornelno-transform
, max-age
, s-maxage
, must-revalidate
, proxy-revalidate
又怎么样?我们可以使用它们吗? - Ram Patra您可以使用以下方式在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)
?>
Pragma
和Expires
头部可能是多余的。 - thijsai正如我所写的,最好使用.htaccess
文件进行控制缓存,但要注意缓存内容的保留时间。
使用:
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
其中: 604800 = 7天
PS: 这可以用来重置任何标头(header)
该页面(http://www.askapache.com/htaccess/apache-speed-cache-control.html)建议使用以下代码:
添加缓存控制头信息
将以下代码加入你的根目录.htaccess文件中,但如果你可以访问httpd.conf则最好在其中进行修改。
此代码使用FilesMatch指令和Header指令将缓存控制头信息添加到特定的文件中。
# 480 weeks
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=290304000, public"
</FilesMatch>
cache
和no-cache
),则顺序无关紧要。 max-age
和 public
不冲突,因此顺序无关紧要。 - Blaisea2enmod
或将符号链接从/etc/apache2/mods-available/headers.load
创建到/etc/apache2/mods-enabled/headers.load
。 - Skippy le Grand Gourou这是我在实际网站中使用过的最佳.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>
<filesMatch "\.(x?html?|php)$">
以避免静态HTML页面被缓存。其余的文件匹配是可以的。 - nicoX<FilesMatch "\.(eot|woff|otf|ttf|svg)$">
。 - nicoXcss
和js
设置为私有。我在您提供的链接中没有找到任何关于此的建议。大多数CDN都建议您缓存这些值。 - nicoX对于 Apache 服务器,您可以检查 mod_expires 设置 Expires 和 Cache-Control 头文件。
另外,您也可以使用 Header 指令自己添加 Cache-Control:
Header set Cache-Control "max-age=290304000, public"
允许的值包括:
Public - 可以在公共共享缓存中缓存
Private - 只能在私有缓存中缓存
no-Cache - 不能被缓存
no-Store - 可以被缓存但不会被归档
请注意大小写敏感性。在您的网页源代码中添加以下meta标签。标签末尾的拼写差异是您使用“/> = 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">
Source-> MetaTags
OWASP推荐以下做法:
尽可能地确保设置cache-control HTTP头,其中包含no-cache、no-store、must-revalidate、private;并且设置pragma HTTP头,其中包含no-cache。
<IfModule mod_headers.c>
Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</IfModule>