Set Vary: Accept-Encoding Header (nginx)

前端 未结 4 1667
生来不讨喜
生来不讨喜 2021-02-05 01:47

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?

Thank

相关标签:
4条回答
  • 2021-02-05 02:01

    If you've tried the other answers and are still seeing vary off (e.g. 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.

    0 讨论(0)
  • 2021-02-05 02:06

    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.

    0 讨论(0)
  • 2021-02-05 02:13

    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/

    0 讨论(0)
  • 2021-02-05 02:14

    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.

    0 讨论(0)
提交回复
热议问题