13

Google 和 pingdom.com 说我应该“指定一个 Vary:Accept-Encoding 标头”

我不知道或不明白如何做到这一点。谁能解释它是什么以及它的作用?

4

3 回答 3

18

我在https://tools.pingdom.com/https://developers.google.com/speed/pagespeed/insights/中获得了近 100% 的分数

我发现了一篇有用的帖子来加速 wordpress 网站或博客https://www.keycdn.com/blog/speed-up-wordpress/

通过其他一些优化,我还在我的网站上的.htaccess文件中使用以下代码(通常隐藏在主网站文件夹中)

我的服务器是 Apache,您可以查看主机控制面板(如 cPanel/WHM 面板)(如果您的服务器是 nginx,请查看 keycdn.com 帖子)

(在 .htaccess 文件中复制并粘贴以下代码,这对我很有用)

(如果对您有用,请支持此答案)

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 month"
ExpiresByType image/jpeg "access 1 month"
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType image/svg "access 1 month"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType text/javascript "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/xhtml+xml "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 month"
ExpiresDefault "access 1 month"
</IfModule>

<ifModule mod_headers.c>
  <filesMatch ".(css|jpg|jpeg|png|gif|swf|svg|js|ico)$">
    Header set Cache-Control "max-age=2592000, public"
  </filesMatch>
  <filesMatch ".(x?html?|php)$">
    Header set Cache-Control "private, must-revalidate"
  </filesMatch>
</ifModule>

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/json
  AddOutputFilterByType DEFLATE application/atom+xml
  AddOutputFilterByType DEFLATE application/rdf+xml
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-font-woff
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/truetype
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml
</IfModule>
于 2016-06-23T22:23:08.403 回答
16

我认为您必须为特定文件启用压缩,例如css,jsxml. 添加到您域的根 .htaccess 文件的以下代码将在您的服务器上启用这种功能:

<IfModule mod_headers.c>
  <FilesMatch "\.(js|css|xml|gz)$">
    Header append Vary: Accept-Encoding
  </FilesMatch>
</IfModule>

如果您想在此规则中添加更多文件类型,只需将其扩展名添加到语句中即可!<FilesMatch "\.(js|css|xml|gz|newone)$">

于 2013-05-20T06:49:23.013 回答
3

我也有这个不工作的问题

发生的事情是我的 php 文件有另一个标头指令

我有一个 Header set Cache-control - 它覆盖了 Header append Vary,所以你必须把它们放在同一个块中。
我必须做的是在一个 Filesmatch 语句中为所有其他文件设置 Vary,在一个单独的 FilesMatch 语句中为 php 文件设置缓存和 Vary,如下所示:

<IfModule mod_headers.c>
<FilesMatch "\.(js|css|gz)$">
 Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>


<IfModule mod_headers.c>
<FilesMatch "\.(php)$">
 Header set Cache-Control "max-age=300"
 Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>

这不是我实际的 Cache-Control 语句 - 只是为示例代码进行了简化。

于 2014-11-30T13:41:22.067 回答