如何指定 Vary: Accept-Encoding 标头?

Posted

技术标签:

【中文标题】如何指定 Vary: Accept-Encoding 标头?【英文标题】:How to Specify a Vary: Accept-Encoding header? 【发布时间】:2013-05-14 13:52:57 【问题描述】:

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

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

【问题讨论】:

【参考方案1】:

我在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>

【讨论】:

这让我从 C -> B(在页面标题指标上)...现在收到有关使用 cookie 的消息(虽然我有一个简单的单页应用程序..gunna 继续拨打它..但这对我帮助很大!) 减少放气的东西,你可以使用: SetOutputFilter DEFLATE 【参考方案2】:

我也有这个不工作的问题

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

我有一个 Header set Cache-control - 它会覆盖 Header append Vary,所以你必须把它们放在同一个块中。 我必须做的是在一个 Filesmatch 语句中为所有其他文件设置 Vary,在一个单独的 FilesMatch 语句中为 php 文件设置 Cache 和 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 语句 - 只是为示例代码进行了简化。

【讨论】:

【参考方案3】:

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

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

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

【讨论】:

我将该代码添加到我的 .htaccess 文件中,但我的网站仍然没有压缩,您能帮什么忙.. 我联系了我的托管公司,他们是一群白痴,说 htaccess 不应该在首先是网站的根目录!!!疯狂 我不知道是什么问题。您应该将代码插入到一个空的.htaccess(必须与 index.html/index.php 位于同一目录中)文件中。也许它会起作用,您可以找出问题所在。尝试this tool 测试压缩。可能是浏览器缓存出了问题。 您的代码中有一个错字——您需要在“Vary”之后使用冒号:Header append Vary: Accept-Encoding

以上是关于如何指定 Vary: Accept-Encoding 标头?的主要内容,如果未能解决你的问题,请参考以下文章

HTTP标头“Vary:Accept-Encoding”指定方法及其重要性分析

vary的用法

如何将 yaws 中的“Vary: Accept-Encoding”标头添加到可缓存文件中

获取指定时间段里年月日三级日历

是否有任何理由对同一资源使用“Vary: *”和“Vary: Foo”响应?

为啥没有在 CORS 未命中时设置“Vary: Origin”响应?