Varnish Configuration Language: pezzi di VCL

Se siete finiti su questa pagina, probabilemente e’ perche’ siete alla ricerca di VCL da copiare ed incollare nella vostra configurazione.
Probabilmente avete gia’ provato altri pezzi di VCL, ma non hanno funzionato come vi aspettavate. Se questo e’ il caso, allora questo blog e’ per voi.
Le linee di VCL qui sotto riportate sono state tutte testate ripetutamente, ergo sono pronte per essere usate da voi.

  1. ACL and Purge

    # Who is allowed to purge
    acl local {
      “Localhost”;
      “192.168.1.0”/24; /* and everyone on the local network */
      ! “192.168.1.23”; /* except for the dialin router */
    }
        
    sub vcl_recv {
      if (req.method == “PURGE”) {
        if (client.ip ~ local) {
          return(purge);
        } else {
          return(synth(403, “Access  denied”));
        }
      } 
    }

  2. Authentication

    sub vcl_recv {
      if (req.http.authstatus) {
        unset req.http.authstatus;
      }
    
      if (req.http.signature) {
        set req.http.sig-verf = digest.hmac_sha256("key", req.http.host);
          if (req.http.sig-verf == req.http.signature) {
            set req.http.authstatus = "ok";
          }
      }
    
      if (req.http.authstatus == "ok") {
        # implement your logic for authenticated users
        return(synth(200, "ok"));
      } else {
        # implement your logic for unauthenticated users
        return(synth(401, "not ok"));
      }
    }

  3. Stale-while-revalidate

    sub vcl_hit {
      if (obj.ttl >= 0s) {
        # normal hit
        return (deliver);
      }
      # We have no fresh fish. Lets look at the stale ones.
      if (std.healthy(req.backend_hint)) {
        # Backend is healthy. Limit age to 10s.
        if (obj.ttl + 10s > 0s) {
          set req.http.grace = "normal(limited)";
          return (deliver);
         } else {
          # No candidate for grace. Fetch a fresh object.
          return(fetch);
         }
      } else {
        # backend is sick - use full grace
        if (obj.ttl + obj.grace > 0s) {
          set req.http.grace = "full";
          return (deliver);
        } else {
          # no graced object.
          return (fetch);
        }
      }
    }

  4. Stale-if-error

    sub try_stale_if_error {
      if (obj.ttl < 0s && obj.ttl + obj.grace > 0s) {
        if (req.restarts == 0) {
          set req.http.sie-enabled = true;
          return (fetch);
        } else {
          set req.http.sie-abandon = true;
          return (deliver);
        }
      }
    }
    
    sub vcl_backend_fetch {
      if (bereq.http.sie-abandon) {
        return (abandon);
      }
    }
    
    sub vcl_backend_response {
      if (beresp.status > 400 && bereq.http.sie-enabled) {
        return (abandon);
      }
    }
    
    sub vcl_backend_error {
      if (bereq.http.sie-enabled) {
        return (abandon);
      }
    }
    
    sub vcl_synth {
      if (resp.status == 503 && req.http.sie-enabled) {
        unset req.http.sie-enabled;
        return (restart);
      }
    }
    
    sub vcl_hit {
      call try_stale_if_error;
    }

  5. Redirect to a permanent or temporary location

    sub vcl_recv {
      unset req.http.location;
    
      if (req.url ~ "/permanent") {
        set req.http.location = "https://new.example.com" + req.url;
        return(synth(301));
      }
    
      if (req.url ~ "/temporary") {
        set req.http.location = "https://temporary.example.com" + req.url;
        return(synth(302));
      }
    }
    
    sub vcl_synth {
      # Permanent redirect
      if (resp.status == 301) {
        set resp.http.Location = req.http.location;
        return (deliver);
      }
    
      # Temporary redirect
      if (resp.status == 302) {
        set resp.http.Location = req.http.location;
        return (deliver);
       }
    }

Questo blog post e’ stato originariamente scritto qui: https://info.varnish-software.com/blog/varnish-configuration-language-vcl-snippets