|
| 1 | +import std; |
| 2 | +# The minimal Varnish version is 3.0.5 |
| 3 | + |
| 4 | +backend default { |
| 5 | + .host = "127.0.0.1"; |
| 6 | + .port = "8080"; |
| 7 | +} |
| 8 | + |
| 9 | +acl purge { |
| 10 | + "127.0.0.1"; |
| 11 | +} |
| 12 | + |
| 13 | +sub vcl_recv { |
| 14 | + if (req.restarts == 0) { |
| 15 | + if (req.http.x-forwarded-for) { |
| 16 | + set req.http.X-Forwarded-For = |
| 17 | + req.http.X-Forwarded-For + ", " + client.ip; |
| 18 | + } else { |
| 19 | + set req.http.X-Forwarded-For = client.ip; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + if (req.request == "PURGE") { |
| 24 | + if (client.ip !~ purge) { |
| 25 | + error 405 "Method not allowed"; |
| 26 | + } |
| 27 | + if (!req.http.X-Magento-Tags-Pattern) { |
| 28 | + error 400 "X-Magento-Tags-Pattern header required"; |
| 29 | + } |
| 30 | + ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern); |
| 31 | + error 200 "Purged"; |
| 32 | + } |
| 33 | + |
| 34 | + if (req.request != "GET" && |
| 35 | + req.request != "HEAD" && |
| 36 | + req.request != "PUT" && |
| 37 | + req.request != "POST" && |
| 38 | + req.request != "TRACE" && |
| 39 | + req.request != "OPTIONS" && |
| 40 | + req.request != "DELETE") { |
| 41 | + /* Non-RFC2616 or CONNECT which is weird. */ |
| 42 | + return (pipe); |
| 43 | + } |
| 44 | + |
| 45 | + # We only deal with GET and HEAD by default |
| 46 | + if (req.request != "GET" && req.request != "HEAD") { |
| 47 | + return (pass); |
| 48 | + } |
| 49 | + |
| 50 | + # Bypass shopping cart and checkout requests |
| 51 | + if (req.url ~ "/checkout") { |
| 52 | + return (pass); |
| 53 | + } |
| 54 | + |
| 55 | + # normalize url in case of leading HTTP scheme and domain |
| 56 | + set req.url = regsub(req.url, "^http[s]?://", ""); |
| 57 | + |
| 58 | + # collect all cookies |
| 59 | + std.collect(req.http.Cookie); |
| 60 | + |
| 61 | + # static files are always cacheable. remove SSL flag and cookie |
| 62 | + if (req.url ~ "^/(pub/)?(media|static)/.*\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|gz|tgz|bz2|tbz|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)$") { |
| 63 | + unset req.http.Https; |
| 64 | + unset req.http.Cookie; |
| 65 | + } |
| 66 | + |
| 67 | + set req.grace = 1m; |
| 68 | + |
| 69 | + return (lookup); |
| 70 | +} |
| 71 | + |
| 72 | +sub vcl_hash { |
| 73 | + if (req.http.cookie ~ "X-Magento-Vary=") { |
| 74 | + hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1")); |
| 75 | + } |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | +sub vcl_fetch { |
| 80 | + if (beresp.http.content-type ~ "text") { |
| 81 | + set beresp.do_esi = true; |
| 82 | + } |
| 83 | + |
| 84 | + if (req.url ~ "\.js$" || beresp.http.content-type ~ "text") { |
| 85 | + set beresp.do_gzip = true; |
| 86 | + } |
| 87 | + |
| 88 | + # cache only successfully responses and 404s |
| 89 | + if (beresp.status != 200 && beresp.status != 404) { |
| 90 | + set beresp.ttl = 0s; |
| 91 | + return (hit_for_pass); |
| 92 | + } elsif (beresp.http.Cache-Control ~ "private") { |
| 93 | + return (hit_for_pass); |
| 94 | + } |
| 95 | + |
| 96 | + if (beresp.http.X-Magento-Debug) { |
| 97 | + set beresp.http.X-Magento-Cache-Control = beresp.http.Cache-Control; |
| 98 | + } |
| 99 | + |
| 100 | + # validate if we need to cache it and prevent from setting cookie |
| 101 | + # images, css and js are cacheable by default so we have to remove cookie also |
| 102 | + if (beresp.ttl > 0s && (req.request == "GET" || req.request == "HEAD")) { |
| 103 | + unset beresp.http.set-cookie; |
| 104 | + if (req.url !~ "\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)(\?|$)") { |
| 105 | + set beresp.http.Pragma = "no-cache"; |
| 106 | + set beresp.http.Expires = "-1"; |
| 107 | + set beresp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0"; |
| 108 | + set beresp.grace = 1m; |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +sub vcl_deliver { |
| 114 | + if (resp.http.X-Magento-Debug) { |
| 115 | + if (obj.hits > 0) { |
| 116 | + set resp.http.X-Magento-Cache-Debug = "HIT"; |
| 117 | + } else { |
| 118 | + set resp.http.X-Magento-Cache-Debug = "MISS"; |
| 119 | + } |
| 120 | + } else { |
| 121 | + unset resp.http.Age; |
| 122 | + } |
| 123 | + |
| 124 | + unset resp.http.X-Magento-Debug; |
| 125 | + unset resp.http.X-Magento-Tags; |
| 126 | + unset resp.http.X-Powered-By; |
| 127 | + unset resp.http.Server; |
| 128 | + unset resp.http.X-Varnish; |
| 129 | + unset resp.http.Via; |
| 130 | + unset resp.http.Link; |
| 131 | +} |
0 commit comments