如何通过正确的 Apache 缓存标头设置来加速 Web 开发?
尽管采取了各种措施来在 htaccess 文件中设置正确的缓存代码,但我仍然收到此错误:
指定缓存验证器: 所有静态资源都应具有 Last-Modified 或 ETag 标头。这将使浏览器能够充分利用缓存的优势。
我的 htaccess 缓存设置有什么问题吗?如果您对这些设置有改进,我将很高兴听到。非常感谢您的建议。
<IfModule mod_headers.c>
Header unset Pragma
FileETag None
Header unset ETag
ExpiresActive On
##### DYNAMIC PAGES
<FilesMatch "\\.(ast|php|css)$">
Header set Cache-Control "public, max-age=3600, must-revalidate"
</FilesMatch>
##### STATIC FILES
<FilesMatch "\\.(png|svg|swf|js|xml)$">
Header set Cache-Control "public, max-age=604800, must-revalidate"
Header unset Last-Modified
</FilesMatch>
##### ETERNAL FILES
<FilesMatch "\\.(ico|jpg|gif|ttf|eot|pdf|flv)$">
Header set Cache-Control "public, max-age=7257600, must-revalidate"
Header unset Last-Modified
</FilesMatch>
</IfModule>
Despite various measure ments to setup correct caching code in htaccess file, I still get this error:
Specify a cache validator:
All static resources should have either a Last-Modified or ETag header. This will allow browsers to take advantage of the full benefits of caching.
Is there anything wrong with my htaccess caching settings? If you have improvements for these settings i will be very happy to hear. Thank you very much for your suggestions.
<IfModule mod_headers.c>
Header unset Pragma
FileETag None
Header unset ETag
ExpiresActive On
##### DYNAMIC PAGES
<FilesMatch "\\.(ast|php|css)$">
Header set Cache-Control "public, max-age=3600, must-revalidate"
</FilesMatch>
##### STATIC FILES
<FilesMatch "\\.(png|svg|swf|js|xml)$">
Header set Cache-Control "public, max-age=604800, must-revalidate"
Header unset Last-Modified
</FilesMatch>
##### ETERNAL FILES
<FilesMatch "\\.(ico|jpg|gif|ttf|eot|pdf|flv)$">
Header set Cache-Control "public, max-age=7257600, must-revalidate"
Header unset Last-Modified
</FilesMatch>
</IfModule>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您专门取消了 Last-Modified 标头的设置。这是缓存验证器部分。删除包括以下内容的行:
另外,你的 css 真的是动态的吗? CSS 对于很多网站来说可能都很大。尝试像任何其他静态内容一样缓存它。
You are specifically unsetting the Last-Modified header. That's the cache validator section. Remove those lines that include:
Also, is your css really dynamic? CSS can be huge for a lot of websites. Try to cache that just like any other static content.
所有静态资源都应具有 Last-Modified 或 ETag 标头。这将使浏览器能够充分利用缓存的优势。
All static resources should have either a Last-Modified or ETag header. This will allow browsers to take advantage of the full benefits of caching.