Skip to content

Instantly share code, notes, and snippets.

@ThijsFeryn
Last active July 28, 2021 18:57
Show Gist options
  • Select an option

  • Save ThijsFeryn/13d42093b39fa8b0932d54245ede344a to your computer and use it in GitHub Desktop.

Select an option

Save ThijsFeryn/13d42093b39fa8b0932d54245ede344a to your computer and use it in GitHub Desktop.

Revisions

  1. ThijsFeryn revised this gist Jul 28, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion magento.vcl
    Original file line number Diff line number Diff line change
    @@ -161,7 +161,6 @@ sub vcl_deliver {
    unset resp.http.Age;
    }

    # Not letting browser to cache non-static files.
    if (resp.http.Cache-Control !~ "private" && req.url !~ "^/(pub/)?(media|static)/") {
    set resp.http.Pragma = "no-cache";
    set resp.http.Expires = "-1";
  2. ThijsFeryn created this gist Jul 28, 2021.
    178 changes: 178 additions & 0 deletions magento.vcl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,178 @@
    vcl 4.0;

    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 {
    "localhost";
    }

    sub vcl_recv {
    if (req.url ~ "\?$") {
    set req.url = regsub(req.url, "\?$", "");
    }

    set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");
    set req.url = std.querysort(req.url);
    unset req.http.proxy;
    set req.url = regsub(req.url, "^http[s]?://", "");

    if (!req.http.X-Forwarded-Proto && (std.port(server.ip) == 443)) {
    set req.http.X-Forwarded-Proto = "https";
    }

    if (std.healthy(req.backend_hint)) {
    set req.grace = 300s;
    }

    if (req.method == "PURGE") {
    if (client.ip !~ purge) {
    return (synth(405, "Method not allowed"));
    }
    if (!req.http.X-Magento-Tags-Pattern && !req.http.X-Pool) {
    return (synth(400, "X-Magento-Tags-Pattern or X-Pool header required"));
    }
    if (req.http.X-Magento-Tags-Pattern) {
    ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
    }
    if (req.http.X-Pool) {
    ban("obj.http.X-Pool ~ " + req.http.X-Pool);
    }
    return (synth(200, "Purged"));
    }

    if (req.method != "GET" &&
    req.method != "HEAD" &&
    req.method != "PUT" &&
    req.method != "POST" &&
    req.method != "PATCH" &&
    req.method != "TRACE" &&
    req.method != "OPTIONS" &&
    req.method != "DELETE") {
    return (pipe);
    }

    if (req.method != "GET" && req.method != "HEAD") {
    return (pass);
    }

    if (req.url ~ "/checkout") {
    return (pass);
    }

    if (req.url ~ "^/(pub/)?(health_check.php)$") {
    return (pass);
    }

    std.collect(req.http.Cookie);

    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, "[?|&]+$", "");
    }

    if (req.url ~ "/graphql" && req.http.Authorization ~ "^Bearer") {
    return (pass);
    }

    return (hash);
    }

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

    hash_data(req.http.X-Forwarded-Proto);

    if (req.url ~ "/graphql") {
    hash_data(req.http.Store);
    hash_data(req.http.Content-Currency);
    }
    }

    sub vcl_backend_response {

    set beresp.grace = 3d;

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

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

    if (beresp.status != 200 && beresp.status != 404) {
    set beresp.ttl = 120s;
    set beresp.uncacheable = true;
    return (deliver);
    } elsif (beresp.http.Cache-Control ~ "private") {
    set beresp.uncacheable = true;
    set beresp.ttl = 86400s;
    return (deliver);
    }

    if (beresp.ttl > 0s && (bereq.method == "GET" || bereq.method == "HEAD")) {
    unset beresp.http.set-cookie;
    }

    if (beresp.ttl <= 0s ||
    beresp.http.Surrogate-control ~ "no-store" ||
    (!beresp.http.Surrogate-Control &&
    beresp.http.Cache-Control ~ "no-cache|no-store") ||
    beresp.http.Vary == "*") {
    set beresp.ttl = 120s;
    set beresp.uncacheable = true;
    }

    return (deliver);
    }

    sub vcl_deliver {
    if (resp.http.X-Magento-Debug) {
    if (obj.uncacheable) {
    set resp.http.X-Magento-Cache-Debug = "UNCACHEABLE";
    } else if (obj.hits) {
    set resp.http.X-Magento-Cache-Debug = "HIT";
    set resp.http.Grace = req.http.grace;
    } else {
    set resp.http.X-Magento-Cache-Debug = "MISS";
    }
    } else {
    unset resp.http.Age;
    }

    # Not letting browser to cache non-static files.
    if (resp.http.Cache-Control !~ "private" && req.url !~ "^/(pub/)?(media|static)/") {
    set resp.http.Pragma = "no-cache";
    set resp.http.Expires = "-1";
    set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
    }

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