varnish5.vcl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # VCL version 5.0 is not supported so it should be 4.0 even though actually used Varnish version is 5
  2. vcl 4.0;
  3. import std;
  4. # The minimal Varnish version is 5.0
  5. # For SSL offloading, pass the following header in your proxy server or load balancer: '/* {{ ssl_offloaded_header }} */: https'
  6. backend default {
  7. .host = "/* {{ host }} */";
  8. .port = "/* {{ port }} */";
  9. .first_byte_timeout = 600s;
  10. .probe = {
  11. .url = "/pub/health_check.php";
  12. .timeout = 2s;
  13. .interval = 5s;
  14. .window = 10;
  15. .threshold = 5;
  16. }
  17. }
  18. acl purge {
  19. /* {{ ips }} */
  20. }
  21. sub vcl_recv {
  22. if (req.method == "PURGE") {
  23. if (client.ip !~ purge) {
  24. return (synth(405, "Method not allowed"));
  25. }
  26. # To use the X-Pool header for purging varnish during automated deployments, make sure the X-Pool header
  27. # has been added to the response in your backend server config. This is used, for example, by the
  28. # capistrano-magento2 gem for purging old content from varnish during it's deploy routine.
  29. if (!req.http.X-Magento-Tags-Pattern && !req.http.X-Pool) {
  30. return (synth(400, "X-Magento-Tags-Pattern or X-Pool header required"));
  31. }
  32. if (req.http.X-Magento-Tags-Pattern) {
  33. ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
  34. }
  35. if (req.http.X-Pool) {
  36. ban("obj.http.X-Pool ~ " + req.http.X-Pool);
  37. }
  38. return (synth(200, "Purged"));
  39. }
  40. if (req.method != "GET" &&
  41. req.method != "HEAD" &&
  42. req.method != "PUT" &&
  43. req.method != "POST" &&
  44. req.method != "TRACE" &&
  45. req.method != "OPTIONS" &&
  46. req.method != "DELETE") {
  47. /* Non-RFC2616 or CONNECT which is weird. */
  48. return (pipe);
  49. }
  50. # We only deal with GET and HEAD by default
  51. if (req.method != "GET" && req.method != "HEAD") {
  52. return (pass);
  53. }
  54. # Bypass shopping cart, checkout and search requests
  55. if (req.url ~ "/checkout" || req.url ~ "/catalogsearch") {
  56. return (pass);
  57. }
  58. # Bypass health check requests
  59. if (req.url ~ "/pub/health_check.php") {
  60. return (pass);
  61. }
  62. # Set initial grace period usage status
  63. set req.http.grace = "none";
  64. # normalize url in case of leading HTTP scheme and domain
  65. set req.url = regsub(req.url, "^http[s]?://", "");
  66. # collect all cookies
  67. std.collect(req.http.Cookie);
  68. # Compression filter. See https://www.varnish-cache.org/trac/wiki/FAQ/Compression
  69. if (req.http.Accept-Encoding) {
  70. if (req.url ~ "\.(jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$") {
  71. # No point in compressing these
  72. unset req.http.Accept-Encoding;
  73. } elsif (req.http.Accept-Encoding ~ "gzip") {
  74. set req.http.Accept-Encoding = "gzip";
  75. } elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") {
  76. set req.http.Accept-Encoding = "deflate";
  77. } else {
  78. # unknown algorithm
  79. unset req.http.Accept-Encoding;
  80. }
  81. }
  82. # Remove Google gclid parameters to minimize the cache objects
  83. set req.url = regsuball(req.url,"\?gclid=[^&]+$",""); # strips when QS = "?gclid=AAA"
  84. set req.url = regsuball(req.url,"\?gclid=[^&]+&","?"); # strips when QS = "?gclid=AAA&foo=bar"
  85. set req.url = regsuball(req.url,"&gclid=[^&]+",""); # strips when QS = "?foo=bar&gclid=AAA" or QS = "?foo=bar&gclid=AAA&bar=baz"
  86. # Static files caching
  87. if (req.url ~ "^/(pub/)?(media|static)/") {
  88. # Static files should not be cached by default
  89. return (pass);
  90. # But if you use a few locales and don't use CDN you can enable caching static files by commenting previous line (#return (pass);) and uncommenting next 3 lines
  91. #unset req.http.Https;
  92. #unset req.http./* {{ ssl_offloaded_header }} */;
  93. #unset req.http.Cookie;
  94. }
  95. return (hash);
  96. }
  97. sub vcl_hash {
  98. if (req.http.cookie ~ "X-Magento-Vary=") {
  99. hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
  100. }
  101. # For multi site configurations to not cache each other's content
  102. if (req.http.host) {
  103. hash_data(req.http.host);
  104. } else {
  105. hash_data(server.ip);
  106. }
  107. # To make sure http users don't see ssl warning
  108. if (req.http./* {{ ssl_offloaded_header }} */) {
  109. hash_data(req.http./* {{ ssl_offloaded_header }} */);
  110. }
  111. /* {{ design_exceptions_code }} */
  112. }
  113. sub vcl_backend_response {
  114. set beresp.grace = 3d;
  115. if (beresp.http.content-type ~ "text") {
  116. set beresp.do_esi = true;
  117. }
  118. if (bereq.url ~ "\.js$" || beresp.http.content-type ~ "text") {
  119. set beresp.do_gzip = true;
  120. }
  121. if (beresp.http.X-Magento-Debug) {
  122. set beresp.http.X-Magento-Cache-Control = beresp.http.Cache-Control;
  123. }
  124. # cache only successfully responses and 404s
  125. if (beresp.status != 200 && beresp.status != 404) {
  126. set beresp.ttl = 0s;
  127. set beresp.uncacheable = true;
  128. return (deliver);
  129. } elsif (beresp.http.Cache-Control ~ "private") {
  130. set beresp.uncacheable = true;
  131. set beresp.ttl = 86400s;
  132. return (deliver);
  133. }
  134. # validate if we need to cache it and prevent from setting cookie
  135. if (beresp.ttl > 0s && (bereq.method == "GET" || bereq.method == "HEAD")) {
  136. unset beresp.http.set-cookie;
  137. }
  138. # If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
  139. if (beresp.ttl <= 0s ||
  140. beresp.http.Surrogate-control ~ "no-store" ||
  141. (!beresp.http.Surrogate-Control &&
  142. beresp.http.Cache-Control ~ "no-cache|no-store") ||
  143. beresp.http.Vary == "*") {
  144. # Mark as Hit-For-Pass for the next 2 minutes
  145. set beresp.ttl = 120s;
  146. set beresp.uncacheable = true;
  147. }
  148. return (deliver);
  149. }
  150. sub vcl_deliver {
  151. if (resp.http.X-Magento-Debug) {
  152. if (resp.http.x-varnish ~ " ") {
  153. set resp.http.X-Magento-Cache-Debug = "HIT";
  154. set resp.http.Grace = req.http.grace;
  155. } else {
  156. set resp.http.X-Magento-Cache-Debug = "MISS";
  157. }
  158. } else {
  159. unset resp.http.Age;
  160. }
  161. # Not letting browser to cache non-static files.
  162. if (resp.http.Cache-Control !~ "private" && req.url !~ "^/(pub/)?(media|static)/") {
  163. set resp.http.Pragma = "no-cache";
  164. set resp.http.Expires = "-1";
  165. set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
  166. }
  167. unset resp.http.X-Magento-Debug;
  168. unset resp.http.X-Magento-Tags;
  169. unset resp.http.X-Powered-By;
  170. unset resp.http.Server;
  171. unset resp.http.X-Varnish;
  172. unset resp.http.Via;
  173. unset resp.http.Link;
  174. }
  175. sub vcl_hit {
  176. if (obj.ttl >= 0s) {
  177. # Hit within TTL period
  178. return (deliver);
  179. }
  180. if (std.healthy(req.backend_hint)) {
  181. if (obj.ttl + /* {{ grace_period }} */s > 0s) {
  182. # Hit after TTL expiration, but within grace period
  183. set req.http.grace = "normal (healthy server)";
  184. return (deliver);
  185. } else {
  186. # Hit after TTL and grace expiration
  187. return (miss);
  188. }
  189. } else {
  190. # server is not healthy, retrieve from cache
  191. set req.http.grace = "unlimited (unhealthy server)";
  192. return (deliver);
  193. }
  194. }