Skip to content

Instantly share code, notes, and snippets.

@matthewjackowski
Last active June 19, 2021 01:35
Show Gist options
  • Save matthewjackowski/8e118f1f7e2aef5b5eb2a03631e734f7 to your computer and use it in GitHub Desktop.
Save matthewjackowski/8e118f1f7e2aef5b5eb2a03631e734f7 to your computer and use it in GitHub Desktop.

Revisions

  1. Matthew Jackowski revised this gist May 14, 2016. 1 changed file with 32 additions and 31 deletions.
    63 changes: 32 additions & 31 deletions edgecache.vcl
    Original file line number Diff line number Diff line change
    @@ -19,12 +19,12 @@ sub vcl_recv {

    # Setting http headers for backend
    set req.http.X-Forwarded-For = client.ip;
    set req.http.X-Forwarded-Proto = "https";
    set req.http.X-Forwarded-Proto = "https";

    # Unset headers that might cause us to cache duplicate infos
    unset req.http.Accept-Language;
    unset req.http.User-Agent;
    unset req.http.Vary;
    unset req.http.Accept-Language;
    unset req.http.User-Agent;
    unset req.http.Vary;

    # Redirect www to non-www
    if (req.http.host ~ "^www\.") {
    @@ -39,49 +39,50 @@ sub vcl_recv {
    }

    # Redirect mobile to homepage
    if (req.url ~ "/mobile/$" || req.url ~ "/m/$" ) {
    set req.url = "/";
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    }
    if (req.url ~ "/mobile/$" || req.url ~ "/m/$" ) {
    set req.url = "/";
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    }
    # Weird things bing looks for
    if (req.url ~ "sitemap\.aspx") {
    set req.url = "/sitemap_index.xml";
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    set req.url = "/sitemap_index.xml";
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    }
    if (req.url ~ "aboutus\.aspx$") {
    set req.url = "/about/";
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    if (req.url ~ "aboutus\.aspx$") {
    set req.url = "/about/";
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    }
    # drop params from static assets
    if (req.url ~ "\.(gif|jpg|jpeg|swf|ttf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
    set req.url = regsub(req.url, "\?.*$", "");
    }
    # drop params from static asset
    if (req.url ~ "\.(gif|jpg|jpeg|swf|ttf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
    set req.url = regsub(req.url, "\?.*$", "");
    }

    # drop tracking params
    if (req.url ~ "\?(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er|iew))=") {
    set req.url = regsub(req.url, "\?.*$", "");
    }
    if (req.url ~ "\?(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|null|feed|ref(id|src)?|v(er|iew))=") {
    set req.url = regsub(req.url, "\?.*$", "");
    }


    unset req.http.cookie;
    }
    }

    sub vcl_backend_response {
    # retry a few times if backend is down
    if (beresp.status == 503 && bereq.retries < 3 ) {
    if (beresp.status == 503 && bereq.retries < 3 ) {
    return(retry);
    }

    if (bereq.url ~ "\.(css|js|ttf)(\?.*|)$") {
    unset beresp.http.cookie;
    unset beresp.http.Set-Cookie;
    set beresp.ttl = 1d;
    unset beresp.http.Cache-Control;
    if (bereq.url ~ "\.(css|js|ttf)(\?.*|)$") {
    unset beresp.http.cookie;
    unset beresp.http.Set-Cookie;
    set beresp.ttl = 1d;
    unset beresp.http.Cache-Control;
    }

    if (beresp.ttl < 120s) {
    if (beresp.ttl < 120s) {
    unset beresp.http.cookie;
    unset beresp.http.Set-Cookie;
    set beresp.ttl = 120s;
  2. Matthew Jackowski revised this gist Apr 11, 2016. 1 changed file with 1 addition and 19 deletions.
    20 changes: 1 addition & 19 deletions edgecache.vcl
    Original file line number Diff line number Diff line change
    @@ -64,20 +64,8 @@ sub vcl_recv {
    if (req.url ~ "\?(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er|iew))=") {
    set req.url = regsub(req.url, "\?.*$", "");
    }
    # pass if admin url
    if (req.url ~ "(wp-login|wp-admin)" || req.url ~ "preview=true" || req.url ~ "xmlrpc.php") {
    return (pass);
    }

    # pass if admin cookies
    if (req.http.cookie) {
    if (req.http.cookie ~ "(wordpress_|wp-settings-)") {
    return(pass);
    } else {
    unset req.http.cookie;
    }
    }

    unset req.http.cookie;
    }

    sub vcl_backend_response {
    @@ -86,12 +74,6 @@ sub vcl_backend_response {
    return(retry);
    }

    # Don't store backend
    if (bereq.url ~ "wp-(login|admin)" || bereq.url ~ "preview=true") {
    set beresp.uncacheable = true;
    set beresp.ttl = 30s;
    return (deliver);
    }
    if (bereq.url ~ "\.(css|js|ttf)(\?.*|)$") {
    unset beresp.http.cookie;
    unset beresp.http.Set-Cookie;
  3. Matthew Jackowski revised this gist Apr 10, 2016. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions edgecache.vcl
    Original file line number Diff line number Diff line change
    @@ -92,6 +92,12 @@ sub vcl_backend_response {
    set beresp.ttl = 30s;
    return (deliver);
    }
    if (bereq.url ~ "\.(css|js|ttf)(\?.*|)$") {
    unset beresp.http.cookie;
    unset beresp.http.Set-Cookie;
    set beresp.ttl = 1d;
    unset beresp.http.Cache-Control;
    }

    if (beresp.ttl < 120s) {
    unset beresp.http.cookie;
  4. Matthew Jackowski revised this gist Apr 10, 2016. 1 changed file with 13 additions and 13 deletions.
    26 changes: 13 additions & 13 deletions edgecache.vcl
    Original file line number Diff line number Diff line change
    @@ -39,21 +39,21 @@ sub vcl_recv {
    }

    # Redirect mobile to homepage
    if (req.url ~ "/mobile/$" || req.url ~ "/m/$" ) {
    set req.url = "/";
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    }
    if (req.url ~ "/mobile/$" || req.url ~ "/m/$" ) {
    set req.url = "/";
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    }
    # Weird things bing looks for
    if (req.url ~ "sitemap.aspx$") {
    set req.url = "/sitemap_index.xml";
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    if (req.url ~ "sitemap\.aspx") {
    set req.url = "/sitemap_index.xml";
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    }
    if (req.url ~ "aboutus.​aspx$") {
    set req.url = "/about/";
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    if (req.url ~ "aboutus\.aspx$") {
    set req.url = "/about/";
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    }
    # drop params from static assets
    if (req.url ~ "\.(gif|jpg|jpeg|swf|ttf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
  5. Matthew Jackowski revised this gist Apr 10, 2016. 1 changed file with 19 additions and 2 deletions.
    21 changes: 19 additions & 2 deletions edgecache.vcl
    Original file line number Diff line number Diff line change
    @@ -33,11 +33,28 @@ sub vcl_recv {
    }

    # Redirect http to https
    if ( std.port(server.ip) == 80) {
    set req.http.x-redir = "https://" + req.http.host + req.url;
    if ( std.port(server.ip) == 80) {
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    }

    # Redirect mobile to homepage
    if (req.url ~ "/mobile/$" || req.url ~ "/m/$" ) {
    set req.url = "/";
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    }
    # Weird things bing looks for
    if (req.url ~ "sitemap​.aspx$") {
    set req.url = "/sitemap_index.xml";
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    }
    if (req.url ~ "aboutus.​aspx$") {
    set req.url = "/about/";
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    }
    # drop params from static assets
    if (req.url ~ "\.(gif|jpg|jpeg|swf|ttf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
    set req.url = regsub(req.url, "\?.*$", "");
  6. Matthew Jackowski revised this gist Apr 5, 2016. 1 changed file with 6 additions and 11 deletions.
    17 changes: 6 additions & 11 deletions edgecache.vcl
    Original file line number Diff line number Diff line change
    @@ -24,6 +24,7 @@ sub vcl_recv {
    # Unset headers that might cause us to cache duplicate infos
    unset req.http.Accept-Language;
    unset req.http.User-Agent;
    unset req.http.Vary;

    # Redirect www to non-www
    if (req.http.host ~ "^www\.") {
    @@ -68,26 +69,20 @@ sub vcl_backend_response {
    return(retry);
    }


    # For static content strip all backend cookies
    if (bereq.url ~ "\.(gif|jpg|jpeg|swf|ttf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
    unset beresp.http.cookie;
    }

    # Don't store backend
    if (bereq.url ~ "wp-(login|admin)" || bereq.url ~ "preview=true") {
    set beresp.uncacheable = true;
    set beresp.ttl = 30s;
    return (deliver);
    }

    # Only allow cookies to be set if we're in admin area
    if (!(bereq.url ~ "(wp-login|wp-admin|preview=true)")) {
    unset beresp.http.set-cookie;
    if (beresp.ttl < 120s) {
    unset beresp.http.cookie;
    unset beresp.http.Set-Cookie;
    set beresp.ttl = 120s;
    unset beresp.http.Cache-Control;
    }



    # remove extra headers
    unset beresp.http.X-Powered-By;
    unset beresp.http.Server;
  7. Matthew Jackowski revised this gist Apr 5, 2016. 1 changed file with 33 additions and 28 deletions.
    61 changes: 33 additions & 28 deletions edgecache.vcl
    Original file line number Diff line number Diff line change
    @@ -46,6 +46,20 @@ sub vcl_recv {
    if (req.url ~ "\?(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er|iew))=") {
    set req.url = regsub(req.url, "\?.*$", "");
    }
    # pass if admin url
    if (req.url ~ "(wp-login|wp-admin)" || req.url ~ "preview=true" || req.url ~ "xmlrpc.php") {
    return (pass);
    }

    # pass if admin cookies
    if (req.http.cookie) {
    if (req.http.cookie ~ "(wordpress_|wp-settings-)") {
    return(pass);
    } else {
    unset req.http.cookie;
    }
    }

    }

    sub vcl_backend_response {
    @@ -54,35 +68,26 @@ sub vcl_backend_response {
    return(retry);
    }

    # if we get a session cookie...caching is a no-go
    if (bereq.http.Cookie ~ "(UserID|_session)") {
    set beresp.http.X-Cacheable = "NO:Got Session";
    set beresp.uncacheable = true;
    return (deliver);

    } elsif (beresp.ttl <= 0s) {
    # Varnish determined the object was not cacheable
    set beresp.http.X-Cacheable = "NO:Not Cacheable";

    } elsif (beresp.http.set-cookie) {
    # Don't cache content for logged in users
    set beresp.http.X-Cacheable = "NO:Set-Cookie";
    set beresp.uncacheable = true;
    return (deliver);

    } elsif (beresp.http.Cache-Control ~ "private") {
    # Respect the Cache-Control=private header from the backend
    set beresp.http.X-Cacheable = "NO:Cache-Control=private";
    set beresp.uncacheable = true;
    return (deliver);

    } else {
    # Object is cacheable
    set beresp.http.X-Cacheable = "YES";
    }

    # unset cookies from backendresponse
    unset beresp.http.set-cookie;
    # For static content strip all backend cookies
    if (bereq.url ~ "\.(gif|jpg|jpeg|swf|ttf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
    unset beresp.http.cookie;
    }

    # Don't store backend
    if (bereq.url ~ "wp-(login|admin)" || bereq.url ~ "preview=true") {
    set beresp.uncacheable = true;
    set beresp.ttl = 30s;
    return (deliver);
    }

    # Only allow cookies to be set if we're in admin area
    if (!(bereq.url ~ "(wp-login|wp-admin|preview=true)")) {
    unset beresp.http.set-cookie;
    }



    # remove extra headers
    unset beresp.http.X-Powered-By;
    unset beresp.http.Server;
  8. Matthew Jackowski revised this gist Apr 2, 2016. 1 changed file with 6 additions and 20 deletions.
    26 changes: 6 additions & 20 deletions edgecache.vcl
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ import std;

    # Backend setup
    backend default {
    .host = "ec2-52-24-255-72.us-west-2.compute.amazonaws.com";
    .host = "brilliantcoding.us";
    .port = "80";
    }

    @@ -46,20 +46,6 @@ sub vcl_recv {
    if (req.url ~ "\?(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er|iew))=") {
    set req.url = regsub(req.url, "\?.*$", "");
    }
    # pass if admin url
    if (req.url ~ "(wp-login|wp-admin)" || req.url ~ "preview=true" || req.url ~ "xmlrpc.php") {
    return (pass);
    }

    # pass if admin cookies
    if (req.http.cookie) {
    if (req.http.cookie ~ "(wordpress_|wp-settings-)") {
    return(pass);
    } else {
    unset req.http.cookie;
    }
    }

    }

    sub vcl_backend_response {
    @@ -98,11 +84,11 @@ sub vcl_backend_response {
    # unset cookies from backendresponse
    unset beresp.http.set-cookie;
    # remove extra headers
    unset resp.http.X-Powered-By;
    unset resp.http.Server;
    unset resp.http.Via;
    unset resp.http.X-Pingback;
    unset resp.http.X-Varnish;
    unset beresp.http.X-Powered-By;
    unset beresp.http.Server;
    unset beresp.http.Via;
    unset beresp.http.X-Pingback;
    unset beresp.http.X-Varnish;

    }

  9. Matthew Jackowski revised this gist Apr 2, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions edgecache.vcl
    Original file line number Diff line number Diff line change
    @@ -8,8 +8,8 @@ import std;

    # Backend setup
    backend default {
    .host = "192.168.99.100";
    .port = "32777";
    .host = "ec2-52-24-255-72.us-west-2.compute.amazonaws.com";
    .port = "80";
    }

    sub vcl_recv {
  10. Matthew Jackowski renamed this gist Apr 1, 2016. 1 changed file with 13 additions and 3 deletions.
    16 changes: 13 additions & 3 deletions edge.vcl → edgecache.vcl
    Original file line number Diff line number Diff line change
    @@ -17,9 +17,6 @@ sub vcl_recv {
    # Only a single backend
    set req.backend_hint= default;

    # Drop cookies
    unset req.http.cookie;

    # Setting http headers for backend
    set req.http.X-Forwarded-For = client.ip;
    set req.http.X-Forwarded-Proto = "https";
    @@ -49,6 +46,19 @@ sub vcl_recv {
    if (req.url ~ "\?(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er|iew))=") {
    set req.url = regsub(req.url, "\?.*$", "");
    }
    # pass if admin url
    if (req.url ~ "(wp-login|wp-admin)" || req.url ~ "preview=true" || req.url ~ "xmlrpc.php") {
    return (pass);
    }

    # pass if admin cookies
    if (req.http.cookie) {
    if (req.http.cookie ~ "(wordpress_|wp-settings-)") {
    return(pass);
    } else {
    unset req.http.cookie;
    }
    }

    }

  11. Matthew Jackowski revised this gist Apr 1, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion edge.vcl
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,6 @@
    # Sets forwarded proxy headers
    # Custom error page
    vcl 4.0;
    import directors;
    import std;

    # Backend setup
  12. Matthew Jackowski revised this gist Apr 1, 2016. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion edge.vcl
    Original file line number Diff line number Diff line change
    @@ -87,7 +87,13 @@ sub vcl_backend_response {
    }

    # unset cookies from backendresponse
    unset beresp.http.set-cookie;
    unset beresp.http.set-cookie;
    # remove extra headers
    unset resp.http.X-Powered-By;
    unset resp.http.Server;
    unset resp.http.Via;
    unset resp.http.X-Pingback;
    unset resp.http.X-Varnish;

    }

  13. Matthew Jackowski revised this gist Apr 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion edge.vcl
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ import std;

    # Backend setup
    backend default {
    .host = "http://192.168.99.100";
    .host = "192.168.99.100";
    .port = "32777";
    }

  14. Matthew Jackowski revised this gist Apr 1, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions edge.vcl
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,8 @@ import std;

    # Backend setup
    backend default {
    .host = "wordpress";
    .port = "80";
    .host = "http://192.168.99.100";
    .port = "32777";
    }

    sub vcl_recv {
  15. Matthew Jackowski created this gist Apr 1, 2016.
    133 changes: 133 additions & 0 deletions edge.vcl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,133 @@
    # A basic setup for the edge
    # Strips cookies and un-needed params
    # Does a few redirects
    # Sets forwarded proxy headers
    # Custom error page
    vcl 4.0;
    import directors;
    import std;

    # Backend setup
    backend default {
    .host = "wordpress";
    .port = "80";
    }

    sub vcl_recv {

    # Only a single backend
    set req.backend_hint= default;

    # Drop cookies
    unset req.http.cookie;

    # Setting http headers for backend
    set req.http.X-Forwarded-For = client.ip;
    set req.http.X-Forwarded-Proto = "https";

    # Unset headers that might cause us to cache duplicate infos
    unset req.http.Accept-Language;
    unset req.http.User-Agent;

    # Redirect www to non-www
    if (req.http.host ~ "^www\.") {
    set req.http.x-redir = regsub(req.http.host, "^www\.(.*)", "https://\1") + req.url;
    return (synth(750, "Moved permanently"));
    }

    # Redirect http to https
    if ( std.port(server.ip) == 80) {
    set req.http.x-redir = "https://" + req.http.host + req.url;
    return (synth(750, "Moved permanently"));
    }

    # drop params from static assets
    if (req.url ~ "\.(gif|jpg|jpeg|swf|ttf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
    set req.url = regsub(req.url, "\?.*$", "");
    }

    # drop tracking params
    if (req.url ~ "\?(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er|iew))=") {
    set req.url = regsub(req.url, "\?.*$", "");
    }

    }

    sub vcl_backend_response {
    # retry a few times if backend is down
    if (beresp.status == 503 && bereq.retries < 3 ) {
    return(retry);
    }

    # if we get a session cookie...caching is a no-go
    if (bereq.http.Cookie ~ "(UserID|_session)") {
    set beresp.http.X-Cacheable = "NO:Got Session";
    set beresp.uncacheable = true;
    return (deliver);

    } elsif (beresp.ttl <= 0s) {
    # Varnish determined the object was not cacheable
    set beresp.http.X-Cacheable = "NO:Not Cacheable";

    } elsif (beresp.http.set-cookie) {
    # Don't cache content for logged in users
    set beresp.http.X-Cacheable = "NO:Set-Cookie";
    set beresp.uncacheable = true;
    return (deliver);

    } elsif (beresp.http.Cache-Control ~ "private") {
    # Respect the Cache-Control=private header from the backend
    set beresp.http.X-Cacheable = "NO:Cache-Control=private";
    set beresp.uncacheable = true;
    return (deliver);

    } else {
    # Object is cacheable
    set beresp.http.X-Cacheable = "YES";
    }

    # unset cookies from backendresponse
    unset beresp.http.set-cookie;

    }

    sub vcl_hash {
    if ( req.http.X-Forwarded-Proto ) {
    hash_data( req.http.X-Forwarded-Proto );
    }
    }


    sub vcl_synth {
    # redirect for http
    if (resp.status == 750) {
    set resp.status = 301;
    set resp.http.Location = req.http.x-redir;
    return(deliver);
    }
    # display custom error page if backend down
    if (resp.status == 503) {
    set resp.status = 404;
    synthetic(std.fileread("/etc/varnish/error.html"));
    return(deliver);
    }
    }


    sub vcl_deliver {
    # oh noes backend is down
    if (resp.status == 503) {
    return(restart);
    }
    if (obj.hits > 0) {
    set resp.http.X-Cache = "HIT";
    } else {
    set resp.http.X-Cache = "MISS";
    }
    set resp.http.Access-Control-Allow-Origin = "*";
    }
    sub vcl_hit {
    }

    sub vcl_miss {
    }