简体   繁体   中英

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. Does anyone have info about this?

Thanks!

This is from the nginx documentation .

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

Enables response header of "Vary: Accept-Encoding" . Note that this header causes IE 4-6 not to cache the content due to a bug (see 2 ).

There if you just add 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.

Inside the server { of the domain/subdomain that you want to set it, add

    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.

Simple. In Nginx conf:

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; parameter and restart or reload nginx service:

service nginx restart

This should fix the issue. If you are running an old version of nginx, you may need to enable gzip on; , as this is required for the vary header to work.

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/

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:

Make sure all your gzip_types are also set in /etc/nginx/nginx.conf http section.

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.

In /etc/nginx/nginx.conf you will find an 'http' section - in here, there is also a gzip_types and 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).

So adding the full list of gzip_types to the http section in nginx.conf fixed my issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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