├── README.md ├── prestashop.vcl └── wordpress.vcl /README.md: -------------------------------------------------------------------------------- 1 | # Varnish Examples 2 | Examples of Varnish configuration files for Clever Cloud 3 | 4 | ## How to use 5 | 6 | To use one of this files, you need to copy the content of the file you want to use, and put it in a `varnish.vcl` file. 7 | 8 | ## Contribute 9 | 10 | If you have a Varnish file for a CMS who's not present in the repository, or if you have an improvement for an existing one, just fork the repository and add/edit the content you want. Commit it, and create a pull request. 11 | -------------------------------------------------------------------------------- /prestashop.vcl: -------------------------------------------------------------------------------- 1 | # Only allow purging from specific IPs 2 | acl purge { 3 | "localhost"; 4 | "127.0.0.1"; 5 | } 6 | 7 | # This function is used when a request is send by a HTTP client (Browser) 8 | sub vcl_recv { 9 | # Normalize the header, remove the port (in case you're testing this on various TCP ports) 10 | set req.http.Host = regsub(req.http.Host, ":[0-9]+", ""); 11 | # Remove has_js and CloudFlare/Google Analytics __* cookies. 12 | set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_[_a-z]+|has_js)=[^;]*", ""); 13 | # Remove a ";" prefix, if present. 14 | set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", ""); 15 | # Allow purging from ACL 16 | if (req.method == "PURGE") { 17 | # If not allowed then a error 405 is returned 18 | if (!client.ip ~ purge) { 19 | return(synth(405, "This IP is not allowed to send PURGE requests.")); 20 | } 21 | # If allowed, do a cache_lookup -> vlc_hit() or vlc_miss() 22 | return (purge); 23 | } 24 | # Post requests will not be cached 25 | if (req.http.Authorization || req.method == "POST") { 26 | return (pass); 27 | } 28 | if (req.method == "GET" && (req.url ~ "^/?mylogout=")) { 29 | unset req.http.Cookie; 30 | return (pass); 31 | } 32 | #we should not cache any page for Prestashop backend 33 | if (req.method == "GET" && (req.url ~ "^/admin70")) { 34 | return (pass); 35 | } 36 | #we should not cache any page for customers 37 | if (req.method == "GET" && (req.url ~ "^/authentification" || req.url ~ "^/my-account")) { 38 | return (pass); 39 | } 40 | #we should not cache any page for customers 41 | if (req.method == "GET" && (req.url ~ "^/identity" || req.url ~ "^/my-account.php")) { 42 | return (pass); 43 | } 44 | #we should not cache any page for sales 45 | if (req.method == "GET" && (req.url ~ "^/cart.php" || req.url ~ "^/order.php")) { 46 | return (pass); 47 | } 48 | #we should not cache any page for sales 49 | if (req.method == "GET" && (req.url ~ "^/addresses.php" || req.url ~ "^/order-detail.php")) { 50 | return (pass); 51 | } 52 | #we should not cache any page for sales 53 | if (req.method == "GET" && (req.url ~ "^/order-confirmation.php" || req.url ~ "^/order-return.php")) { 54 | return (pass); 55 | } 56 | if (req.method != "GET" && req.method != "HEAD") { 57 | return (pass); 58 | } 59 | # Remove the "has_js" cookie 60 | set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", ""); 61 | # Remove any Google Analytics based cookies 62 | # set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", ""); 63 | # removes all cookies named __utm? (utma, utmb...) - tracking thing 64 | set req.http.Cookie = regsuball(req.http.Cookie, "(^|(?<=; )) *__utm.=[^;]+;? *", "\1"); 65 | # Remove a ";" prefix, if present. 66 | set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", ""); 67 | # Are there cookies left with only spaces or that are empty? 68 | if (req.http.Cookie ~ "^ *$") { 69 | unset req.http.Cookie; 70 | } 71 | # Cache the following files extensions 72 | if (req.url ~ "\.(css|js|png|gif|jp(e)?g|swf|ico|woff)") { 73 | unset req.http.Cookie; 74 | } 75 | # Normalize Accept-Encoding header and compression 76 | # https://www.varnish-cache.org/docs/3.0/tutorial/vary.html 77 | if (req.http.Accept-Encoding) { 78 | # Do no compress compressed files... 79 | if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") { 80 | unset req.http.Accept-Encoding; 81 | } elsif (req.http.Accept-Encoding ~ "gzip") { 82 | set req.http.Accept-Encoding = "gzip"; 83 | } elsif (req.http.Accept-Encoding ~ "deflate") { 84 | set req.http.Accept-Encoding = "deflate"; 85 | } else { 86 | unset req.http.Accept-Encoding; 87 | } 88 | } 89 | # Did not cache HTTP authentication and HTTP Cookie 90 | if (req.http.Authorization) { 91 | # Not cacheable by default 92 | return (pass); 93 | } 94 | # Cache all others requests 95 | return (hash); 96 | } 97 | sub vcl_pipe { 98 | return (pipe); 99 | } 100 | sub vcl_pass { 101 | return (fetch); 102 | } 103 | # The data on which the hashing will take place 104 | sub vcl_hash { 105 | hash_data(req.url); 106 | # If the client supports compression, keep that in a different cache 107 | if (req.http.Accept-Encoding) { 108 | hash_data(req.http.Accept-Encoding); 109 | } 110 | 111 | if (req.http.Cookie) { 112 | hash_data(req.http.Cookie); 113 | } 114 | return (lookup); 115 | } 116 | # This function is used when a request is sent by our backend (Nginx server) 117 | sub vcl_backend_response { 118 | # Remove some headers we never want to see 119 | unset beresp.http.Server; 120 | unset beresp.http.X-Powered-By; 121 | # For static content strip all backend cookies 122 | if (bereq.url ~ "\.(css|js|png|gif|jp(e?)g)|swf|ico|woff") { 123 | unset beresp.http.cookie; 124 | } 125 | # Don't store backend 126 | if (bereq.url ~ "admin70" || bereq.url ~ "preview=true") { 127 | set beresp.uncacheable = true; 128 | set beresp.ttl = 30s; 129 | return (deliver); 130 | } 131 | if (bereq.method == "GET" && (bereq.url ~ "^/?mylogout=")) { 132 | set beresp.ttl = 0s; 133 | unset beresp.http.Set-Cookie; 134 | set beresp.uncacheable = true; 135 | return(deliver); 136 | } 137 | # don't cache response to posted requests or those with basic auth 138 | if ( bereq.method == "POST" || bereq.http.Authorization ) { 139 | set beresp.uncacheable = true; 140 | set beresp.ttl = 120s; 141 | return (deliver); 142 | } 143 | # don't cache search results 144 | if ( bereq.url ~ "\?s=" ){ 145 | set beresp.uncacheable = true; 146 | set beresp.ttl = 120s; 147 | return (deliver); 148 | } 149 | # only cache status ok 150 | if ( beresp.status != 200 ) { 151 | set beresp.uncacheable = true; 152 | set beresp.ttl = 120s; 153 | return (deliver); 154 | } 155 | # A TTL of 2h 156 | set beresp.ttl = 2h; 157 | # Define the default grace period to serve cached content 158 | set beresp.grace = 30s; 159 | return (deliver); 160 | } 161 | # The routine when we deliver the HTTP request to the user 162 | # Last chance to modify headers that are sent to the client 163 | sub vcl_deliver { 164 | if (obj.hits > 0) { 165 | set resp.http.X-Varnish-Cache = "HIT"; 166 | set resp.http.X-Varnish = "HIT"; 167 | } else { 168 | set resp.http.X-Varnish-Cache = "MISS"; 169 | set resp.http.X-Varnish = "MISS"; 170 | } 171 | # Remove some headers: PHP version 172 | unset resp.http.X-Powered-By; 173 | # Remove some headers: Apache version & OS 174 | unset resp.http.Server; 175 | # Remove some heanders: Varnish 176 | unset resp.http.Via; 177 | unset resp.http.X-Varnish; 178 | return (deliver); 179 | } 180 | sub vcl_init { 181 | return (ok); 182 | } 183 | sub vcl_fini { 184 | return (ok); 185 | } 186 | -------------------------------------------------------------------------------- /wordpress.vcl: -------------------------------------------------------------------------------- 1 | acl purge { 2 | "localhost"; 3 | "127.0.0.1"; 4 | } 5 | 6 | sub vcl_backend_response { 7 | if ( (!(bereq.url ~ "(wp-(login|admin)|login)")) || (bereq.method == "GET") ) { 8 | unset beresp.http.set-cookie; 9 | set beresp.ttl = 1h; 10 | } 11 | 12 | if (bereq.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") { 13 | set beresp.ttl = 365d; 14 | } 15 | } 16 | 17 | sub vcl_recv { 18 | if (req.method == "PURGE") { 19 | if (!client.ip ~ purge) { 20 | return (synth(405, "Not allowed.")); 21 | } 22 | return (hash); 23 | } 24 | 25 | if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") { 26 | unset req.http.cookie; 27 | set req.url = regsub(req.url, "\?.*$", ""); 28 | } 29 | 30 | if (req.url ~ "\?(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er|iew))=") { 31 | set req.url = regsub(req.url, "\?.*$", ""); 32 | } 33 | 34 | if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true" || req.url ~ "xmlrpc.php") { 35 | return (pass); 36 | } 37 | 38 | if (req.http.cookie) { 39 | if (req.http.cookie ~ "(wordpress_|wp-settings-)") { 40 | return(pass); 41 | } else { 42 | unset req.http.cookie; 43 | } 44 | } 45 | } 46 | 47 | # Needed for SSL support 48 | sub vcl_hash { 49 | if ( req.http.X-Forwarded-Proto ) { 50 | hash_data( req.http.X-Forwarded-Proto ); 51 | } 52 | } 53 | 54 | sub vcl_deliver { 55 | # multi-server webfarm? set a variable here so you can check 56 | # the headers to see which frontend served the request 57 | # set resp.http.X-Server = "server-01"; 58 | if (obj.hits > 0) { 59 | set resp.http.X-Cache = "HIT"; 60 | } else { 61 | set resp.http.X-Cache = "MISS"; 62 | } 63 | } 64 | sub vcl_hit { 65 | if (req.method == "PURGE") { 66 | return (synth(200, "OK")); 67 | } 68 | } 69 | 70 | sub vcl_miss { 71 | if (req.method == "PURGE") { 72 | return (synth(404, "Not cached")); 73 | } 74 | } 75 | --------------------------------------------------------------------------------