Skip to content

Instantly share code, notes, and snippets.

@dvershinin
Last active August 24, 2022 08:50
Show Gist options
  • Select an option

  • Save dvershinin/de91aa78a96ae9f6e8d2950b3e1463a4 to your computer and use it in GitHub Desktop.

Select an option

Save dvershinin/de91aa78a96ae9f6e8d2950b3e1463a4 to your computer and use it in GitHub Desktop.

Revisions

  1. dvershinin revised this gist Oct 12, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion magento.vcl
    Original file line number Diff line number Diff line change
    @@ -200,7 +200,7 @@ sub vcl_hit {
    return (deliver);
    }
    if (std.healthy(req.backend_hint)) {
    if (obj.ttl + /* {{ grace_period }} */s > 0s) {
    if (obj.ttl + 259200s > 0s) {
    # Hit after TTL expiration, but within grace period
    set req.http.grace = "normal (healthy server)";
    return (deliver);
  2. dvershinin revised this gist Oct 12, 2020. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions magento.vcl
    Original file line number Diff line number Diff line change
    @@ -192,4 +192,25 @@ sub vcl_deliver {
    unset resp.http.X-Varnish;
    unset resp.http.Via;
    unset resp.http.Link;
    }

    sub vcl_hit {
    if (obj.ttl >= 0s) {
    # Hit within TTL period
    return (deliver);
    }
    if (std.healthy(req.backend_hint)) {
    if (obj.ttl + /* {{ grace_period }} */s > 0s) {
    # Hit after TTL expiration, but within grace period
    set req.http.grace = "normal (healthy server)";
    return (deliver);
    } else {
    # Hit after TTL and grace expiration
    return (fetch);
    }
    } else {
    # server is not healthy, retrieve from cache
    set req.http.grace = "unlimited (unhealthy server)";
    return (deliver);
    }
    }
  3. dvershinin revised this gist Oct 12, 2020. 1 changed file with 34 additions and 5 deletions.
    39 changes: 34 additions & 5 deletions magento.vcl
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,14 @@ import std;
    backend default {
    .host = "localhost";
    .port = "8080";
    .first_byte_timeout = 600s;
    .probe = {
    .url = "/health_check.php";
    .timeout = 2s;
    .interval = 5s;
    .window = 10;
    .threshold = 5;
    }
    }

    acl purge {
    @@ -56,18 +64,24 @@ sub vcl_recv {

    # collect all cookies
    std.collect(req.http.Cookie);

    # 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"

    # Remove all marketing get parameters to minimize the cache objects
    if (req.url ~ "(\?|&)(gclid|cx|ie|cof|siteurl|zanpid|origin|fbclid|mc_[a-z]+|utm_[a-z]+|_bta_[a-z]+)=") {
    set req.url = regsuball(req.url, "(gclid|cx|ie|cof|siteurl|zanpid|origin|fbclid|mc_[a-z]+|utm_[a-z]+|_bta_[a-z]+)=[-_A-z0-9+()%.]+&?", "");
    set req.url = regsub(req.url, "[?|&]+$", "");
    }

    # static files are always cacheable. remove SSL flag and cookie
    if (req.url ~ "^/(media|static)/.*\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)$") {
    unset req.http.Https;
    unset req.http.X-Forwarded-Proto;
    unset req.http.Cookie;
    }

    # Authenticated GraphQL requests should not be cached by default
    if (req.url ~ "/graphql" && req.http.Authorization ~ "^Bearer") {
    return (pass);
    }

    return (hash);
    }
    @@ -84,16 +98,31 @@ sub vcl_hash {
    hash_data(server.ip);
    }

    if (req.url ~ "/graphql") {
    call process_graphql_headers;
    }

    # To make sure http users don't see ssl warning
    if (req.http.X-Forwarded-Proto) {
    hash_data(req.http.X-Forwarded-Proto);
    }

    }

    sub process_graphql_headers {
    if (req.http.Store) {
    hash_data(req.http.Store);
    }
    if (req.http.Content-Currency) {
    hash_data(req.http.Content-Currency);
    }
    }

    sub vcl_backend_response {
    set beresp.http.X-Host = bereq.http.host;

    set beresp.grace = 3d;

    if (beresp.http.content-type ~ "text") {
    set beresp.do_esi = true;
    }
  4. dvershinin revised this gist Sep 11, 2020. No changes.
  5. dvershinin revised this gist Sep 13, 2019. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions magento.vcl
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,8 @@ sub vcl_recv {
    if (!req.http.X-Magento-Tags-Pattern) {
    return (purge);
    }
    if (req.http.host && req.http.host != "") {
    # required: https://github.com/magento/magento2/issues/4871
    if (req.http.host && req.http.host != "" && req.http.host != "127.0.0.1") {
    ban("obj.http.X-Host ~ " + req.http.host + " && obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
    } else {
    ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
    @@ -62,7 +63,7 @@ sub vcl_recv {
    set req.url = regsuball(req.url,"&gclid=[^&]+",""); # strips when QS = "?foo=bar&gclid=AAA" or QS = "?foo=bar&gclid=AAA&bar=baz"

    # static files are always cacheable. remove SSL flag and cookie
    if (req.url ~ "^/(pub/)?(media|static)/.*\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)$") {
    if (req.url ~ "^/(media|static)/.*\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)$") {
    unset req.http.Https;
    unset req.http.X-Forwarded-Proto;
    unset req.http.Cookie;
  6. dvershinin revised this gist Oct 4, 2017. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion magento.vcl
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,11 @@ sub vcl_recv {
    if (!req.http.X-Magento-Tags-Pattern) {
    return (purge);
    }
    ban("obj.http.X-Host ~ " + req.http.host + " && obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
    if (req.http.host && req.http.host != "") {
    ban("obj.http.X-Host ~ " + req.http.host + " && obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
    } else {
    ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
    }
    return (synth(200, "Purged"));
    }

  7. dvershinin revised this gist Apr 9, 2017. No changes.
  8. dvershinin revised this gist Apr 9, 2017. 1 changed file with 2 additions and 17 deletions.
    19 changes: 2 additions & 17 deletions magento.vcl
    Original file line number Diff line number Diff line change
    @@ -19,9 +19,9 @@ sub vcl_recv {
    return (synth(405, "Method not allowed"));
    }
    if (!req.http.X-Magento-Tags-Pattern) {
    return (synth(400, "X-Magento-Tags-Pattern header required"));
    return (purge);
    }
    ban("obj.http.X-Host ~ " + req.http.host + " && "obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
    ban("obj.http.X-Host ~ " + req.http.host + " && obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
    return (synth(200, "Purged"));
    }

    @@ -52,21 +52,6 @@ sub vcl_recv {
    # collect all cookies
    std.collect(req.http.Cookie);

    # Compression filter. See https://www.varnish-cache.org/trac/wiki/FAQ/Compression
    if (req.http.Accept-Encoding) {
    if (req.url ~ "\.(jpg|jpeg|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;
    }
    }

    # 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"
  9. dvershinin revised this gist Mar 16, 2017. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions magento.vcl
    Original file line number Diff line number Diff line change
    @@ -41,8 +41,8 @@ sub vcl_recv {
    return (pass);
    }
    # Bypass shopping cart, checkout and search requests
    if (req.url ~ "/checkout" || req.url ~ "/catalogsearch") {
    # Bypass shopping cart, checkout
    if (req.url ~ "/checkout") {
    return (pass);
    }
    @@ -139,6 +139,11 @@ sub vcl_backend_response {
    }
    }

    # "Microcache" for search
    if (bereq.url ~ "/catalogsearch") {
    set beresp.ttl = 30m;
    }

    # If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
    if (beresp.ttl <= 0s ||
    beresp.http.Surrogate-control ~ "no-store" ||
  10. dvershinin created this gist Feb 13, 2017.
    171 changes: 171 additions & 0 deletions magento.vcl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,171 @@
    vcl 4.0;

    import std;
    # The minimal Varnish version is 4.0
    # For SSL offloading, pass the following header in your proxy server or load balancer: 'X-Forwarded-Proto: https'

    backend default {
    .host = "localhost";
    .port = "8080";
    }

    acl purge {
    "localhost";
    }

    sub vcl_recv {
    if (req.method == "PURGE") {
    if (client.ip !~ purge) {
    return (synth(405, "Method not allowed"));
    }
    if (!req.http.X-Magento-Tags-Pattern) {
    return (synth(400, "X-Magento-Tags-Pattern header required"));
    }
    ban("obj.http.X-Host ~ " + req.http.host + " && "obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
    return (synth(200, "Purged"));
    }
    if (req.method != "GET" &&
    req.method != "HEAD" &&
    req.method != "PUT" &&
    req.method != "POST" &&
    req.method != "TRACE" &&
    req.method != "OPTIONS" &&
    req.method != "DELETE") {
    /* Non-RFC2616 or CONNECT which is weird. */
    return (pipe);
    }
    # We only deal with GET and HEAD by default
    if (req.method != "GET" && req.method != "HEAD") {
    return (pass);
    }
    # Bypass shopping cart, checkout and search requests
    if (req.url ~ "/checkout" || req.url ~ "/catalogsearch") {
    return (pass);
    }
    # normalize url in case of leading HTTP scheme and domain
    set req.url = regsub(req.url, "^http[s]?://", "");

    # collect all cookies
    std.collect(req.http.Cookie);

    # Compression filter. See https://www.varnish-cache.org/trac/wiki/FAQ/Compression
    if (req.http.Accept-Encoding) {
    if (req.url ~ "\.(jpg|jpeg|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;
    }
    }

    # 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"

    # static files are always cacheable. remove SSL flag and cookie
    if (req.url ~ "^/(pub/)?(media|static)/.*\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)$") {
    unset req.http.Https;
    unset req.http.X-Forwarded-Proto;
    unset req.http.Cookie;
    }

    return (hash);
    }

    sub vcl_hash {
    if (req.http.cookie ~ "X-Magento-Vary=") {
    hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
    }

    # 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);
    }

    # To make sure http users don't see ssl warning
    if (req.http.X-Forwarded-Proto) {
    hash_data(req.http.X-Forwarded-Proto);
    }

    }

    sub vcl_backend_response {
    set beresp.http.X-Host = bereq.http.host;

    if (beresp.http.content-type ~ "text") {
    set beresp.do_esi = true;
    }

    if (bereq.url ~ "\.js$" || beresp.http.content-type ~ "text") {
    set beresp.do_gzip = true;
    }

    # cache only successfully responses and 404s
    if (beresp.status != 200 && beresp.status != 404) {
    set beresp.ttl = 0s;
    set beresp.uncacheable = true;
    return (deliver);
    } elsif (beresp.http.Cache-Control ~ "private") {
    set beresp.uncacheable = true;
    set beresp.ttl = 86400s;
    return (deliver);
    }

    if (beresp.http.X-Magento-Debug) {
    set beresp.http.X-Magento-Cache-Control = beresp.http.Cache-Control;
    }

    # validate if we need to cache it and prevent from setting cookie
    # images, css and js are cacheable by default so we have to remove cookie also
    if (beresp.ttl > 0s && (bereq.method == "GET" || bereq.method == "HEAD")) {
    unset beresp.http.set-cookie;
    if (bereq.url !~ "\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|gz|tgz|bz2|tbz|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)(\?|$)") {
    set beresp.http.Pragma = "no-cache";
    set beresp.http.Expires = "-1";
    set beresp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
    set beresp.grace = 1m;
    }
    }

    # If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
    if (beresp.ttl <= 0s ||
    beresp.http.Surrogate-control ~ "no-store" ||
    (!beresp.http.Surrogate-Control && beresp.http.Vary == "*")) {
    # Mark as Hit-For-Pass for the next 2 minutes
    set beresp.ttl = 120s;
    set beresp.uncacheable = true;
    }
    return (deliver);
    }

    sub vcl_deliver {
    if (resp.http.X-Magento-Debug) {
    if (resp.http.x-varnish ~ " ") {
    set resp.http.X-Magento-Cache-Debug = "HIT";
    } else {
    set resp.http.X-Magento-Cache-Debug = "MISS";
    }
    } else {
    unset resp.http.Age;
    }

    unset resp.http.X-Magento-Debug;
    unset resp.http.X-Magento-Tags;
    unset resp.http.X-Powered-By;
    unset resp.http.Server;
    unset resp.http.X-Varnish;
    unset resp.http.Via;
    unset resp.http.Link;
    }