利用浏览器caching,如何在Apache或.htaccess?

不知道在这里做什么? 我有一个谷歌速度页面说“利用浏览器caching”巨大的文件列表..但我不知道如何? 我是否会混淆或更改Apacheconfiguration文件(下面),或者我在.htaccess页面中放置了什么?

非常感谢!!

<IfModule mod_proxy.c> ProxyRequests Off CacheRoot "/var/run/proxy" CacheSize 1024 CacheGcInterval 24 #CacheMaxExpire 24 #CacheLastModifiedFactor 0.1 #CacheDefaultExpire 1 #NoCache a_domain.com another_domain.edu joes.garage_sale.com <Directory "disabled_proxy"> Allow from example.com Deny from all Order Deny,Allow </Directory> </IfModule> ## #### mod_expires is configured so that all static files but images #### expire after 60 seconds. Any response that has a life span of more #### than 5 seconds (see webperfcache.conf) will be cached by webperfcache. #### Make sure your CGIs return a "Cache-Control: no-cache" header if you #### elect to make your dynamically generated HTML pages not cache-able. #### If all your HTML pages are static you may also increase ExpiresDefault. <IfModule mod_expires.c> ExpiresActive On ExpiresDefault A60 ExpiresByType image/bmp A3600 ExpiresByType image/gif A3600 ExpiresByType image/ief A3600 ExpiresByType image/jpeg A3600 ExpiresByType image/png A3600 ExpiresByType image/tiff A3600 ExpiresByType image/x-cmu-raster A3600 ExpiresByType image/x-portable-anymap A3600 ExpiresByType image/x-portable-bitmap A3600 ExpiresByType image/x-portable-graymap A3600 ExpiresByType image/x-portable-pixmap A3600 ExpiresByType image/x-rgb A3600 ExpiresByType image/x-xbitmap A3600 ExpiresByType image/x-xpixmap A3600 ExpiresByType image/x-xwindowdump A3600 ExpiresByType audio/basic A3600 ExpiresByType audio/midi A3600 ExpiresByType audio/mpeg A3600 ExpiresByType audio/x-aiff A3600 ExpiresByType audio/x-pn-realaudio A3600 ExpiresByType audio/x-pn-realaudio-plugin A3600 ExpiresByType audio/x-realaudio A3600 ExpiresByType audio/x-wav A3600 ExpiresByType video/mpeg A3600 ExpiresByType video/quicktime A3600 ExpiresByType video/x-msvideo A3600 ExpiresByType video/x-sgi-movie A3600 </IfModule> 

几天前我也在做同样的事情。 添加到我的.htaccess文件:

 ExpiresActive On ExpiresByType image/gif A2592000 ExpiresByType image/jpeg A2592000 ExpiresByType image/jpg A2592000 ExpiresByType image/png A2592000 ExpiresByType image/x-icon A2592000 ExpiresByType text/css A86400 ExpiresByType text/javascript A86400 ExpiresByType application/x-shockwave-flash A2592000 # <FilesMatch "\.(gif¦jpe?g¦png¦ico¦css¦js¦swf)$"> Header set Cache-Control "public" </FilesMatch> 

现在,当我运行谷歌速度页面时,利用浏览器caching不再是高优先级。

希望这可以帮助。

我借此机会提供完整的.htaccess代码来传递Google PageSpeed Insight:

  1. 启用压缩
  2. 利用浏览器caching
 # Enable Compression <IfModule mod_deflate.c> AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/vnd.ms-fontobject AddOutputFilterByType DEFLATE application/x-font AddOutputFilterByType DEFLATE application/x-font-opentype AddOutputFilterByType DEFLATE application/x-font-otf AddOutputFilterByType DEFLATE application/x-font-truetype AddOutputFilterByType DEFLATE application/x-font-ttf AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE font/opentype AddOutputFilterByType DEFLATE font/otf AddOutputFilterByType DEFLATE font/ttf AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE image/x-icon AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/plain </IfModule> <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> # Leverage Browser 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> <IfModule mod_headers.c> <filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$"> Header set Cache-Control "max-age=2678400, public" </filesmatch> <filesmatch "\.(html|htm)$"> Header set Cache-Control "max-age=7200, private, must-revalidate" </filesmatch> <filesmatch "\.(pdf)$"> Header set Cache-Control "max-age=86400, public" </filesmatch> <filesmatch "\.(js)$"> Header set Cache-Control "max-age=2678400, private" </filesmatch> </IfModule> 

这里也有一些用于各种networking服务器的configuration。
希望这将有助于获得100/100的分数。

优化网页评分

这是我用来控制标头/caching,我不是一个Apache的专业人士,所以让我知道是否有改进的余地,但我知道这一直在我的网站上运行良好,现在一段时间了。

指定mod_expires

http://httpd.apache.org/docs/2.2/mod/mod_expires.html

此模块控制服务器响应中的Expires HTTP标头和Cache-Control HTTP标头的max-age指令的设置。 到期date可以设置为相对于源文件最后一次修改的时间或客户端访问时间。

这些HTTP头是对客户关于文档的有效性和持久性的指示。 如果caching,文档可能会从caching中而不是从源获取,直到这个时间已经过去。 之后,caching副本被视为“过期”且无效,并且必须从源获得新的副本。

 # BEGIN Expires <ifModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 1 seconds" ExpiresByType text/html "access plus 1 seconds" ExpiresByType image/gif "access plus 2592000 seconds" ExpiresByType image/jpeg "access plus 2592000 seconds" ExpiresByType image/png "access plus 2592000 seconds" ExpiresByType text/css "access plus 604800 seconds" ExpiresByType text/javascript "access plus 216000 seconds" ExpiresByType application/x-javascript "access plus 216000 seconds" </ifModule> # END Expires 

mod_headers中

http://httpd.apache.org/docs/2.2/mod/mod_headers.html

这个模块提供了控制和修改HTTP请求和响应头的指令。 标题可以合并,replace或删除。

 # BEGIN Caching <ifModule mod_headers.c> <filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$"> Header set Cache-Control "max-age=2592000, public" </filesMatch> <filesMatch "\.(css)$"> Header set Cache-Control "max-age=604800, public" </filesMatch> <filesMatch "\.(js)$"> Header set Cache-Control "max-age=216000, private" </filesMatch> <filesMatch "\.(xml|txt)$"> Header set Cache-Control "max-age=216000, public, must-revalidate" </filesMatch> <filesMatch "\.(html|htm|php)$"> Header set Cache-Control "max-age=1, private, must-revalidate" </filesMatch> </ifModule> # END Caching 

首先我们需要检查是否启用了mod_headers.c和mod_expires.c。

 sudo apache2 -l 

如果我们没有它,我们需要启用它们

 sudo a2enmod headers 

然后我们需要重新启动Apache

 sudo apache2 restart 

最后,例如,在.htaccess上添加规则(在其他答案中可以看到)

 ExpiresActive On ExpiresByType image/gif A2592000 ExpiresByType image/jpeg A2592000 ExpiresByType image/jpg A2592000 ExpiresByType image/png A2592000 ExpiresByType image/x-icon A2592000 ExpiresByType text/css A86400 ExpiresByType text/javascript A86400 ExpiresByType application/x-shockwave-flash A2592000 # <FilesMatch "\.(gif¦jpe?g¦png¦ico¦css¦js¦swf)$"> Header set Cache-Control "public" </FilesMatch>