Skip to content
Permalink
Browse files

Serve webp images if supported by client

  • Loading branch information
FlorianSW committed Nov 24, 2019
1 parent 9c4bd5d commit 3f27dbbc92a2904884693df72543d2efbaf3ac06
Showing with 33 additions and 11 deletions.
  1. +33 −11 modules/role/templates/varnish/default.vcl.erb
@@ -1,13 +1,18 @@
vcl 4.0;

backend default {
.host = "172.16.0.1";
.port = "80";
.host = "172.16.0.1";
.port = "80";
}

backend thumbor {
.host = "172.16.0.1";
.port = "8181";
}

acl purge {
"172.16.0.1";
"172.16.0.2";
"172.16.0.1";
"172.16.0.2";
}

sub vcl_recv {
@@ -33,10 +38,22 @@ sub vcl_recv {
return (pass);
}

if (req.url ~ "(?i)\.(jpg|jpeg|jpe|png)$" && req.url ~ "(?i)/thumb/" && req.http.Accept ~ "(?i)image/webp" && req.http.x-no-thumbor != "yes") {
set req.http.x-orig-url = req.url;
set req.url = regsub(req.url,".*\/thumb\/(.*)","/unsafe/filters:format(webp)/thumb/\1");
set req.backend_hint = thumbor;
}

if (req.http.Authorization || req.http.Cookie ~ "session" || req.http.Cookie ~ "Token") {
return (pass);
}

if (req.http.Cookie ~ "droidwikiwikicookiewarning_dismissed=true") {
set req.http.Cookie = "droidwikiwikicookiewarning_dismissed=true";
} else {
unset req.http.Cookie;
}

if (req.http.If-None-Match) {
return (pass);
}
@@ -61,17 +78,12 @@ sub vcl_recv {
}
}

// Remove has_js and Google Analytics __* cookies.
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_[_a-z]+|has_js)=[^;]*", "");
// Remove a ";" prefix, if present.
set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");

unset req.http.x-wap;
if(req.http.User-Agent ~ "(?i)^(lg-|sie-|nec-|lge-|sgh-|pg-)|(mobi|240x240|240x320|320x320|alcatel|android|audiovox|bada|benq|blackberry|cdm-|compal-|docomo|ericsson|hiptop|htc[-_]|huawei|ipod|kddi-|kindle|meego|midp|mitsu|mmp\/|mot-|motor|ngm_|nintendo|opera.m|palm|panasonic|philips|phone|playstation|portalmmm|sagem-|samsung|sanyo|sec-|sendo|sharp|softbank|symbian|teleca|up.browser|webos)") {
if (req.http.User-Agent ~ "(?i)^(lg-|sie-|nec-|lge-|sgh-|pg-)|(mobi|240x240|240x320|320x320|alcatel|android|audiovox|bada|benq|blackberry|cdm-|compal-|docomo|ericsson|hiptop|htc[-_]|huawei|ipod|kddi-|kindle|meego|midp|mitsu|mmp\/|mot-|motor|ngm_|nintendo|opera.m|palm|panasonic|philips|phone|playstation|portalmmm|sagem-|samsung|sanyo|sec-|sendo|sharp|softbank|symbian|teleca|up.browser|webos)") {
set req.http.x-wap = "no";
}

if(req.http.Cookie ~ "mf_useformat=") {
if (req.http.Cookie ~ "mf_useformat=") {
set req.http.x-wap = "no";
}

@@ -111,13 +123,23 @@ sub vcl_miss {
}

sub vcl_deliver {
if (resp.http.x-origin == "thumbor" && resp.status != 200) {
set req.url = req.http.x-orig-url;
set req.http.x-no-thumbor = "yes";
return (restart);
}

if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}

sub vcl_backend_error {
set beresp.http.x-origin = beresp.backend.name;
}

sub vcl_backend_response {
set beresp.grace = 120s;

0 comments on commit 3f27dbb

Please sign in to comment.
You can’t perform that action at this time.