改善网站性能缓存静态内容问题

时间:2013-05-17 12:01:17

标签: performance web

我正在使用网页测试来了解我的网站性能。我发现缓存静态内容存在问题,即“利用浏览器缓存静态资产:88/100”

失败 - (没有最大年龄或过期)

我该如何解决这个问题?

我的htaccess看起来像:

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/png
AddOutputFilterByType DEFLATE image/jpg
AddOutputFilterByType DEFLATE image/gif

# Or, compress certain file types by extension:
<ifModule mod_deflate.c>
<filesMatch ".(js|css|html|htm|php|jpg|jpeg|woff|eot)$">
SetOutputFilter DEFLATE
</FilesMatch>
</ifModule>

# Direct Apache to send all HTML output to the mod_pagespeed output handler.
AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text/html

由于

2 个答案:

答案 0 :(得分:0)

除了deflate设置之外,您还需要添加一些过期控制来管理浏览器缓存。我使用了以下效果很好。根据您网站上文件的具体情况,您可能还需要添加音频和视频文件。

从内存中,当您将到期时间设置为至少1个月时,网页测试会喜欢它,因此您可能还需要调整1周的设置

祝你好运!

################
# Enable expiration control
ExpiresActive On

# Default expiration: 1 hour after request
ExpiresDefault "now plus 1 hour"

# CSS and JS expiration: 1 week after request
ExpiresByType text/css "now plus 1 week"
ExpiresByType application/javascript "now plus 1 week"
ExpiresByType application/x-javascript "now plus 1 week"

# Image files expiration: 1 month after request
ExpiresByType image/bmp "now plus 1 month"
ExpiresByType image/gif "now plus 1 month"
ExpiresByType image/jpeg "now plus 1 month"
ExpiresByType image/jp2 "now plus 1 month"
ExpiresByType image/pipeg "now plus 1 month"
ExpiresByType image/png "now plus 1 month"

########## End - Optimal expiration time

答案 1 :(得分:0)

ExpiresActive on
ExpiresDefault "access plus 1 month"
ExpiresByType text/html "now plus 0 second"
<FilesMatch "\.(css|js|png|bmp|ico|htm|gff|html|js|jpg|jpeg|gif|gcf)$">
FileETag MTime Size
ExpiresDefault "access plus 1 month"
</FilesMatch>

这是我在托管中更改缓存控件后的.htaccess中的内容。但即使我改变了这些,当我运行webpagetest.org时似乎没有任何好的效果。我需要等到之前的缓存过期并再次运行它。这行得通。欢呼! 。在这里我找到了一个有用的链接 http://httpd.apache.org/docs/current/mod/mod_expires.html