加速网站正确完成

发布于 2024-11-27 14:53:18 字数 750 浏览 1 评论 0原文

我想加快我的网站速度。我想知道我是否在语法方面做得正确。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倦话 2024-12-04 14:53:18

不要缓存 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.

圈圈圆圆圈圈 2024-12-04 14:53:18

我知道这个问题是 2011 年提出的,但即便如此,Pragma 仍然很旧。我认为停止使用它是安全的。

我们很少需要设置“公共”或必须重新验证,除非您尝试支持非常旧的浏览器和代理(例如 IEv4 之前的版本等)。

如果你要缓存你的 php,你仍然需要在你的 php 代码中做一些工作。

取消设置图像和内容的“上次修改时间”本质上会导致这些文件出现“从不缓存”的情况,直到静态(当时)未来日期的预定日期为止。我认为您希望在未来 X 天始终缓存静态文件,而不是取消设置上次修改。

您的 htaccess 文件可以变得更加简单。借用 H5BP 的一些片段,我们可以对其进行一些现代化改造,使其成为:

## set some mime types to their proper types
AddType application/javascript      js jsonp
AddType application/json            json
AddType application/xml             rss atom xml rdf
AddType image/x-icon                ico


<IfModule mod_deflate.c>
  # Force deflate for mangled headers developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
  <IfModule mod_setenvif.c>
    <IfModule mod_headers.c>
      SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
      RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
    </IfModule>
  </IfModule>

  # Compress all output labeled with one of the following MIME-types
  # (for Apache versions below 2.3.7, you don't need to enable `mod_filter`
  # and can remove the `<IfModule mod_filter.c>` and `</IfModule>` lines as
  # `AddOutputFilterByType` is still in the core directives)
  <IfModule mod_filter.c>
    AddOutputFilterByType DEFLATE application/atom+xml \
                                  application/javascript \
                                  application/json \
                                  application/rss+xml \
                                  application/vnd.ms-fontobject \
                                  application/x-font-ttf \
                                  application/xhtml+xml \
                                  application/xml \
                                  font/opentype \
                                  image/svg+xml \
                                  image/x-icon \
                                  text/css \
                                  text/html \
                                  text/plain \
                                  text/x-component \
                                  text/xml
  </IfModule>
</IfModule>


<IfModule mod_expires.c>
  ExpiresActive on
  ExpiresDefault                          "access plus 1 month"
# Your document html
  ExpiresByType text/html                 "access plus 0 seconds"
# Data
  ExpiresByType application/json          "access plus 0 seconds"
  ExpiresByType application/xml           "access plus 0 seconds"
  ExpiresByType text/xml                  "access plus 0 seconds"
# Feed
  ExpiresByType application/atom+xml      "access plus 1 hour"
  ExpiresByType application/rss+xml       "access plus 1 hour"
# Favicon (cannot be renamed)
  ExpiresByType image/x-icon              "access plus 1 week"
# images
  ExpiresByType image/gif                 "access plus 1 month"
  ExpiresByType image/jpeg                "access plus 1 month"
  ExpiresByType image/png                 "access plus 1 month"
# CSS and JavaScript
  ExpiresByType application/javascript    "access plus 1 year"
  ExpiresByType text/css                  "access plus 1 year"
</IfModule>

# ----------------------------------------------------------------------
# Prevent mobile network providers from modifying your site
# ----------------------------------------------------------------------

# The following header prevents modification of your code over 3G on some
# European providers.
# This is the official 'bypass' suggested by O2 in the UK.

<IfModule mod_headers.c>
Header set Cache-Control "no-transform"
</IfModule>


# ----------------------------------------------------------------------
# ETag removal
# ----------------------------------------------------------------------

# FileETag None is not enough for every server.
<IfModule mod_headers.c>
  Header unset ETag
</IfModule>

# Since we're sending far-future expires, we don't need ETags for
# static content.
#   developer.yahoo.com/performance/rules.html#etags
FileETag None

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:

## set some mime types to their proper types
AddType application/javascript      js jsonp
AddType application/json            json
AddType application/xml             rss atom xml rdf
AddType image/x-icon                ico


<IfModule mod_deflate.c>
  # Force deflate for mangled headers developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
  <IfModule mod_setenvif.c>
    <IfModule mod_headers.c>
      SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
      RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
    </IfModule>
  </IfModule>

  # Compress all output labeled with one of the following MIME-types
  # (for Apache versions below 2.3.7, you don't need to enable `mod_filter`
  # and can remove the `<IfModule mod_filter.c>` and `</IfModule>` lines as
  # `AddOutputFilterByType` is still in the core directives)
  <IfModule mod_filter.c>
    AddOutputFilterByType DEFLATE application/atom+xml \
                                  application/javascript \
                                  application/json \
                                  application/rss+xml \
                                  application/vnd.ms-fontobject \
                                  application/x-font-ttf \
                                  application/xhtml+xml \
                                  application/xml \
                                  font/opentype \
                                  image/svg+xml \
                                  image/x-icon \
                                  text/css \
                                  text/html \
                                  text/plain \
                                  text/x-component \
                                  text/xml
  </IfModule>
</IfModule>


<IfModule mod_expires.c>
  ExpiresActive on
  ExpiresDefault                          "access plus 1 month"
# Your document html
  ExpiresByType text/html                 "access plus 0 seconds"
# Data
  ExpiresByType application/json          "access plus 0 seconds"
  ExpiresByType application/xml           "access plus 0 seconds"
  ExpiresByType text/xml                  "access plus 0 seconds"
# Feed
  ExpiresByType application/atom+xml      "access plus 1 hour"
  ExpiresByType application/rss+xml       "access plus 1 hour"
# Favicon (cannot be renamed)
  ExpiresByType image/x-icon              "access plus 1 week"
# images
  ExpiresByType image/gif                 "access plus 1 month"
  ExpiresByType image/jpeg                "access plus 1 month"
  ExpiresByType image/png                 "access plus 1 month"
# CSS and JavaScript
  ExpiresByType application/javascript    "access plus 1 year"
  ExpiresByType text/css                  "access plus 1 year"
</IfModule>

# ----------------------------------------------------------------------
# Prevent mobile network providers from modifying your site
# ----------------------------------------------------------------------

# The following header prevents modification of your code over 3G on some
# European providers.
# This is the official 'bypass' suggested by O2 in the UK.

<IfModule mod_headers.c>
Header set Cache-Control "no-transform"
</IfModule>


# ----------------------------------------------------------------------
# ETag removal
# ----------------------------------------------------------------------

# FileETag None is not enough for every server.
<IfModule mod_headers.c>
  Header unset ETag
</IfModule>

# Since we're sending far-future expires, we don't need ETags for
# static content.
#   developer.yahoo.com/performance/rules.html#etags
FileETag None
无远思近则忧 2024-12-04 14:53:18

谷歌有一个网站可以帮助您分析您的网站。该工具提供了许多工具和有用的分析。

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文