简体   繁体   English

设置变化:接受编码 Header (nginx)

[英]Set Vary: Accept-Encoding Header (nginx)

I have an nginx server and can't seem to find any information on how to send Vary: Accept-Encoding headers for CSS and JS files.我有一个 nginx 服务器,似乎找不到任何有关如何发送 Vary: Accept-Encoding headers for CSS 和 JS 文件的信息。 Does anyone have info about this?有人有这方面的信息吗?

Thanks!谢谢!

This is from the nginx documentation .这来自 nginx文档

 gzip_vary syntax: gzip_vary on|off default: gzip_vary off context: http, server, location

Enables response header of "Vary: Accept-Encoding" .启用"Vary: Accept-Encoding"的响应 header 。 Note that this header causes IE 4-6 not to cache the content due to a bug (see 2 ).请注意,此 header 会导致 IE 4-6 由于错误而无法缓存内容(请参阅2 )。

There if you just add gzip_vary on;如果你只是添加gzip_vary on; it should do it's job.它应该做它的工作。

Also make sure you have any one of the directives gzip, gzip_static, or gunzip are active.还要确保您有任何一个 gzip、gzip_static 或 gunzip 指令处于活动状态。

Inside the server { of the domain/subdomain that you want to set it, add在要设置的域/子域的server {中,添加

    gzip on;
    gzip_min_length  1100;
    gzip_buffers  4 32k;
    gzip_types    text/plain application/x-javascript text/xml text/css;
    gzip_vary on;

Save the file and restart nginx.保存文件并重启 nginx。

Simple.简单的。 In Nginx conf:在 Nginx 配置文件中:

vim /etc/nginx/nginx.conf

Add the following near the bottom under the section:在该部分的底部附近添加以下内容:

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

Just uncomment the gzip_vary on;只需取消注释gzip_vary on; parameter and restart or reload nginx service:参数并重新启动或重新加载 nginx 服务:

service nginx restart

This should fix the issue.这应该可以解决问题。 If you are running an old version of nginx, you may need to enable gzip on;如果您运行的是旧版本的 nginx,您可能需要启用gzip on; , as this is required for the vary header to work. ,因为这是使不同的 header 工作所必需的。

I hope this helps.我希望这有帮助。 FYI, this applies to all server/site conf files unless overwritten in their own respective server blocks.仅供参考,这适用于所有服务器/站点配置文件,除非在它们各自的服务器块中被覆盖。 server {}

Source info for nginx, apache and IIS: https://www.maxcdn.com/blog/accept-encoding-its-vary-important/ Source info for nginx, apache and IIS: https://www.maxcdn.com/blog/accept-encoding-its-vary-important/

If you've tried the other answers and are still seeing vary off (eg if you are testing speed of your page using GTMetrix), the following might help:如果您已经尝试了其他答案并且仍然看到变化(例如,如果您正在使用 GTMetrix 测试页面的速度),那么以下可能会有所帮助:

Make sure all your gzip_types are also set in /etc/nginx/nginx.conf http section.确保您的所有 gzip_types 也在 /etc/nginx/nginx.conf http 部分中设置。

The server block in /etc/nginx/sites-available/* is one place where you can set the vary header and gzip_types, but depending on how you have your site setup you may be processing and returning files before they reach that bit of config. /etc/nginx/sites-available/* 中的服务器块是您可以设置不同的 header 和 gzip_types 的地方,但根据您的站点设置方式,您可能会在文件到达该配置位之前处理和返回文件.

In /etc/nginx/nginx.conf you will find an 'http' section - in here, there is also a gzip_types and gzip_vary.在 /etc/nginx/nginx.conf 中,你会找到一个“http”部分——在这里,还有一个 gzip_types 和 gzip_vary。

For me - CSS and JS are static files and were being served up before hitting the (wordpress) nginx sites-available file (I have varnish in front of it).对我来说 - CSS 和 JS 是 static 文件,并且在点击(wordpress)nginx 站点可用文件(我在它的前面有varnish)之前被提供。

So adding the full list of gzip_types to the http section in nginx.conf fixed my issue.因此,将 gzip_types 的完整列表添加到 nginx.conf 中的 http 部分解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM