From ce224ba5f07d4e699e1652f81d44521b8de19500 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Thu, 10 Jan 2019 18:23:22 +0000 Subject: [PATCH 1/3] Streaming is enabled by default Support more filetypes for caching static media --- installation/pleroma.vcl | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/installation/pleroma.vcl b/installation/pleroma.vcl index 63c1cb74d..ca79eb7fc 100644 --- a/installation/pleroma.vcl +++ b/installation/pleroma.vcl @@ -42,15 +42,12 @@ sub vcl_recv { # Strip headers that will affect caching from all other static content # This also permits caching of individual toots and AP Activities if ((req.url ~ "^/(media|static)/") || - (req.url ~ "(?i)\.(html|js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|svg|swf|ttf|pdf|woff|woff2)$")) + (req.url ~ "(?i)\.(html|js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|mp4|ogg|webm|svg|swf|ttf|pdf|woff|woff2)$")) { unset req.http.Cookie; unset req.http.Authorization; return (hash); } - - # Everything else should just be piped to Pleroma - return (pipe); } sub vcl_backend_response { @@ -59,9 +56,6 @@ sub vcl_backend_response { set beresp.do_gzip = true; } - # etags are bad - unset beresp.http.etag; - # Don't cache objects that require authentication if (beresp.http.Authorization && !beresp.http.Cache-Control ~ "public") { set beresp.uncacheable = true; @@ -92,14 +86,12 @@ sub vcl_backend_response { } # Strip cache-restricting headers from Pleroma on static content that we want to cache - # Also enable streaming of cached content to clients (no waiting for Varnish to complete backend fetch) - if (bereq.url ~ "(?i)\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|svg|swf|ttf|pdf|woff|woff2)$") + if (bereq.url ~ "(?i)\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|mp4|ogg|webm|svg|swf|ttf|pdf|woff|woff2)$") { unset beresp.http.set-cookie; unset beresp.http.Cache-Control; unset beresp.http.x-request-id; set beresp.http.Cache-Control = "public, max-age=86400"; - set beresp.do_stream = true; } } From fda942c329696fc44d3671c5ca58a28c3c38f50a Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Thu, 10 Jan 2019 18:28:14 +0000 Subject: [PATCH 2/3] Cache partial objects for 10 minutes This enables caching/streaming of chunked responses --- installation/pleroma.vcl | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/installation/pleroma.vcl b/installation/pleroma.vcl index ca79eb7fc..30e552411 100644 --- a/installation/pleroma.vcl +++ b/installation/pleroma.vcl @@ -18,6 +18,11 @@ sub vcl_recv { return (synth(750, "")); } + # CHUNKED SUPPORT + if (req.http.Range ~ "bytes=") { + set req.http.x-range = req.http.Range; + } + # Pipe if WebSockets request is coming through if (req.http.upgrade ~ "(?i)websocket") { return (pipe); @@ -56,6 +61,12 @@ sub vcl_backend_response { set beresp.do_gzip = true; } + # CHUNKED SUPPORT + if (bereq.http.x-range ~ "bytes=" && beresp.status == 206) { + set beresp.ttl = 10m; + set beresp.http.CR = beresp.http.content-range; + } + # Don't cache objects that require authentication if (beresp.http.Authorization && !beresp.http.Cache-Control ~ "public") { set beresp.uncacheable = true; @@ -111,3 +122,26 @@ sub vcl_pipe { set bereq.http.connection = req.http.connection; } } + +sub vcl_hash { + # CHUNKED SUPPORT + if (req.http.x-range ~ "bytes=") { + hash_data(req.http.x-range); + unset req.http.Range; + } +} + +sub vcl_backend_fetch { + # CHUNKED SUPPORT + if (bereq.http.x-range) { + set bereq.http.Range = bereq.http.x-range; + } +} + +sub vcl_deliver { + # CHUNKED SUPPORT + if (resp.http.CR) { + set resp.http.Content-Range = resp.http.CR; + unset resp.http.CR; + } +} From efaa41fad2db59b9fd730013b0074edd8c2da29c Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Thu, 10 Jan 2019 18:29:36 +0000 Subject: [PATCH 3/3] Consistent intentation --- installation/pleroma.vcl | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/installation/pleroma.vcl b/installation/pleroma.vcl index 30e552411..92153d8ef 100644 --- a/installation/pleroma.vcl +++ b/installation/pleroma.vcl @@ -14,8 +14,8 @@ acl purge { sub vcl_recv { # Redirect HTTP to HTTPS if (std.port(server.ip) != 443) { - set req.http.x-redir = "https://" + req.http.host + req.url; - return (synth(750, "")); + set req.http.x-redir = "https://" + req.http.host + req.url; + return (synth(750, "")); } # CHUNKED SUPPORT @@ -25,23 +25,23 @@ sub vcl_recv { # Pipe if WebSockets request is coming through if (req.http.upgrade ~ "(?i)websocket") { - return (pipe); + return (pipe); } # Allow purging of the cache if (req.method == "PURGE") { - if (!client.ip ~ purge) { - return(synth(405,"Not allowed.")); - } - return(purge); + if (!client.ip ~ purge) { + return(synth(405,"Not allowed.")); + } + return(purge); } # Pleroma MediaProxy - strip headers that will affect caching if (req.url ~ "^/proxy/") { - unset req.http.Cookie; - unset req.http.Authorization; - unset req.http.Accept; - return (hash); + unset req.http.Cookie; + unset req.http.Authorization; + unset req.http.Accept; + return (hash); } # Strip headers that will affect caching from all other static content @@ -86,9 +86,9 @@ sub vcl_backend_response { # Do not cache redirects and errors if ((beresp.status >= 300) && (beresp.status < 500)) { - set beresp.uncacheable = true; - set beresp.ttl = 30s; - return (deliver); + set beresp.uncacheable = true; + set beresp.ttl = 30s; + return (deliver); } # Pleroma MediaProxy internally sets headers properly @@ -118,8 +118,8 @@ sub vcl_synth { # Ensure WebSockets through the pipe do not close prematurely sub vcl_pipe { if (req.http.upgrade) { - set bereq.http.upgrade = req.http.upgrade; - set bereq.http.connection = req.http.connection; + set bereq.http.upgrade = req.http.upgrade; + set bereq.http.connection = req.http.connection; } }