首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加速网站的正确运行

加速网站的正确运行
EN

Stack Overflow用户
提问于 2011-07-31 02:06:28
回答 3查看 301关注 0票数 1

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

代码语言:javascript
复制
<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>
EN

回答 3

Stack Overflow用户

发布于 2011-07-31 21:23:14

不要缓存asp或php或任何动态页面!这将导致不可预知的结果。

下面是一个示例,假设您有一个名为catalog.php的页面,该页面呈现具有最新价格和库存的产品目录。由于您已将其设置为在浏览器上缓存一个小时的结果,因此它将在浏览器上显示一个小时的陈旧数据!

动态页面永远不应该像这样批量缓存。您需要根据从页面返回的数据的新鲜度,在页面上放置单独的缓存逻辑。

但是,对于静态页面,您可以执行这种大规模过期操作。但是,请注意,如果您将css和js文件设置为在一年后过期,则今天访问您站点的用户将在许多天或几个月内无法从webserver获取最新的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/

如果这有帮助,请告诉我。

票数 0
EN

Stack Overflow用户

发布于 2013-03-06 02:53:28

我知道这个问题是在2011年提出的,但即便如此,普拉格玛也是老问题了。我认为停止使用它是安全的。

我们很少需要设置'public‘或必须重新验证,除非你想要支持非常老的浏览器和代理,比如pre IEv4之类的。

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

取消设置图像和内容的上次修改时间本质上是导致这些文件在静态(当时)未来日期的预定日期之前永远不缓存的情况。我会认为你想要缓存静态文件X天在未来总是,而不是取消设置上次修改。

您的htaccess文件可以变得更简单。借用some snippets from H5BP,我们可以对其进行一点现代化,使其成为:

代码语言:javascript
复制
## 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
票数 0
EN

Stack Overflow用户

发布于 2013-03-06 03:18:54

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

https://developers.google.com/speed/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6885016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档