如何指定“变化:接受编码” .htaccess 中的标头

发布于 2024-09-18 00:49:56 字数 90 浏览 4 评论 0原文

Google PageSpeed 说我应该为 JS 和 CSS“指定一个 Vary:Accept-Encoding 标头”。如何在 .htaccess 中执行此操作?

Google PageSpeed says I should "Specify a Vary: Accept-Encoding header" for JS and CSS. How do I do this in .htaccess?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

榕城若虚 2024-09-25 00:49:57

还要 gzip 压缩你的字体文件!

add "x-font/otf x-font/ttf x-font/eot"

如:

AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml x-font/otf x-font/ttf x-font/eot

To gzip up your font files as well!

add "x-font/otf x-font/ttf x-font/eot"

as in:

AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml x-font/otf x-font/ttf x-font/eot
蒗幽 2024-09-25 00:49:57

这让我抓狂,但 aularon 的编辑似乎缺少 “Vary” 之后的冒号。因此,将 "Vary Accept-Encoding" 更改为 "Vary: Accept-Encoding" 解决了我的问题。

我本来想在帖子下面发表评论,但似乎不允许我这样做。

无论如何,我希望这可以帮助别人解决我遇到的同样的麻烦。

This was driving me crazy, but it seems that aularon's edit was missing the colon after "Vary". So changing "Vary Accept-Encoding" to "Vary: Accept-Encoding" fixed the issue for me.

I would have commented below the post, but it doesn't seem like it will let me.

Anyhow, I hope this saves someone the same trouble I was having.

枯寂 2024-09-25 00:49:57

花了很多时间来澄清那是什么。请阅读这篇文章以获取高级信息.HTACCESS 代码并了解它们的作用。

您可以使用:

Header append Vary "Accept-Encoding"
#or
Header set Vary "Accept-Encoding"

Many hours spent to clarify what was that. Please, read this post to get the advanced .HTACCESS codes and learn what they do.

You can use:

Header append Vary "Accept-Encoding"
#or
Header set Vary "Accept-Encoding"
梦旅人picnic 2024-09-25 00:49:57

如果有人需要这个 NGINX 配置文件,这里是代码片段:

location ~* \.(js|css|xml|gz)$ {
    add_header Vary "Accept-Encoding";
    (... other headers or rules ...)
}

if anyone needs this for NGINX configuration file here is the snippet:

location ~* \.(js|css|xml|gz)$ {
    add_header Vary "Accept-Encoding";
    (... other headers or rules ...)
}
童话 2024-09-25 00:49:57

无需指定甚至检查文件是否已压缩,
您可以根据每个请求将其发送到每个文件。

它告诉下游代理如何匹配未来的请求标头来决定
是否可以使用缓存的响应而不是请求新的响应
来自源服务器的一个。

<ifModule mod_headers.c>
  Header unset Vary
  Header set Vary "Accept-Encoding, X-HTTP-Method-Override, X-Forwarded-For, Remote-Address, X-Real-IP, X-Forwarded-Proto, X-Forwarded-Host, X-Forwarded-Port, X-Forwarded-Server"
</ifModule>
  • unset 是为了修复旧版 GoDaddy 托管中的一些错误(可选)。

No need to specify or even check if the file is/has compressed,
you can send it to every file, On every request.

It tells downstream proxies how to match future request headers to decide
whether the cached response can be used rather than requesting a fresh
one from the origin server.

<ifModule mod_headers.c>
  Header unset Vary
  Header set Vary "Accept-Encoding, X-HTTP-Method-Override, X-Forwarded-For, Remote-Address, X-Real-IP, X-Forwarded-Proto, X-Forwarded-Host, X-Forwarded-Port, X-Forwarded-Server"
</ifModule>
  • the unset is to fix some bugs in older GoDaddy hosting, optionally.
霓裳挽歌倾城醉 2024-09-25 00:49:56

我想这意味着您为 css 和 js 文件启用 gzip 压缩,因为这将使客户端能够接收 gzip 编码的内容和纯内容。

这是在 apache2 中执行此操作的方法:

<IfModule mod_deflate.c>
    #The following line is enough for .js and .css
    AddOutputFilter DEFLATE js css

    #The following line also enables compression by file content type, for the following list of Content-Type:s
    AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml

    #The following lines are to avoid bugs with some browsers
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html 
</IfModule>

以下是添加 Vary Accept-Encoding 标头的方法:[src]

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

Vary: 标头告知所提供的内容因为这个url会根据某个请求头的值而变化。这里它表示,它将为那些声称接受编码:gzip,deflate(请求标头)的客户端提供不同的内容,而不是为不发送此标头的客户端提供的内容。 AFAIK 的主要优点是让中间缓存代理知道由于这种更改,它们需要具有同一 url 的两个不同版本。

I guess it's meant that you enable gzip compression for your css and js files, because that will enable the client to receive both gzip-encoded content and a plain content.

This is how to do it in apache2:

<IfModule mod_deflate.c>
    #The following line is enough for .js and .css
    AddOutputFilter DEFLATE js css

    #The following line also enables compression by file content type, for the following list of Content-Type:s
    AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml

    #The following lines are to avoid bugs with some browsers
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html 
</IfModule>

And here's how to add the Vary Accept-Encoding header: [src]

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

The Vary: header tells the that the content served for this url will vary according to the value of a certain request header. Here it says that it will serve different content for clients who say they Accept-Encoding: gzip, deflate (a request header), than the content served to clients that do not send this header. The main advantage of this, AFAIK, is to let intermediate caching proxies know they need to have two different versions of the same url because of such change.

难理解 2024-09-25 00:49:56

恐怕 Aularon 没有提供足够的步骤来完成该过程。经过一些尝试和错误,我能够在我的专用 WHM 服务器上成功启用 Gzipping。

以下是步骤:

  • 在 WHM 中运行 EasyApache,在详尽选项列表中选择 Deflate,然后重建服务器。

  • 完成后,转到服务配置>> Apache 配置>>包括编辑器>>发布 VirtualHost Include,选择“所有版本”,然后将 mod_headers.c 和 mod_headers.c 代码(在 Aularon 的帖子中列出)粘贴到输入字段中的另一个代码的顶部。

  • 保存后,我发现平均节省了 75.36% 的数据流量!您可以使用此 HTTP 压缩工具运行前后测试来查看您自己的结果:http://www. Whatsmyip.org/http_compression/

希望这对大家有用!

  • 马特

I'm afraid Aularon didn't provide enough steps to complete the process. With a little trial and error, I was able to successfully enable Gzipping on my dedicated WHM server.

Below are the steps:

  • Run EasyApache within WHM, select Deflate within the Exhaustive Options list, and rebuild the server.

  • Once done, goto Services Configuration >> Apache Configuration >> Include Editor >> Post VirtualHost Include, select All Versions, and then paste the mod_headers.c and mod_headers.c code (listed above in Aularon's post) on top of on another within the input field.

  • Once saved, I was seeing a 75.36% data savings on average! You can run a before and after test by using this HTTP Compression tool to see your own results: http://www.whatsmyip.org/http_compression/

Hope this works for you all!

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