加速网站正确完成
我想加快我的网站速度。我想知道我是否在语法方面做得正确。
<IfModule mod_expires.c>
Header unset Pragma
FileETag None
Header unset ETag
ExpiresActive On
ExpiresDefault "access plus 1 year"
##### DYNAMIC PAGES
<FilesMatch "\\.(ast|php)$">
ExpiresDefault A7200
Header set Cache-Control "public, max-age=3600, must-revalidate"
</FilesMatch>
##### STATIC FILES
<FilesMatch "(?i)^.*\.(ico|png|gif|jpg)$">
Header unset Last-Modified
Header set Expires "Fri, 21 Dec 2012 00:00:00 GMT"
Header set Cache-Control "public, no-transform"
</FilesMatch>
<FilesMatch "\\.(css|js|xml)$">
Header set Cache-Control "public, max-age=604800, must-revalidate"
</FilesMatch>
</IfModule>
I want to speed up my website. I was wondering I've done it correctly syntax wise.
<IfModule mod_expires.c>
Header unset Pragma
FileETag None
Header unset ETag
ExpiresActive On
ExpiresDefault "access plus 1 year"
##### DYNAMIC PAGES
<FilesMatch "\\.(ast|php)$">
ExpiresDefault A7200
Header set Cache-Control "public, max-age=3600, must-revalidate"
</FilesMatch>
##### STATIC FILES
<FilesMatch "(?i)^.*\.(ico|png|gif|jpg)$">
Header unset Last-Modified
Header set Expires "Fri, 21 Dec 2012 00:00:00 GMT"
Header set Cache-Control "public, no-transform"
</FilesMatch>
<FilesMatch "\\.(css|js|xml)$">
Header set Cache-Control "public, max-age=604800, must-revalidate"
</FilesMatch>
</IfModule>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要缓存 asp 或 php 或任何动态页面!这将导致不可预测的结果。
下面是一个示例,假设您有一个名为catalog.php 的页面,该页面呈现具有最新价格和库存情况的产品目录。由于您已将其设置为在浏览器上缓存结果一小时,因此它将在浏览器上显示陈旧数据一小时!
动态页面永远不应该像这样批量缓存。您需要根据从页面返回的数据的新鲜程度在页面上放置单独的缓存逻辑。
对于静态页面,您可以进行此类批量过期操作。但是,请注意,如果您将 css 和 js 文件设置为一年后过期,那么今天访问您网站的用户将在很多天或几个月内无法从网络服务器获取最新的 js 和 css。如果您对脚本或样式进行更改,它们将看不到更改,除非您使用某些唯一的查询字符串手动更改文件的 url。
我在这里讨论了一种仅适用于 ASP.NET 的方法。但它会告诉您注意事项。
http://omaralzabir.com/automatic-javascript-css- versioning-to-refresh-browser-cache/
您还可以阅读我关于充分利用缓存的 7 个技巧,其中解释了所有此类方法以及每种方法的优缺点:
http://omaralzabir.com/making_best_use_of_cache_for_high_performance_website/
请告诉我这是否有帮助。
Do not cache asp or php or any dynamic page! This will cause unpredictable result.
Here's an example, say you have a page called catalog.php that renders the product catalog with uptodate price and stock availability. Since you have set it to cache the result on browser for an hour, it will show stale data on browser for an hour!
Dynamic pages should never be cached wholesale like this. You need to put individual caching logic on pages based on how fresh the data should be returned from the page.
For static pages though you can do such wholesale expiration. However, be careful that if you set css and js files to expire after one year, a user who visits your site today will not fetch latest js and css from webserver for many days or months. If you make changes to the scripts or style, they will not see the changes unless you use some unique querystring to change the url of the files manually.
I have discussed an approach here that works for ASP.NET only. But it tells you about the dos and don'ts.
http://omaralzabir.com/automatic-javascript-css-versioning-to-refresh-browser-cache/
You can also read my 7 tips for making best use of caching that explains all such approaches and pros and cons of each:
http://omaralzabir.com/making_best_use_of_cache_for_high_performance_website/
Let me know if this helps.
我知道这个问题是 2011 年提出的,但即便如此,Pragma 仍然很旧。我认为停止使用它是安全的。
我们很少需要设置“公共”或必须重新验证,除非您尝试支持非常旧的浏览器和代理(例如 IEv4 之前的版本等)。
如果你要缓存你的 php,你仍然需要在你的 php 代码中做一些工作。
取消设置图像和内容的“上次修改时间”本质上会导致这些文件出现“从不缓存”的情况,直到静态(当时)未来日期的预定日期为止。我认为您希望在未来 X 天始终缓存静态文件,而不是取消设置上次修改。
您的 htaccess 文件可以变得更加简单。借用 H5BP 的一些片段,我们可以对其进行一些现代化改造,使其成为:
I am aware the question is from 2011 but even then, Pragma was old. I think it is safe to stop using it.
We rarely need to set 'public' or must-revalidate unless your trying to support really old browsers and proxies like pre IEv4 and stuff.
If your going to cache your php you still need to do some work in your php code too.
Unsetting your Last-Modified for images and stuff is essentially causing a 'never cache' situation for those files until the predetermined date of the static (at the time) future date. I would think you want to cache static files X days in future always and not unset last modified.
Your htaccess file can be made to be much simpler. Borrowing some snippets from H5BP we can modernize it a bit to become:
谷歌有一个网站可以帮助您分析您的网站。该工具提供了许多工具和有用的分析。
https://developers.google.com/speed/
Google has a website that will help you analyze your site. There are a number of tools and helpful analysis available with this tool.
https://developers.google.com/speed/