利用浏览器缓存,在 apache 或 .htaccess 上如何?
在这里做什么?我有一个巨大的文件列表,Google Speed Page 说“利用浏览器缓存”..但我不知道如何?我是否会弄乱或更改 Apache 配置文件(如下),或者是否在 .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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我借此机会提供了完整的
.htaccess
代码来传递 Google PageSpeed Insight:还有一些针对各种 Web 服务器的配置,请参阅此处。
希望这有助于获得 100/100 分。
I took my chance to provide full
.htaccess
code to pass on Google PageSpeed Insight:There is also some configurations for various web servers see here.
Hope this would help to get the 100/100 score.
几天前我也在做同样的事情。将此添加到我的 .htaccess 文件中:
现在,当我运行 google speed page 时,利用浏览器缓存不再是高优先级。
希望这有帮助。
I was doing the same thing a couple days ago. Added this to my .htaccess file:
And now when I run google speed page, leverage browwer caching is no longer a high priority.
Hope this helps.
这是我用来控制标头/缓存的,我不是 Apache 专业人士,所以请告诉我是否有改进的空间,但我知道这在我的所有网站上已经运行良好一段时间了。
Mod_expires
http://httpd.apache.org/docs /2.2/mod/mod_expires.html
该模块控制服务器响应中 Expires HTTP 标头的设置和 Cache-Control HTTP 标头的 max-age 指令。到期日期可以设置为相对于源文件上次修改的时间或客户端访问的时间。
这些 HTTP 标头是向客户端发出的有关文档有效性和持久性的指令。如果已缓存,则可以从缓存而不是从源获取文档,直到该时间过去。此后,缓存副本被视为“过期”且无效,必须从源获取新副本。
Mod_headers
http://httpd.apache.org/docs /2.2/mod/mod_headers.html
该模块提供控制和修改 HTTP 请求和响应标头的指令。标头可以合并、替换或删除。
This is what I use to control headers/caching, I'm not an Apache pro, so let me know if there is room for improvement, but I know that this has been working well on all of my sites for some time now.
Mod_expires
http://httpd.apache.org/docs/2.2/mod/mod_expires.html
This module controls the setting of the Expires HTTP header and the max-age directive of the Cache-Control HTTP header in server responses. The expiration date can set to be relative to either the time the source file was last modified, or to the time of the client access.
These HTTP headers are an instruction to the client about the document's validity and persistence. If cached, the document may be fetched from the cache rather than from the source until this time has passed. After that, the cache copy is considered "expired" and invalid, and a new copy must be obtained from the source.
Mod_headers
http://httpd.apache.org/docs/2.2/mod/mod_headers.html
This module provides directives to control and modify HTTP request and response headers. Headers can be merged, replaced or removed.
首先我们需要检查是否启用了 mod_headers.c 和 mod_expires.c。
如果我们没有,我们需要启用它们
然后我们需要重新启动apache
最后,在.htaccess上添加规则(在其他答案中看到),例如
First we need to check if we have enabled mod_headers.c and mod_expires.c.
If we don't have it, we need to enable them
Then we need to restart apache
At last, add the rules on .htaccess (seen on other answers), for example