varnish4.vcl 7.1 KB

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