Stripping out select querystring attribute/value pairs so varnish will not vary cache by them

前端 未结 7 1032
名媛妹妹
名媛妹妹 2020-12-29 10:57

My goal is to \"whitelist\" certain querystring attributes and their values so varnish will not vary cache between the urls.

Example:

Url 1: http:/         


        
7条回答
  •  借酒劲吻你
    2020-12-29 11:08

    There's something wrong with the RegEx.
    I changed the RegExes used in both regsub calls:

    sub normalize_req_url {
        # Clean up root URL
        if (req.url ~ "^/(?:\?.*)?$") {
            set req.url = "/";
        }
    
        # Strip out Google Analytics campaign variables
        # They are only needed by the javascript running on the page
        # utm_source, utm_medium, utm_campaign, gclid, ...
        if (req.url ~ "(\?|&)(gclid|cx|ie|cof|siteurl|zanpid|origin|utm_[a-z]+|mr:[A-z]+)=") {
            set req.url = regsuball(req.url, "(gclid|cx|ie|cof|siteurl|zanpid|origin|utm_[a-z]+|mr:[A-z]+)=[%\._A-z0-9-]+&?", "");
        }
        set req.url = regsub(req.url, "(\?&|\?|&)$", "");
    }
    

    The first change is the part "[%._A-z0-9-]", because the dash functioned like a range symbol, that's why I've moved it to the end, and the dot should be escaped.

    The second change is to not only remove a question mark at the remaining URL, but also an ampersand or question mark and ampersand.

提交回复
热议问题