Skip to content

Varnish VCL improvements #1575

Closed
Closed
@nagno

Description

@nagno

Hi,

I have some suggestions related to the Varnish VCL file.

  1. Inside vcl_recv to
    # Even though there are few possible values for Accept-Encoding, Varnish treats
    # them literally rather than semantically, so even a small difference which makes
    # no difference to the backend can reduce cache efficiency by making Varnish cache
    # too many different versions of an object.
    # https://www.varnish-cache.org/trac/wiki/FAQ/Compression
    if (req.http.Accept-Encoding) {
        if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$") {
            # No point in compressing these
            unset req.http.Accept-Encoding;
        } elsif (req.http.Accept-Encoding ~ "gzip") {
            set req.http.Accept-Encoding = "gzip";
        } elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") {
            set req.http.Accept-Encoding = "deflate";
        } else {
            # unkown algorithm
            unset req.http.Accept-Encoding;
        }
    }
  1. still inside vcl_recv:
    # Remove Google gclid parameters to minimize the cache objects
    set req.url = regsuball(req.url,"\?gclid=[^&]+$",""); # strips when QS = "?gclid=AAA"
    set req.url = regsuball(req.url,"\?gclid=[^&]+&","?"); # strips when QS = "?gclid=AAA&foo=bar"
    set req.url = regsuball(req.url,"&gclid=[^&]+",""); # strips when QS = "?foo=bar&gclid=AAA" or QS = "?foo=bar&gclid=AAA&bar=baz"
  1. inside vcl_hash:
    #for multi site configurations to not cache each-other's content
    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }

    # mainly to make sure, if the site was cached via a http connection and a visitor opens the 
    # https version, they won't see an ssl warning about mixed content (if the site was cached via http
    # connection, the external resources like css, js will be opened via an http connection as well
    # instead of https   
    if (req.http.X-Forwarded-Proto) {
        hash_data(req.http.X-Forwarded-Proto);
    }

What do you think about them? If you think, it is worth to add them, I'm happy to create a pull request.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions