htaccess的缓存不工作 [英] htaccess caching not working

查看:201
本文介绍了htaccess的缓存不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我输入以下到我htacces,以启动高速缓存在WebContent。据谷歌网页加载速度和YSlow的页面仍然没有缓存。是模块错误不会在应用程序没有发现他们?

站点是在Apache 2.0上运行

htaccess的(部分带缓存模块):

 #过期头
< ifModule mod_expires.c>
ExpiresActive在
ExpiresDefault进入加1秒。
ExpiresByType图像/ X-图标进入加2592000秒
ExpiresByType为image / jpeg进入加2592000秒
ExpiresByType图像/ PNG进入加2592000秒
ExpiresByType图像/ GIF进入加2592000秒
ExpiresByType应用程序/ x-冲击波闪光进入加2592000秒
ExpiresByType文本/ CSS进入加604800秒
ExpiresByType文/ JavaScript的进入加216000秒
ExpiresByType应用程序/ JavaScript的进入加216000秒
ExpiresByType应用程序/ x-j​​avascript的进入加216000秒
ExpiresByType text / html的访问,再加上600秒。
ExpiresByType是application / xhtml + xml的访问,再加上600秒。
< / ifModule>

#缓存控制头
< ifModule mod_headers.c>
#月
< filesMatch\(ICO | JPE G | PNG | GIF |瑞士法郎?)$。>
页眉设置缓存控制最大年龄= 2592000,公开
< / filesMatch>
#周
< filesMatch\(CSS | JS)$。>
页眉设置缓存控制最大年龄= 604800,公开
< / filesMatch>
#日
< filesMatch\(X HTML | PHP的?)$。>
页眉设置缓存控制最大年龄= 43200,私人,必重新验证
< / filesMatch>
< / ifModule>
#END的Cache-Control头

#开启ETag的关
< ifModule mod_headers.c>
头取消设置的ETag
< / ifModule>
FileETag无

#删除Last-Modified头
< ifModule mod_headers.c>
头取消设置的Last-Modified
< / ifModule>
 

解决方案

走进的httpd.conf 并查找 mod_expires 行,它不应该被注释掉。查找 mod_headers中行,并确保它没有被注释掉。

或者(不适用于关键应用程序)有一个简单的和肮脏的测试:删除< ifModule mod_expires.c> < / ifModule> 留下的东西之间​​,同样适用于< ifModule mod_headers.c> 如果你的服务器失败,500互联网服务器错误,那么你可能缺少其中的一个模块或两个,他们都没有启用。如果是的话,进入httpd.conf文件,使你所需要的。

您也可以使用如 REDbot 工具测试你的网站的响应头。只需选择喜欢一个指向一个资源的URL图像并将其粘贴工具,看看有什么头送回到连同一些建议。请注意,它遵循域的robots.txt规则并不会检查资源,如果它是不允许的。

而像戈说,使用net选项卡火狐,Chrome开发工具,或者一些等价的web开发工具,帮助看到正在发送和接收什么样的头。

您也不必设置缓存控制公共。而你并不需要使用最大年龄如果您还使用 ExpiresByType 通话。

有关更多信息阅读这个伟大的教程: http://www.mnot.net/cache_docs/

和学习到的例子:结帐它是如何在 HTML5的样板做在的 https://github.com/h5bp/html5-boilerplate/blob/master/dist/.htaccess

有关其他流行的服务器配置的例子如 lighthttpd 的Node.js Nginx的,等看到: https://github.com/h5bp/server-configs

I inputted the following into my htacces in order to start caching the webcontent. According to Google Page Speed and Yslow the pages are still not cached. Are the modules wrong does the apps not spot that they are?

Site is running on Apache 2.0

htaccess (part with caching modules):

# Expire headers
<ifModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType image/x-icon "access plus 2592000 seconds"
  ExpiresByType image/jpeg "access plus 2592000 seconds"
  ExpiresByType image/png "access plus 2592000 seconds"
  ExpiresByType image/gif "access plus 2592000 seconds"
  ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
  ExpiresByType text/css "access plus 604800 seconds"
  ExpiresByType text/javascript "access plus 216000 seconds"
  ExpiresByType application/javascript "access plus 216000 seconds"
  ExpiresByType application/x-javascript "access plus 216000 seconds"
  ExpiresByType text/html "access plus 600 seconds"
  ExpiresByType application/xhtml+xml "access plus 600 seconds"
</ifModule>
 
# Cache-Control Headers
<ifModule mod_headers.c>
#month
  <filesMatch "\.(ico|jpe?g|png|gif|swf)$">
    Header set Cache-Control "max-age=2592000, public"
  </filesMatch>
#week
  <filesMatch "\.(css|js)$">
    Header set Cache-Control "max-age=604800, public"
  </filesMatch>
#day
  <filesMatch "\.(x?html?|php)$">
    Header set Cache-Control "max-age=43200, private, must-revalidate"
  </filesMatch>
</ifModule>
# END Cache-Control Headers
 
# Turn ETags Off
<ifModule mod_headers.c>
  Header unset ETag
</ifModule>
FileETag None
 
# Remove Last-Modified Header
<ifModule mod_headers.c>
  Header unset Last-Modified
</ifModule>

解决方案

Go into httpd.conf and look for the mod_expires line, it should not be commented out. Look for the mod_headers line and make sure it is not commented out.

Or (not for a critical app) there is an easy and dirty test: remove <ifModule mod_expires.c> and </ifModule> leave stuff in between, same goes for <ifModule mod_headers.c> and if your server fails with 500 internet Server Error then your probably missing one or both of those modules and they are not enabled. If so then go into httpd.conf and enable what you need.

You can also test your site's response headers using a tool like REDbot. Simply pick a resource URL like one pointing to an image and paste it in the tool to see what headers get sent back along with some recommendations. Note that it follows the domain's robots.txt rules and will not check the resource if it is disallowed.

And like Gerben said, using the net tab in firefox, chrome dev tools, or some equivalent web developer tool helps see what headers are being sent and received.

You also don't need to set Cache-Control public. And you don't need to use max age if you're also using ExpiresByType calls.

For more info read this great tutorial: http://www.mnot.net/cache_docs/

And learn by example: checkout how it is done in the html5-boilerplate at https://github.com/h5bp/html5-boilerplate/blob/master/dist/.htaccess

For other popular server config examples like lighthttpd, Node.js, Nginx, etc. see: https://github.com/h5bp/server-configs

这篇关于htaccess的缓存不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆