Hateburo: kazeburo hatenablog

SRE / 運用系小姑 / Goを書くPerl Monger

Nginx gzip and Vary. How to

I want to output "Vary: Accept-Encoding,User-Agent".
But gzip_vary only adds Accept-Encoding. "add_header Vary" does not merge headers.

My answer is..

location / {
    gzip on;
    gzip_disable  "MSIE [1-6]\.";
    gzip_types text/css application/x-javascript;
    gzip_vary off;
    if ( $request_filename ~ "\.(html|css|js)$" ) {
        add_header Vary "Accept-Encoding,User-Agent";
    }
    root /path/to/htdocs
}

MSIE < 7 have a bug around gzip and cache. Do not response compressed javascript/css for those browser.