Alister West

home is where your code is ...

Varnish - Standardise the Accept-Encoding

Varnish adds the header: "Vary: Accept-Encoding" and all my browsers send a slightly different Accept-Encoding header. Varnish won't cache pages where this differs - so we need to standardise this.

# Headers
Firefox:     Accept-Encoding: gzip,deflate
Chrome:      Accept-Encoding: gzip,deflate,sdch
Lynx:        Accept-Encoding: gzip, compress, bzip2
lwp-request: TE: deflate,gzip;q=0.3



# Homepage should be cached with varnish

#!/bin/bash
for i in `seq 88 99`; do
    echo $i 
    lwp-request -dUesS -H "Accept-Encoding: gzip,$i" http://www.mysite.com/ \
        | grep X-Cache;
done;

88 X-Cache: MISS
89 X-Cache: MISS
90 X-Cache: MISS
91 X-Cache: MISS
... 




# default.vcl
# 
# Don't cache multiple versions of the same page based on 
# the clients 'Accept-Encoding' header.
# See http://varnish-cache.org/wiki/FAQ/Compression
# 

sub vcl_recv {

    ...

    /* Standardise Accept-Encoding */
    if (req.http.Accept-Encoding) {
        if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
            # No point in compressing these
            remove req.http.Accept-Encoding;
        } elsif (req.http.Accept-Encoding ~ "gzip") {
            set req.http.Accept-Encoding = "gzip";
        } elsif (req.http.Accept-Encoding ~ "deflate") {
            set req.http.Accept-Encoding = "deflate";
        } else {
            # unkown algorithm
            remove req.http.Accept-Encoding;
        }
    }

    /* Some other handy varnish settings ... */

    /* override any X-Forwarded-For so we don't have spoofed proxies */
    set req.http.X-Forwarded-For = client.ip;

    if (req.http.Cache-Control ~ "no-cache") { purge_url(req.url); }
    if (req.request != "GET")                { return(pass);       }
    if (req.http.Authorization)              { return(pass);       }

    return(lookup);
}
By Alister West